Separate formatting from transports, make formats for transports customizable (was: Prefixless Logging) #135

Open
polm opened this Issue May 25, 2012 · 11 comments

Comments

Projects
None yet

polm commented May 25, 2012

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?

Contributor

jfhbrook commented May 25, 2012

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.

Owner

indexzero commented May 25, 2012

+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.

pkrefta commented Jul 10, 2012

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.

Koblaid commented Jul 18, 2013

+1

mbleigh commented Oct 11, 2013

👍

riston commented Oct 14, 2013

+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.

kelaban commented Jun 4, 2014

+1 To fully custom format. I've added my own patch that accepts a function similar to @pkrefta implementation

This was referenced Oct 29, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment