简体   繁体   中英

Winston MongoDB: How to pipe logs to many collections, instead all goes to one collection

How to make winston-mongodb logs to different collections at the same time

var winston = require('winston');
require('winston-mongodb').MongoDB;


var logger = new winston.Logger({
  level: 'info',
  transports: [
    new(winston.transports.MongoDB)({
            db : 'mongodb://xxxx'
        })
  ],
  capped : true,  // defaults to false
})

This one logs everything to the default collection log , but I need to make a collection to each module, so I know where to investigate certain problem for a specific domain

Simple and straight forward, we can channel logs to as many collections as we want:

winston.loggers.add('mongoLog',{
    transports : [
        new(winston.transports.MongoDB)({
            db : 'mongodb://xxxxx',',
            collection : 'collection1',
            level : 'info',
            capped : true
        }),
    ]
});

var mongoLog = winston.loggers.get('mongoLog')
mongoLog.info('hello') 


winston.loggers.add('profileLog',{
    transports : [
        new(winston.transports.MongoDB)({
            db : 'mongodb://xxxxx',
            collection : 'collection2',
            level : 'info',
            capped : true
        }),
    ]
});

var profileLog = winston.loggers.get('profileLog')
profileLog.error('user profile is not valid')

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