node.jsloggingwinston

Winston DailyRotateFile writes escape characters in log


I have set up logging with winston and want to write log files with dailyRotateFile from winston. Logging to the console works without problems but logging the exact same to the file does kinda work but it does include all the escape characters somehow.

const winstonLogFormat = winston.format.combine(
    winston.format.colorize({ all: true }),
    winston.format.timestamp({
        format: 'YYYY-MM-DD HH:mm:ss.SSS',
    }),
    winston.format.align(),
    winston.format.printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
)

const fileRotateTransport = new winston.transports.DailyRotateFile({
    filename: '%DATE%-app-log.log',
    dirname: 'logs',
    datePattern: 'YYYY-MM-DD',
    maxFiles: '28d',
    zippedArchive: true,
    json: false,
});

const logger = winston.createLogger({
    level: logLevel,
    format: winstonLogFormat,
    transports: [
        fileRotateTransport,
        new winston.transports.Console()
    ]
});

I do log with logger.info('Connected to: ' + target); and get this in the logfile:

[2024-01-11 10:50:46.160] [32minfo[39m:     [32mConnected to: Home[39m

(Before every [ Notepad++ displayed an ESC in black)

The console will just show me the correct log without any strange characters:

[2024-01-11 10:50:46.160] info:     Connected to: Home

Any idea on how to fix this issue?


Edit: Found out that it has something to do with the colors. When removing winston.format.colorize({ all: true }) from the log format the console will not have any color BUT the log will be without errors.

Any idea on how to remove the color just in the log files? I tryed to add a new log format to the DailyRotateFile but without luck:

const winstonLogFormat = winston.format.combine(
    winston.format.colorize({ all: true }),
    winston.format.timestamp({
        format: 'YYYY-MM-DD HH:mm:ss.SSS',
    }),
    winston.format.align(),
    winston.format.printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
)

const winstonLogFormatForFile = winston.format.combine(
    winston.format.timestamp({
        format: 'YYYY-MM-DD HH:mm:ss.SSS',
    }),
    winston.format.align(),
    winston.format.printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
)

const fileRotateTransport = new winston.transports.DailyRotateFile({
    filename: '%DATE%-app-log.log',
    dirname: 'logs',
    datePattern: 'YYYY-MM-DD',
    maxFiles: '28d',
    zippedArchive: true,
    json: false,
    format: winstonLogFormatForFile
});

const logger = winston.createLogger({
    level: logLevel,
    format: winstonLogFormat,
    transports: [
        fileRotateTransport,
        new winston.transports.Console()
    ]
});

Solution

  • ?[32 and ?[39 are codes to make text a specific color in the console. Turns out you can give the winston.transports.Console() its own format so that you don't need to provide a global format with the color so the logging to file wont have any color in it:

    const winstonLogFormatForFile = winston.format.combine(
        winston.format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss.SSS',
        }),
        winston.format.align(),
        winston.format.printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
    )
    
    const fileRotateTransport = new winston.transports.DailyRotateFile({
        filename: '%DATE%-app-log.log',
        dirname: 'logs',
        datePattern: 'YYYY-MM-DD',
        maxFiles: '28d',
        zippedArchive: true,
        json: false,
        format: winstonLogFormatForFile
    });
    
    const consoleTransport = new winston.transports.Console({
        format: winston.format.combine(
            winston.format.colorize({ all: true }),
            winston.format.timestamp({
                format: 'YYYY-MM-DD HH:mm:ss.SSS',
            }),
            winston.format.align(),
            winston.format.printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
        )
    });
    
    const logger = winston.createLogger({
        level: logLevel,
        transports: [
            fileRotateTransport,
            consoleTransport
        ]
    });