-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.js
50 lines (36 loc) · 1.27 KB
/
logging.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var fs = require('node:fs')
const path = require('node:path')
const logging = (logLevel, message) => {
// Create a seperate folder for logging
const folderName = 'logs'
if (!fs.existsSync(folderName)) {
fs.mkdir(folderName, {recursive:true}, (err) => {
if(err) {
console.error(`Error appending to file: ${err}`);
} else {
console.log('Content appended successfully.');
appendToFile();
}
});
} else {
appendToFile();
}
function appendToFile() {
//Log filename based on the date
let today = new Date();
let year = today.getFullYear()
let month = String(today.getMonth() + 1).padStart(2, '0')
let date = String(today.getDate()).padStart(2, '0')
logFileName = `logs_${year+month+date}.txt`
// Date time
logTime = new Date().toLocaleString();
// log level [Error, Info, warn]
logLevel = "[" + logLevel + "]";
// Message
message = message;
logMessage = logTime + "\t" + logLevel + "\t" + message + "\n";
console.log(logMessage);
fs.appendFile(`${folderName}/${logFileName}`, logMessage, function (err) {})
}
}
module.exports = logging