-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
61 lines (50 loc) · 1.25 KB
/
Copy pathlogger.js
File metadata and controls
61 lines (50 loc) · 1.25 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import {appendFile} from 'fs';
import {join, resolve, dirname} from 'path';
import {fileURLToPath} from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const logLevels = {
DEBUG: 'DEBUG',
INFO: 'INFO',
WARN: 'WARN',
ERROR: 'ERROR',
};
let logFilePath = resolve(join(__dirname, 'log/server.log'));
// Helper function to write logs to the log file
function writeLog(level, msg) {
const time = new Date().toISOString().replace('T', ' ').substring(0, 19);
const logMessage = `[${time}] [${level}] ${msg}\n`;
appendFile(logFilePath, logMessage, (err) => {
if (err) {
console.error(`Error opening the file: ${logFilePath}`);
}
});
}
// Log a debug message
function debug(msg) {
writeLog(logLevels.DEBUG, msg);
}
// Log an info message
function info(msg) {
writeLog(logLevels.INFO, msg);
}
// Log a warning message
function warn(msg) {
writeLog(logLevels.WARN, msg);
}
// Log an error message
function error(msg) {
writeLog(logLevels.ERROR, msg);
}
// Set the path to the log file
function setLogFile(path) {
logFilePath = path;
}
// Export the functions as a module
export default {
debug,
info,
warn,
error,
setLogFile,
};