-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogging.js
58 lines (51 loc) · 1.35 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
51
52
53
54
55
56
57
58
const winston = require("winston");
const path = require("path");
var logger;
var enumerateErrorFormat = winston.format(info => {
if (info.message instanceof Error) {
info.message = Object.assign({
message: info.message.message,
stack: info.message.stack
}, info.message);
}
if (info instanceof Error) {
return Object.assign({
message: info.message,
stack: info.stack
}, info);
}
return info;
});
module.exports = function (directory) {
if (!logger) {
logger = winston.createLogger({
transports: [
new winston.transports.File({
filename: path.join(__dirname, "logs", directory, "error.log"),
level: "error"
}),
new winston.transports.File({
filename: path.join(__dirname, "logs", directory, "warn.log"),
level: "warn"
}),
new winston.transports.File({
filename: path.join(__dirname, "logs", directory, "http.log"),
level: "http"
})
],
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp(),
enumerateErrorFormat(),
winston.format.printf(info => `${info.timestamp} ${info.level}: ${info.message} ${info.stack ? `\n${info.stack}` : ""}`)
),
exitOnError: false
});
logger.stream = {
write: (message, encoding) => {
logger.http(message);
}
};
}
return logger;
};