1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const winston = require('winston');
|
17 |
|
18 | const { LogLevel, StderrLogLevels } = require('./Constants');
|
19 |
|
20 | const levels = {
|
21 | [LogLevel.ERROR]: 0,
|
22 | [LogLevel.WARN]: 1,
|
23 | [LogLevel.DATA]: 2,
|
24 | [LogLevel.INFO]: 3,
|
25 | [LogLevel.DEBUG]: 4
|
26 | };
|
27 |
|
28 | let logger = {};
|
29 |
|
30 | const formatter = (options) => {
|
31 | const printMsgOnly = options.level === LogLevel.INFO || options.level === LogLevel.DATA;
|
32 | if (printMsgOnly) {
|
33 | return `${options.message}`;
|
34 | }
|
35 |
|
36 | const levelPart = logger.stripColors ? `[${options.level}]` : `[${winston.config.colorize(options.level)}]`;
|
37 |
|
38 | if (options.level === LogLevel.ERROR) {
|
39 |
|
40 | const name = options.meta.name ? options.meta.name + ': ' : '';
|
41 | const errMsg = options.message ? options.message : `${name}${options.meta.message}`;
|
42 | const finalErrMsg = `${levelPart} ${errMsg}`;
|
43 | return finalErrMsg;
|
44 | }
|
45 |
|
46 | return `${levelPart} ${options.message}`;
|
47 | };
|
48 |
|
49 | logger = new (winston.Logger)({
|
50 | levels,
|
51 | level: LogLevel.INFO,
|
52 | stripColors: false,
|
53 | transports: [
|
54 | new (winston.transports.Console)({
|
55 | formatter,
|
56 | stderrLevels: StderrLogLevels
|
57 | })
|
58 | ]
|
59 | });
|
60 |
|
61 | module.exports = logger;
|