Winston: Transport Formatter not called/used.

Created on 30 Jan 2015  路  11Comments  路  Source: winstonjs/winston

This might be more of a question than an 'issue'. I am trying to use a custom formatter with a logging transport. It seems that the formatter function is ignored in my simple test. I've taken and slightly modified the example from the README:

'use strict';
var winston = require('winston');
var logger = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)({
            timestamp: function() {
                return Date.now();
            },
            formatter: function(options) {
                // Return string will be passed to logger.
                return 'MEOW!';
            }
        })
    ]
});
logger.info('Data to log.');

I am expecting the output (to stdout in this case) to be: _"MEOW"_
Instead the output is:_"1422632922024 - info: Data to log."_
Which, I believe, is the default. Am I missing something?

Thanks!

Most helpful comment

Just ran into this and came up with a work around that worked out for me.

var logger = new winston.Logger({
    transports: [
        new (winston.transports.File)({
            filename: path,
            json: true,
            stringify: function (options) {
                return  <formatted string>;
            }
        })
    ]
});

All 11 comments

Just ran into the same thing myself. You need to install it from the master branch, ie:

npm install git+https://github.com/flatiron/winston.git

This is in [email protected] which should land this week.

@indexzero I see this working for the Console transport but it seems to be ignoring it for the File transport...

module.export = logger = new (winston.Logger)({
exitOnError: true,
transports: [
    new (winston.transports.File)({
        filename:'./logs/app-logs.log',
        formatter: function(options) {
            return "MEOW!";
        }
    })
] });

outputs normal file log format

I have the same issue, working for console but not with file transport. my version is 0.9.0

@indexzero
My version is 1.0.0, formater still does not work with file transport.

@Airswoop1 @dinushadck @cuongle121 I don't see why it wouldn't. The formatter is set in the Constructor function and then consumed in the File transport

... if you could track down the bug a PR would be welcome. Commiting a PR with a failing test would also help immensely.

I am having similar issues but for me it doesn't work with the console appender. I haven't dug into the code yet but I noticed that this setting

json: true

...caused Winston to ignore the custom formatter. If I remove that line then my custom formatter is called.

@mlconnor removing json: true not working for me here..

I ran into this today. Any update on this issue?

@calebbrewer try this?

logger = new (winston.Logger)({
  transports: [
    new (winston.transports.File)({
      json: false,
      filename: path,
      formatter: (options) ->
        options.message
    })
  ]
})

Just ran into this and came up with a work around that worked out for me.

var logger = new winston.Logger({
    transports: [
        new (winston.transports.File)({
            filename: path,
            json: true,
            stringify: function (options) {
                return  <formatted string>;
            }
        })
    ]
});
Was this page helpful?
0 / 5 - 0 ratings