0
votes

I am attempting to use multiple transports in Winston. This is working...sorta. I have a transport set up for the app's audit log, with a custom level of 'audit', and a transport for 'info'.

var winston_mongo_options = {
    level: 'audit', // level that should be logged
    safe: true, //makes sure writes happen before firing log event
    db: config.db.db,   // db in which to write logs
    host: config.db.host,
    port: config.db.port,
    collection: config.db.audit_collection  // collection we want logging to occur
};
if (config.db.user) {
  winston_mongo_options.username = config.db.user;
  winston_mongo_options.password = config.db.pass;
}

var custom_levels = winston.config.syslog.levels;
custom_levels.audit = 8;
var logger = new (winston.Logger)({
    levels: custom_levels,
    transports : [
        new (winston.transports.MongoDB)(winston_mongo_options),
        new (winston.transports.File)({
            level: 'info',
            silent: false,
            colorize: true,
            timestamp: true,
            filename: config.logs.debug,
            maxsize: 500000,
            maxFiles: 5,
            json: true
        })
    ],
    exceptionHandlers: [ 
        new (winston.transports.File)({
            silent: false,
            colorize: false,
            timestamp: true,
            filename: config.logs.exception,
            maxsize: 500000,
            maxFiles: 5,
            json: true
        })
    ]
});

module.exports.logger = logger;

Obviously, I'm requiring this file where/when I want to do any logging. The problem comes in when wanting to send certain information separately to the logs.

logger.audit('Server Started - to DB');
logger.info('Server Started - to info');

These two lines should write to separate logs. The first line gets properly written to the database AND to the info log file. What am I doing wrong?

1

1 Answers

2
votes

SOLVED: The problem was how I am defining the levels. I didn't realize that the way Winston logging works, is a log will receive everything >= the level defined. So my 'info' transport being a level 0 was receiving messages sent to the 'audit' transport, which was a level 8. I set 'audit' to level 0, and it stopped showing up in the 'info' log. I then found a better solution by creation a whole new logger just for the audit database log.