-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.ts
32 lines (27 loc) · 900 Bytes
/
logger.ts
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
import winston, { createLogger, transports } from 'winston';
const { combine, timestamp, printf } = winston.format;
const myFormat = printf(({ level, message, timestamp, ...meta }) => {
const processedMeta = Object.fromEntries(
Object.entries(meta).map(([key, value]) => [
key,
value === undefined ? '<<UNDEFINED>>' : value,
]),
);
const metaString = Object.keys(processedMeta).length
? `,\n "data": ${JSON.stringify(processedMeta, null, 2).replace(/\n/g, '\n ')}`
: '';
return `{
"timestamp": "${timestamp}",
"level": "${level.toUpperCase()}",
"message": "${message}"${metaString}
}`;
});
const logger = createLogger({
level: 'info',
silent: process.env.NODE_ENV === 'production',
format: combine(timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), myFormat),
transports: new transports.File({
filename: 'app.log',
}),
});
export { logger };