Search code examples
javascriptnode.jsloggingwinston

winston full custom file output


EDIT : Found the answer, and posted a complete answer for future readers.


I have grasped the basics of winston logging in Node.js, and wish to customize my logging a bit more. Here is what I have so far :

var winston = require('winston');         

winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {'colorize': true});
winston.add(winston.transports.File,
  {
    'filename': 'testlog.log',
    'timestamp': function() {return Date.now();},
  }
);

// let's say I only want to log 'info' or 'error'
function loginfo(arg) {return winston.info(arg);}
function logerror(arg) {return winston.error(arg);}

The output file will look like this :

{"level":"info","message":"someinfo","timestamp":1470515515807}
{"level":"error","message":"someerror","timestamp":1470515515808}

I understand the fact that a JSON format standard logging is important, and therefore I want to keep the almost standard file transport as it is.

But since I'm actually reading every line of those logs, I would like to open a third transport to another file, with a completely customized formatting for my eyes only, such as :

1470515515807 info "someinfo"
1470515515808 error "someerror"
1470515519532 error HERE_GOES_SOME_COMPLEX_ERROR_MESSAGE_OR_OBJECT_
I_WANT_TO_SEE_AS_PRETTY_POSSIBLE_DESPITE_THE_FACT_IT_IS_VERY_COMPLEX

I have made some research and it seems there is no such functionnality directly available in winston. I have noticed the fancy bunyan CLI tool but what I want "seems" much more simple.

Questions :

1) Is there no built-in custom output formatting in logging libraries which would meet my requirements ? [Can't believe it]

2) If I have to hard-code it myself, could you give me some directions (best practice to process stdout the way I want without overloading the app itself), especially when it comes to complex objects returned by some errors ?


Solution

  • [Answering my own question]

    Winston has a custom formatter options which is simple and efficient, with an example provided at the very bottom of the GitHub readme.

    With my example, a minimal code which doesn't check for each parameter presence would be :

    var log = new (winston.Logger)({
    
        transports: [
    
            new (winston.transports.File)({
                name: 'info_normal',
                filename: './log_info.log',
                level: 'info',
                timestamp: function(){return Date.now();}
            }),
    
            new (winston.transports.File)({
                name: 'info_custom',
                filename: './log_info_custom.log',
                level: 'info',
                timestamp: function(){return Date.now();},
                formatter: function(options) {
                    // Returned string will be passed to the logger
                    return options.timestamp() + ' ' + options.level + ' ' + 
                    options.message + ' ' + JSON.stringify(options.meta);
                },
                json: false //don't forget it !!!!!!!!!!!
            })
        ]
    });

    From there, improving the customization is quite easy. Chill, Winston !