Winston: Sharing same transport with different labels

Created on 30 Oct 2015  ·  4Comments  ·  Source: winstonjs/winston

I would like all my logs to have a prefix representing where the log came from. This way creates multiple transport for the same file, which is obviously an

var config = require('config')
var winston = require('winston')

var loggers = {}


function getLogger(moduleName) {
  if (!loggers[moduleName]) {
    loggers[moduleName] = createNewLogger(moduleName)
  }

  return loggers[moduleName]
}

function createNewLogger(moduleName) {
  var logger = new winston.Logger()

  //https://github.com/winstonjs/winston/blob/master/docs/transports.md#console-transport
  logger.add(winston.transports.Console,
    {
      colorize: true,
      timestamp: true,
      label: moduleName,
      level: config.get('log.level')
    }
  )

  logger.add(winston.transports.DailyRotateFile,
    {
      filename: config.get('log.filename'),
      level: config.get('log.level')
    }
  )

  return logger
}

module.exports = getLogger

I basically want all my logs into a single file/console, but with a different prefix. Any ideas?

All 4 comments

I believe you want this: https://github.com/winstonjs/winston#working-with-multiple-loggers-in-winston

Thanks for this! I saw it before, but I was under the impression that the transports were being recreated for each logger.

For instance the following code:

function getLogger(moduleName) {
  console.log('Initializing logger for: ' + moduleName)
  winston.loggers.add(moduleName, {
    //https://github.com/winstonjs/winston/blob/master/docs/transports.md#console-transport
    Console: {
      colorize: true,
      timestamp: true,
      label: moduleName,
      level: config.get('log.level')
    },
    DailyRotateFile: {
      filename: config.get('log.filename'),
      level: config.get('log.level')
    }
  })

  return winston.loggers.get(moduleName)

}

Will give Error: Transport already attached: console

What am I doing incorrectly?

Hey @indexzero,

I'm on winston 3.0.0.

I tried this too, unsure if this is the right way; basically I want an easier way to prefix each log message or attach a default field for each log based on the module / function it is being logged from (for some structure). This is the code (going by the example in the docs):

const loggers = {};
module.exports.getModuleLogger = (moduleName, methodName) => {
    const categoryName = [moduleName, methodName].join(':');
    if (!loggers[categoryName]) {
      loggers[categoryName] = winston.loggers.add(categoryName, {
        console: {
          label: categoryName,
        },
        file: {
          filename: './log/test.log',
          label: categoryName,
        },
      });
    }
    return winston.loggers.get(categoryName);
  };

But I get the following warning:

[winston] Attempt to write logs with no transports {"message":"yolo","
level":"info"}

you can achieve this by adding an object witch contains the field "label" in the parameters of you log function, example:
for the following format:

const imLogFormat = printf(info => {
return ${info.timestamp} [${info.label}] ${info.level}: ${info.message};
});

you can do the following:

logger.error('I have the following error' + err, {label: "Class1"})
logger.error('I have the following error' + err, {label: "Class2"})

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sinai-doron picture sinai-doron  ·  3Comments

jlank picture jlank  ·  4Comments

KingRial picture KingRial  ·  3Comments

Buzut picture Buzut  ·  3Comments

greenhat616 picture greenhat616  ·  3Comments