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
Separate formatting from transports, make formats for transports customizable (was: Prefixless Logging) #135
Comments
Right now, formatting and transport are conflated. Separating formats and transports would take a refactor but it would certainly allow for your use case. I don't believe there's a ticket for this yet, so I'll use this issue for it. I hope you don't mind. |
+1 ... This is the last thing missing before 1.0 imho |
+1 Building a new application and in order to have both a console logger transport and file debug logger transport I have instantiate two separate loggers. |
Hi guys, I've started working on this feature and came up with an idea how to implement that. File & Console transports are using common.log function to generate log message string. It should quite easy to make that overridable by adding new option with formatter callback which will return log message as string. E.g. var winston = require('../lib/winston'),
common = require('../lib/winston/common');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
colorize: true,
timestamp: true,
formatter:function(options){
options.message = 'through formatter '+options.message;
return common.log(options); // of course we can use whatever we want
}
})
]
});
logger.log('info', 'Hello info log!', { 'foo': 'bar' });
logger.log('warn', 'Hello warn log!', { 'foo': 'bar' }); Current logger implementation doesn't allow creating more than on one logger with specified transport. I think we can add some kind of identifier to each logger so they can easily identifiable & we will be able more than one with specified transport. var winston = require('../lib/winston'),
common = require('../lib/winston/common');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.File)({
_id : 'first file transport'
filename: 'examples/test.log',
json: false,
formatter:function(options){
options.message = '1st formatter '+options.message;
return common.log(options);
}
}),
new (winston.transports.File)({
_id: 'second file transport',
filename: 'examples/test2.log',
json: false,
formatter:function(options){
options.message = '2nd formatter '+options.message;
return common.log(options);
}
})
]
});
logger.info('Hello info log!');
logger.warn('Hello warn log!'); At the end we should move common.log to separated object - e.g. BaseFormatter that will provide ways of formatting timestamps, levels. In code I've used common.log to generate log but developer can of course provide his own logic to do that :) |
I've started lumber, which seperates "transports" and "encoders" so any transport can use any encoder (potentially custom) to output it's data. I needed this feature before flatiron was willing to start implementing it so I just rolled my own. |
+1 |
2 similar comments
+1 |
👍 |
+1 The prefix, suffix idea would be awesome, for example creating on each request some hash and add the hash to log messages. This allows to but together the full flow in async world. |
+1 To fully custom format. I've added my own patch that accepts a function similar to @pkrefta implementation |
+1 |
Duplicate of #913. |
Sometimes I'd like to log without a prefix and handle all the line formatting myself. I don't think that's possible with winston as-is - am I missing something, or could it be a new feature?
The text was updated successfully, but these errors were encountered: