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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 | 12x
12x
12x
12x
12x
23x
23x
23x
23x
12x
12x
12x
12x
12x
12x
12x
24x
24x
24x
90x
90x
24x
12x
12x
12x
84x
602x
602x
974x
602x
12x
12x
26x
26x
182x
| import {injectable} from "inversify";
const winston = require('winston');
const WinstonGraylog2 = require('winston-graylog2');
const logLevels = winston.config.npm.levels;
/**
* Logs Levels
*/
const alignedWithColorsAndTime = winston.format.combine(
winston.format.colorize(),
winston.format.timestamp(),
winston.format.align(),
winston.format.printf((info: any) => {
const {
timestamp, level, message, ...args
} = info;
const ts = timestamp.slice(0, 19).replace('T', ' ');
return `${ts} [${level}]: ${message} ${Object.keys(args).length ? JSON.stringify(args, null, 2) : ''}`;
}),
);
/**
* Creates winston transports
* @returns {Array}
*/
const createTransports = () => {
const transports: any[] = [];
Eif (process.env.ENABLE_CONSOLE_ERROR || process.env.NODE_ENV != 'production' || process.env.ENVIRONMENT != "production") {
transports.push(new winston.transports.Console({
level: process.env.logLevel || 'warn',
handleExceptions: false,
json: true,
colorize: true,
format: alignedWithColorsAndTime
}));
}
Iif (process.env.GRAYLOG_HOST && process.env.GRAYLOG_PORT && process.env.GRAYLOG_HOSTNAME && process.env.GRAYLOG_FACILITY) {
transports.push(new WinstonGraylog2({
name: 'Graylog',
level: process.env.logLevel || 'warn',
silent: false,
handleExceptions: false,
graylog: {
servers: [{host: process.env.GRAYLOG_HOST, port: +process.env.GRAYLOG_PORT}],
hostname: process.env.GRAYLOG_HOSTNAME,
facility: process.env.GRAYLOG_FACILITY,
bufferSize: 1400,
},
staticMeta: {}
}));
}
return transports;
};
Object.defineProperty(Error.prototype, 'toJSON', {
value(this: { [name: string]: string }) {
const alt: any = {};
const propNames = Object.getOwnPropertyNames(this);
for (let i = 0, len = propNames.length; i < len; i++) {
const key = propNames[i];
alt[key] = this[key];
}
return alt;
},
configurable: true,
writable: true
});
/**
* Create global logger
*/
const _logger = winston.createLogger({
transports: createTransports()
});
export const logger: any = {};
Object.keys(logLevels).forEach(level => {
logger[level] = function () {
let log = ``;
for (let i = 0, len = arguments.length; i < len; i++) {
log += `${JSON.stringify(arguments[i], null, 4)}\r\n`;
}
_logger[level](log);
};
});
/**
* Register to error events
*/
Iif (process.env.NODE_ENV === 'production') {
process
.on('unhandledRejection', (reason, p) => {
logger.error('UN_HANDLED_REJECTION', reason, p);
})
.on('uncaughtException', err => {
logger.error('UN_CAUGHT_EXCEPTION', err);
});
}
@injectable()
export class Logger {
logger: any;
[name: string]: (...args: any[]) => void;
constructor() {
this.logger = logger;
Object.keys(logLevels).forEach(level => {
this[level] = logger[level];
});
}
write(message: string) {
let level = 'info';
if (message.match(/RES: [4]/)) {
level = 'warn';
} else if (message.match(/RES: [5]/)) {
level = 'error';
}
logger[level](message);
}
}
|