-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
27 lines (23 loc) · 851 Bytes
/
Copy pathutils.js
File metadata and controls
27 lines (23 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const fs = require("fs");
function formatLog(entry, format = "plain") {
switch (format) {
case "json":
return JSON.stringify(entry);
case "csv":
return `${entry.timestamp},${entry.method},${entry.route},${entry.status},${entry.ip},${entry.duration}`;
default: // plain
return `[${entry.level.toUpperCase()}] ${entry.timestamp} - ${entry.method} ${entry.route} (${entry.status}) from ${entry.ip} in ${entry.duration}`;
}
}
function writeToFile(logText, filePath) {
fs.appendFile(filePath, logText + "\n", (err) => {
if (err) console.error("Error writing to log file:", err);
});
}
function shouldLog(entry, filter = {}) {
for (const key in filter) {
if (entry[key] !== filter[key]) return false;
}
return true;
}
module.exports = { formatLog, writeToFile, shouldLog };