-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
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 What am I doing incorrectly? |
Hey @indexzero, I'm on winston 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: const imLogFormat = printf(info => { you can do the following: logger.error('I have the following error' + err, {label: "Class1"}) |
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
I basically want all my logs into a single file/console, but with a different prefix. Any ideas?
The text was updated successfully, but these errors were encountered: