forked from diegogurpegui/qbittorrent-auto-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
71 lines (65 loc) · 1.62 KB
/
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
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
62
63
64
65
66
67
68
69
70
71
const fs = require("fs")
const config = require("./config")
// level numbers
const levels = {
all: 0,
debug: 10,
info: 20,
warning: 30,
error: 40
}
// get the current level
const currentLevel = config.logger.level.toLowerCase()
// create the logs directory
try {
fs.mkdirSync(config.logger.directory)
} catch (err) {
// probably because the log directory already existed
}
// Create the stream for the log file
let logFileStream = fs.createWriteStream(config.logger.directory + "/app.log", {
flags: "a"
})
function logMessage(level, msg, data) {
level = level.toLowerCase()
// only log if it is withing the current level
if (levels[level] >= levels[currentLevel]) {
if (config.logger.console) {
let consoleData = data || ""
if (level === "error") {
console.error(msg, consoleData)
} else {
console.log(msg, consoleData)
}
}
let dateStr = new Date().toISOString()
if (typeof data === "undefined") {
data = ""
} else {
// try to parse the object to string
try {
data = " => " + JSON.stringify(data)
} catch (err) {
// couldn't parse, then concat as it is
data = " => " + data
}
}
let message = `[${dateStr}] - ${level} - ${msg} ${data}` + "\r\n"
logFileStream.write(message)
}
}
let logger = {
debug: function(msg, data) {
logMessage("debug", msg, data)
},
info: function(msg, data) {
logMessage("info", msg, data)
},
warning: function(msg, data) {
logMessage("warning", msg, data)
},
error: function(msg, data) {
logMessage("error", msg, data)
}
}
module.exports = logger