Practice: Log File Summary
Read a file, split lines and count levels.
Example
const fs = require("fs"), os = require("os"), path = require("path");
const f = path.join(os.tmpdir(), "log_n.txt");
fs.writeFileSync(f, "INFO a\nERROR b\nINFO c\nERROR d\nWARN e\n");
const counts = {};
for (const line of fs.readFileSync(f, "utf8").trim().split("\n")) { const l = line.split(" ")[0]; counts[l] = (counts[l] || 0) + 1; }
console.log(counts);
fs.unlinkSync(f);Result
{ INFO: 2, ERROR: 2, WARN: 1 }Exercise
Split lines.
.split("")
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.