-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
30 lines (24 loc) · 877 Bytes
/
logger.js
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
const fs = require('fs');
const path = require('path');
const logFilePath = path.join(__dirname, 'site.log');
const logStream = fs.createWriteStream(logFilePath, { flags: 'a' });
function getTimestamp() {
const date = new Date();
return `[${date.toLocaleString()}]`;
}
const log = function(...args) {
const message = args.map(arg => typeof arg === 'object' ? JSON.stringify(arg) : arg).join(' ');
const timestamp = getTimestamp();
logStream.write(`${timestamp} ${message}\n`);
console.log(`${timestamp} ${message}`);
};
const error = function(...args) {
const message = args.map(arg => typeof arg === 'object' ? JSON.stringify(arg) : arg).join(' ');
const timestamp = getTimestamp();
logStream.write(`${timestamp} [ERROR] ${message}\n`);
console.error(`${timestamp} [ERROR] ${message}`);
};
module.exports = {
log,
error
};