-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.js
107 lines (93 loc) · 2.87 KB
/
container.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
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
require('dotenv').config();
const { inspect } = require('util');
const winston = require('winston');
require('winston-daily-rotate-file');
const LOG_NAME = process.env.LOG_NAME || 'app';
const LOG_STYLE = process.env.LOG_STYLE || 'rotate'; // single, rotate, both
const LOG_LEVEL = process.env.LOG_LEVEL || 'info';
const LOG_PATH = process.env.LOG_PATH || 'logs/';
const LOG_DAYS = process.env.LOG_DAYS || '180d';
const LOG_DATE_PATTERN = process.env.LOG_DATE_PATTERN || 'YYYY-MM-DD';
const LOG_CONSOLE = process.env.LOG_CONSOLE || 'on';
const LOG_CONSOLE_LEVEL = process.env.LOG_CONSOLE_LEVEL || 'debug';
// ***** Transports *****
/**
* Transport for Logging based con level definition
* @returns {winston.transports.FileTransportInstance}
*/
const logTransport = () => new winston.transports.File({
level: LOG_LEVEL,
filename: `${LOG_PATH}/${LOG_NAME}_${LOG_LEVEL}.log`,
});
/**
* Transport for Logging error only
* @returns {winston.transports.FileTransportInstance}
*/
const errorTransport = () => new winston.transports.File({
level: 'error',
filename: `${LOG_PATH}/${LOG_NAME}_error.log`,
});
/**
* Transport for Logging based con level definition
* @returns {DailyRotateFile}
*/
const dailyRotateTransport = () => new winston.transports.DailyRotateFile({
dirname: LOG_PATH,
filename: `${LOG_NAME}_%DATE%.log`,
datePattern: LOG_DATE_PATTERN,
maxFiles: LOG_DAYS,
});
/**
* Transport for Logging based con level definition
* @returns {winston.transports.ConsoleTransportInstance}
*/
const consoleTransport = () => new winston.transports.Console({
level: LOG_CONSOLE_LEVEL,
handleExceptions: true,
format: winston.format.combine(
winston.format.splat(),
winston.format.colorize(),
winston.format.printf((error) => {
const message = error.stack ? error.stack : error.message;
return `${error.level}: ${inspect(message, { showHidden: true, depth: null, colors: true })}`;
}),
),
});
// ***** Logger Containers *****
/**
* @constant {winston.Container}
*/
const container = new winston.Container();
/**
* @constant {Array<winston.transports.Transport>}
*/
const transports = [];
if (LOG_STYLE === 'single' || LOG_STYLE === 'both') {
transports.push(logTransport());
transports.push(errorTransport());
}
if (LOG_STYLE === 'rotate' || LOG_STYLE === 'both') {
transports.push(dailyRotateTransport());
}
if (process.env.NODE_ENV === 'development' || LOG_CONSOLE === 'on') {
transports.push(consoleTransport());
}
if (transports.length > 0) {
container.add('default', {
level: LOG_LEVEL,
format: winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.errors({ stack: true }),
winston.format.splat(),
winston.format.json(),
),
defaultMeta: { service: LOG_NAME },
transports,
exitOnError: false,
});
}
module.exports = {
container,
transports,
winston,
};