-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinstonLogger.js
147 lines (128 loc) · 3.37 KB
/
WinstonLogger.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import winston, { format } from 'winston';
import fs from 'fs';
import path from 'path';
import DailyRotateFile from 'winston-daily-rotate-file';
import _ from 'lodash';
import defaults from '../../defaults';
const WinstonElastic = require('winston-elasticsearch').ElasticsearchTransport
const logger = winston.createLogger();
function configureTransports(options) {
console.log(options);
const transports = [];
if(!_.isNil(options.esClientOptions)) {
const esTransportOpts = {
level: options.level,
clientOpts: { node: options.esClientOptions.url }
};
const esTransport = new WinstonElastic(esTransportOpts);
transports.push(esTransport);
}
if (options) {
const silent = options.silent;
delete options.silent;
try {
if (!_.isNil(options.dirname)) {
const parseServer = new DailyRotateFile(
Object.assign(
{
filename: 'parse-server.info',
json: true,
format: format.combine(
format.timestamp(),
format.splat(),
format.json()
),
},
options
)
);
parseServer.name = 'parse-server';
transports.push(parseServer);
const parseServerError = new DailyRotateFile(
Object.assign(
{
filename: 'parse-server.err',
json: true,
format: format.combine(
format.timestamp(),
format.splat(),
format.json()
),
},
options,
{ level: 'error' }
)
);
parseServerError.name = 'parse-server-error';
transports.push(parseServerError);
}
} catch (e) {
/* */
}
const consoleFormat = options.json ? format.json() : format.simple();
const consoleOptions = Object.assign(
{
colorize: true,
name: 'console',
silent,
format: consoleFormat,
},
options
);
transports.push(new winston.transports.Console(consoleOptions));
}
logger.configure({
transports,
});
}
export function configureLogger({
logsFolder = defaults.logsFolder,
jsonLogs = defaults.jsonLogs,
logLevel = winston.level,
verbose = defaults.verbose,
silent = defaults.silent,
maxLogFiles,
} = {}) {
if (verbose) {
logLevel = 'verbose';
}
winston.level = logLevel;
const options = {};
if (logsFolder) {
if (!path.isAbsolute(logsFolder)) {
logsFolder = path.resolve(process.cwd(), logsFolder);
}
try {
fs.mkdirSync(logsFolder);
} catch (e) {
/* */
}
}
options.dirname = logsFolder;
options.level = logLevel;
options.silent = silent;
options.maxFiles = maxLogFiles;
if (jsonLogs) {
options.json = true;
options.stringify = true;
}
configureTransports(options);
}
export function addTransport(transport) {
// we will remove the existing transport
// before replacing it with a new one
removeTransport(transport.name);
logger.add(transport);
}
export function removeTransport(transport) {
const matchingTransport = logger.transports.find(t1 => {
return typeof transport === 'string'
? t1.name === transport
: t1 === transport;
});
if (matchingTransport) {
logger.remove(matchingTransport);
}
}
export { logger };
export default logger;