-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.js
52 lines (49 loc) · 1.34 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
const winston = require('winston');
const config = require('config');
const path = require('path');
const fs = require('fs');
const logOptions = {
level: config.Log.level,
silent: config.Log.silent,
transports: [new winston.transports.Console()],
format: winston.format.combine(
winston.format.timestamp({
colorize: false
}),
winston.format.json()
),
defaultMeta: { service: config.App.name }
};
let logger = winston.createLogger(logOptions);
module.exports = function (fileName) {
const name = path.basename(fileName);
return {
switchToFile: () => {
try {
fs.unlinkSync(fileName);
} catch (error) {
// Do Noting
}
const newLogOption = logOptions;
newLogOption.transports = [
new winston.transports.File({ filename: fileName })
];
logger = winston.createLogger(newLogOption);
},
info: (message, meta) => {
logger.info(message, mergeMeta({ file: name }, meta));
},
error: (message, meta) => {
logger.error(message, mergeMeta({ file: name }, meta));
},
debug: (message, meta) => {
logger.debug(message, mergeMeta({ file: name }, meta));
},
verbose: (message, meta) => {
logger.verbose(message, mergeMeta({ file: name }, meta));
}
};
};
function mergeMeta(...objs) {
return Object.assign({}, ...objs);
}