-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
57 lines (55 loc) · 1.55 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
const winston = require('winston');
const config = require('config');
const path = require('path');
const fs = require('fs');
let logger = winston.createLogger({
level: config.Log.level,
silent: config.Log.silent,
transports: [new winston.transports.Console()],
format: winston.format.combine(
winston.format.timestamp({
format: 'MMM-DD-YYYY HH:mm:ss.SSS'
}),
winston.format.json()
),
defaultMeta: { service: config.App.name }
});
module.exports = function (fileName) {
const name = path.basename(fileName);
return {
switchToFile: () => {
try {
fs.unlinkSync(fileName);
} catch (error) {
// Do Noting
}
logger = winston.createLogger({
level: config.Log.level,
silent: config.Log.silent,
transports: [new winston.transports.File({ filename: fileName })],
format: winston.format.combine(
winston.format.timestamp({
format: 'MMM-DD-YYYY HH:mm:ss.SSS'
}),
winston.format.json()
),
defaultMeta: { service: config.App.name }
});
},
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);
}