Winston: Custom Log Format for Files doesn't seem to work

Created on 2 Apr 2015  ·  4Comments  ·  Source: winstonjs/winston

Hi,

i used your formatter function to supply a custom format to my log messages. It works perfect for the transport.Console but the transport.File seams to ignore the custom formatter.

heres my custom file formatter:

function customFileFormatter (options) {
    // Return string will be passed to logger.
    return options.timestamp() +' ['+ options.level.toUpperCase() +'] '+ (undefined !== options.message ? options.message : '') +
     (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
}

its essential the logger given in your example. I apply it like this:

winston.add(winston.transports.File, { 
    name: 'info-file',
    filename: logPath + '/info.log',
    level: 'info',
    maxsize: 15000000,
    formatter: customFileFormatter
});

but the output in the logging file stays the same:
{"level":"info","message":"INITIALISEING","timestamp":"2015-04-02T06:29:57.982Z"}
it should be:
{"level":"[INFO]","message":"INITIALISEING","timestamp":"2015-04-02T06:29:57.982Z"}

I don't know whats wrong because the same works with the transport.Console.

please help

greetings

Tonacate

Most helpful comment

OK never mind.
forgot to set
json: false

but it would be cool if you could change just some fields in the json logging :)

All 4 comments

OK never mind.
forgot to set
json: false

but it would be cool if you could change just some fields in the json logging :)

Took me a bit to figure out where to set "json: false". Set it on the file transport(s). Here's my example:

const logFormatter = function(options) {
    // Return string will be passed to logger.

    return options.timestamp() +` `+ options.level.toUpperCase() +
        ` `+ (options.message ? options.message : ``) +
        (options.meta && Object.keys(options.meta).length ?
            `\n\t`+ JSON.stringify(options.meta) : `` );
};

const timestamp = function() {
    const d = new Date();
    return d.getHours() + `:` + d.getMinutes() + `:` +
        d.getSeconds() + `m` + d.getMilliseconds();
};

const logger = new (winston.Logger)({
    transports: [
        new (winston.transports.File)({
            timestamp: timestamp,
            formatter: logFormatter,
            level: `silly`,
            name: `fileAll`,
            filename: `fileAll.log`,
            json: false,                                 // set json:false on the file transport(s)
        })
    ]
});

formatter works fine for me but timestamp always prints a literal string

function () {
const d = new Date();
return d.getHours() + : + d.getMinutes() + : +
d.getSeconds() + m + d.getMilliseconds();
}|DEBUG|Message for an Debug
function () {
const d = new Date();
return d.getHours() + : + d.getMinutes() + : +
d.getSeconds() + m + d.getMilliseconds();
}|EMERGENCY|Message for an Debug

OK never mind.
forgot to set
json: false

but it would be cool if you could change just some fields in the json logging :)

You saved my time! I wonder where is this documented..

Was this page helpful?
0 / 5 - 0 ratings