Multiple loggers with same file transporter but with different labels
See original GitHub issueHello together,
I have several modules (call them module A
and module B
) and they should share the same file transporter because I only want one single log file e.g. called somefile.log
. Writing the logs to a file is my only transporter. Now I want the content of my log file to look like this:
2017-05-03T06:44:15.300Z - info: [module A] foo
2017-05-03T06:44:15.300Z - info: [module B] bar
I don’t want the JSON style here. Can you please tell me how I can achieve this?
When I define the file transport for each logger but set the filename in both transporters to the same string I get an error that the transport is already defined and I should change the file name:
winston.loggers.add('module A', { file: {
filename: 'somefile.log',
level: 'silly',
label: 'module A',
maxsize: 8,
maxFiles: 3,
json: false,
prettyPrint: true,
tailable: true,
zippedArchive: true,
handleExceptions: true,
humanReadableUnhandledException: true,
exitOnError: false,
} });
winston.loggers.add('module B', { file: {
filename: 'somefile.log',
level: 'silly',
label: 'module B',
maxsize: 8,
maxFiles: 3,
json: false,
prettyPrint: true,
tailable: true,
zippedArchive: true,
handleExceptions: true,
humanReadableUnhandledException: true,
exitOnError: false,
} });
const moduleAlogger = winston.loggers.get('module A');
const moduleBlogger = winston.loggers.get('module B');
moduleAlogger.info('foo');
moduleBlogger.info('bar');
results in:
2017-05-03T06:49:30.641Z - error: uncaughtException: Transport already attached: file, assign a different name
I didn’t understand the idea behind the labels per transporter because this would be redundant information when I am not allowed to write to the same file with different labels.
e.g. when I have two file transporters to different files I have in one file
2017-05-03T06:44:15.300Z - info: [module A] foo
2017-05-03T06:44:15.300Z - info: [module A] bar
.....
and in the other
2017-05-03T06:44:15.300Z - info: [module B] foo
2017-05-03T06:44:15.300Z - info: [module B] bar
.....
The label is totally useless here. What haven’t I understood when using labels in file transporters?
Thanks for your help.
Stefan
Issue Analytics
- State:
- Created 6 years ago
- Reactions:25
- Comments:5
Top GitHub Comments
So I see it is possible, but does winton handle well all the low-level details of multiple file handles writing to the same file? Does it sync to write calls, and make sure data is not overridden?
Here is the solution I am using with
v3.1.0
. You can add the label format to the logger, and everything else you need to the transport.In TypeScript:
So you can pretty easily use this in all your different modules: