-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandardlogger.js
59 lines (48 loc) · 1.49 KB
/
standardlogger.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
const { ElasticsearchTransport } = require('winston-elasticsearch');
const winston = require('winston');
const { combine, timestamp, printf, colorize, align, splat } = winston.format;
// default logging config
const DEFAULT_CONFIG = {
loglevel: 'info',
ESlogging: {
'enabled': false,
'label': 'DEFAULT',
'loglevel:': 'info',
'options' : {
'indexPrefix': 'logs',
'clientOpts': {
'node': 'http://localhost:9200'
}
}
}
}
// merge default with the one from the config file
const config = Object.assign({}, DEFAULT_CONFIG, require('./config.js').parse());
const addLabel = winston.format((info) => {
return {"label": config.ESlogging.label, ...info};
})();
const consoleFormat = combine(
timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
printf((info) => {
let { timestamp, level, message, ...leftovers } = info;
return `[${info.timestamp}] ${info.level.padEnd(7).toUpperCase()} | ${message} | ${JSON.stringify(leftovers)}`;
})
);
const esTransportOpts = {
format: combine(addLabel),
level: config.ESlogging.loglevel,
...config.ESlogging.options
};
const esTransport = new winston.transports.Elasticsearch(esTransportOpts);
const logger = winston.createLogger({
transports: [
new winston.transports.Console({
level: config.loglevel,
format: consoleFormat
}),
esTransport
],
});
module.exports = { logger };