-
Notifications
You must be signed in to change notification settings - Fork 5
/
loggerConfig.js
57 lines (43 loc) · 1.94 KB
/
loggerConfig.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
const { LoggingWinston } = require('@google-cloud/logging-winston');
const winston = require('winston');
const packageInfo = require('./package.json');
const {
APPLICATION_API_LOG_LEVEL, APPLICATION_API_LOG_TRANSPORT, APPLICATION_API_LOGGER_NAME, MAX_LOG_BODY_LENGTH = 1000,
} = process.env;
const appStackDriver = new LoggingWinston({
logName: `${APPLICATION_API_LOGGER_NAME || 'botfront-api_log_app'}`,
level: APPLICATION_API_LOG_LEVEL,
});
const logsTransport = [];
if (!!APPLICATION_API_LOG_TRANSPORT) {
if (APPLICATION_API_LOG_TRANSPORT.includes('console')) logsTransport.push(new winston.transports.Console({level: APPLICATION_API_LOG_LEVEL}));
if (APPLICATION_API_LOG_TRANSPORT.includes('stackdriver')) logsTransport.push(appStackDriver);
} else {
logsTransport.push(new winston.transports.Console({level: APPLICATION_API_LOG_LEVEL}))
}
const checkBodyLength = (meta, key) => (meta !==undefined
&& meta[key]
&& meta[key].body
&& JSON.stringify(meta[key].body).length > MAX_LOG_BODY_LENGTH);
const customFormat = winston.format.printf((arg) => {
const {timestamp, level, message, meta} = arg
const cleanedMeta = meta || ''
if (checkBodyLength(cleanedMeta, 'res')){
cleanedMeta.res.body = 'Body is too large to display in logs.'
}
if (checkBodyLength(cleanedMeta, 'req')){
cleanedMeta.req.body = 'Body is too large to display in logs.'
}
if (cleanedMeta !== '')
return `${timestamp} [${level}]: ${message} res: ${JSON.stringify(cleanedMeta.res)} req: ${JSON.stringify(cleanedMeta.req)} `
return `${timestamp} [${level}]: ${message}`
});
const winstonInstance= winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.colorize(),
customFormat),
transports: logsTransport,
});
winstonInstance.info(`Botfront api ${packageInfo.version} started`)
module.exports = { winstonInstance, logsTransport };