-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
logger.js
77 lines (64 loc) · 1.81 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
72
73
74
75
76
77
import winston from 'winston';
import fs from 'fs';
import path from 'path';
import DailyRotateFile from 'winston-daily-rotate-file';
let LOGS_FOLDER = './logs/';
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
LOGS_FOLDER = './test_logs/'
}
LOGS_FOLDER = process.env.PARSE_SERVER_LOGS_FOLDER || LOGS_FOLDER;
let currentLogsFolder = LOGS_FOLDER;
function generateTransports(level) {
let transports = [
new (DailyRotateFile)({
filename: 'parse-server.info',
dirname: currentLogsFolder,
name: 'parse-server',
level: level
}),
new (DailyRotateFile)({
filename: 'parse-server.err',
dirname: currentLogsFolder,
name: 'parse-server-error',
level: 'error'
})
]
if (!process.env.TESTING || process.env.VERBOSE) {
transports = [new (winston.transports.Console)({
colorize: true,
level:level
})].concat(transports);
}
return transports;
}
const logger = new winston.Logger();
export function configureLogger({logsFolder, level = winston.level}) {
winston.level = level;
logsFolder = logsFolder || currentLogsFolder;
if (!path.isAbsolute(logsFolder)) {
logsFolder = path.resolve(process.cwd(), logsFolder);
}
if (!fs.existsSync(logsFolder)) {
fs.mkdirSync(logsFolder);
}
currentLogsFolder = logsFolder;
logger.configure({
transports: generateTransports(level)
})
}
configureLogger({logsFolder: LOGS_FOLDER});
export function addGroup(groupName) {
let level = winston.level;
let transports = generateTransports().concat(new (DailyRotateFile)({
filename: groupName,
dirname: currentLogsFolder,
name: groupName,
level: level
}));
winston.loggers.add(groupName, {
transports: transports
});
return winston.loggers.get(groupName);
}
export { logger };
export default logger;