File size: 1,886 Bytes
22588bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const winston = require('winston');
const DailyRotateFile = require('winston-daily-rotate-file');
const path = require('path');

winston.addColors({
    error: 'red',
    warn: 'yellow',
    info: 'blue',
    debug: 'green'
});

const uppercaseLevelFormat = winston.format((info) => {
    info.level = info.level.toUpperCase();
    return info;
});

const LOG_LEVEL = process.env.LOG_LEVEL || 'info';
const LOG_FILE_PATH = path.join(__dirname, '../../log/application-%DATE%.log');
const MAX_FILES = process.env.LOG_INTERVAL_DELETION || '3d'; 

const log = winston.createLogger({
    level: LOG_LEVEL,
    format: winston.format.combine(
        uppercaseLevelFormat(),
        winston.format.colorize(),
        winston.format.timestamp(),
        winston.format.printf(({ level, message, timestamp }) => {
            return `${timestamp} ${level}: ${message}`;
        })
    ),
    transports: [
        new winston.transports.Console(),
        new DailyRotateFile({
            filename: LOG_FILE_PATH,
            datePattern: 'YYYY-MM-DD',
            maxSize: '20m',
            maxFiles: MAX_FILES
        })
    ]
});

log.exceptions.handle(
    new winston.transports.Console({
        format: winston.format.combine(
            uppercaseLevelFormat(),
            winston.format.colorize(),
            winston.format.timestamp(),
            winston.format.printf(({ level, message, timestamp }) => {
                return `${timestamp} ${level}: ${message}`;
            })
        )
    }),
    new DailyRotateFile({
        filename: path.join(__dirname, '../../log/exceptions-%DATE%.log'),
        datePattern: 'YYYY-MM-DD',
        maxSize: '20m',
        maxFiles: '30d'
    })
);

process.on('unhandledRejection', (reason, promise) => {
    throw reason;
});

log.on('error', function (err) {
    console.error('Error in logger:', err);
});

module.exports = log;