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

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

Closed
polm opened this issue May 25, 2012 · 12 comments

Comments

@polm
Copy link

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?

@jfhbrook
Copy link
Contributor

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.

@indexzero
Copy link
Member

+1 ... This is the last thing missing before 1.0 imho

@englercj
Copy link

+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
Copy link

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 :)

@englercj
Copy link

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.

@jasonkuhrt
Copy link

+1

2 similar comments
@barbogast
Copy link

+1

@mbleigh
Copy link

mbleigh commented Oct 11, 2013

👍

@riston
Copy link

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
Copy link

kelaban commented Jun 4, 2014

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

@marvinmartian
Copy link

+1

@indexzero
Copy link
Member

Duplicate of #913.

@indexzero indexzero added Duplicate and removed Feature Request Request for new functionality to support use cases not already covered labels Jun 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests