简体   繁体   中英

Setting a the same unique ID in Winston for 2 transports

I am using winston-js as a logger in my project - it works fine, however I'm now trying to add a unique log Id to each log line for debugging.

I have 2 transports - 1) Console, 2) File.

I'd like the log ID for the same log line to be the same in for both transports. At the moment, as the request goes through my system, the log ID stays the same.

In the example code below I'm using winston's defaultMeta - which doesn't seem to work, however i've tried adding functions too - and get the same result.

I do I set the log ID for the same log line to be the same in for both transports?

Note: I am using UUID for the LogId in my project - for simplicity I've used a single number in the examples.

So for example my current setup does:

File

logID: 1 | Request: Post | Request: 1
logID: 1 | Request: Post | Request: 1
logID: 2 | Request: Post | Request: 2

Console

logID: 1 | Request: Post | Request: 1
logID: 1 | Request: Post | Request: 1
logID: 2 | Request: Post | Request: 2

I'd like:

File

logID: 1 | Request: Post | Request: 1
logID: 2 | Request: Post | Request: 1
logID: 3 | Request: Post | Request: 2

Console

logID: 1 | Request: Post | Request: 1
logID: 2 | Request: Post | Request: 1
logID: 3 | Request: Post | Request: 2

.

function devLogger () {

    const logFormat = printf(({ level, message, timestamp, stack, ...meta }) => {
        var logId = meta.logId
        return `${timestamp} [${level}] ${message} | ${logId} | Console`;
    })

    const jsonFormat = printf(({ level, message, timestamp, stack, ...meta }) => {
        var logId = meta.logId
        return `${timestamp} [${level}] ${message} | ${logId} | File`;
    })

    return createLogger({
        defaultMeta: { logId: uuidv4() }, // Add logId to both transports
        transports: [
            new transports.Console({
                level: 'debug',
                format: combine(
                    format.colorize(),
                    timestamp({ format: 'DD-MM-YY HH:mm:ss' }),
                    logFormat
                ),
            }),
            new transports.File({
                level: 'debug',
                filename: 'error.log',
                format: combine(timestamp({ format: 'DD-MM-YY HH:mm:ss' }), errors({ stack: true }), jsonFormat),
            }),
        ]
    });
}

Fixed it with some straight forward variables.

First we set the LogId, the we reuse it, then we set it to a new ID ready for the next request.

This works as we run logFormat before jsonFormat

 function devLogger () {
    
        var setLogId = uuidv4()
    
        const logFormat = printf(({ level, message, timestamp, stack}) => {
            return `${timestamp} [${level}] ${message} | ${setLogId} | Console`;
        })
    
        const jsonFormat = printf(({ level, message, timestamp, stack}) => {
            var logId = setLogId
    
            setLogId = uuidv4()
            return `${timestamp} [${level}] ${message} | ${logId} | File`;
        })
    
    
    
        return createLogger({
            transports: [
                new transports.Console({
                    level: 'debug',
                    format: combine(
                        format.colorize(),
                        timestamp({ format: 'DD-MM-YY HH:mm:ss' }),
                        logFormat
                    ),
                }),
                new transports.File({
                    level: 'debug',
                    filename: 'error.log',
                    format: combine(timestamp({ format: 'DD-MM-YY HH:mm:ss' }), errors({ stack: true }), jsonFormat),
                }),
            ]
        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM