Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharing same transport with different labels #736

Closed
Nepoxx opened this issue Oct 30, 2015 · 4 comments
Closed

Sharing same transport with different labels #736

Nepoxx opened this issue Oct 30, 2015 · 4 comments

Comments

@Nepoxx
Copy link

Nepoxx commented Oct 30, 2015

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?

@indexzero
Copy link
Member

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

@Nepoxx
Copy link
Author

Nepoxx commented Nov 2, 2015

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?

@rheaditi
Copy link

rheaditi commented Aug 11, 2018

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"}

@Pabylon
Copy link

Pabylon commented Jan 3, 2019

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"})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants