How to set different meta for different transports in winston?

Viewed 112

I am trying to implement logging, for my node.js application, that uses socket.io for connections. My initial goal was to implement a logging mechanism, that stores logs in two different files, based on the authenticated Account and the socket.io connection id, so I may quickly review logs related to a specific account, and/or to a specific session when the Account was active.

For this, I've created a logger, that uses two File type transports, like this:

/** ... some constant declarations ommited for brevity **/
const customerTransport = new winston.transports.File({
    dirname: customerSessionDir,
    filename: 'main.log',
    level: 'info',
});

const sessionTransport = new winston.transports.File({
    dirname: path.resolve(customerSessionDir, 'sessions'),
    filename: moment.now().toString() + "." + socketSessionId + ".log",
    level: 'info'
});

const logger = winston.createLogger({
    levels: winston.config.syslog.levels,
    level: 'info',
    format: loggingFormat,
    transports: [customerTransport, sessionTransport]
});

This works as expected, but now, I would want to back reference, the socketSessionId variable, in the main.log file, so that when I am reviewing the log for a specific account, I get a reference in that file, that points to a smaller session log file. I know that this is achiavable, by setting the defaultMeta property, when creating the logger, but if I do it like that, the information will also appear in the session log files, and I would not want to bloat those files with any unneccessary information.

Is it possible somehow, to automagically add meta data to specific transports only? Is this the intended way to achieve my end-goal? Or should I create two separate loggers, and use that for this scenario?

0 Answers
Related