Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the winston log format?

In my node application I'm using winston module to store my application logs. We can store the the logs in two format one is json and the other one is string. While saving the log as string in winston I'm getting below log format.

  2013-09-10T06:51:34.199Z - error: error message!!!
       (timestamp)     -    (level) : (log message)

Now I want change the above log format to the following:

    2013-09-10T06:51:34.199Z/error/error message!!!
       (timestamp)    /     (level) / (log message)

How can this be achieved?

My Code:

  var winston = require('winston');
  winston.loggers.add('category1', {
   file: {
      filename: '/path/to/some/file',json:false
     }
  });              
  var category1 = winston.loggers.get('category1');
  category1.log('error','error message!!!');
like image 711
sachin Avatar asked Sep 10 '13 07:09

sachin


People also ask

How do I set up a Winston logger?

Here's a guide that will help you understand NPM in detail. Install the Winston package using the command npm install winston . Logging with Winston is simple, with just four steps, as shown in the example below, and you have your log recorded. Add the Winston module with a require() function.

Which method is used to create your own logger with Winston?

Creating the Custom Logger classcreateLogger() method. If you wish to completely change the format of the logs you could refer to Winston docs.

What is Winston logs?

winston is designed to be a simple and universal logging library with support for multiple transports. A transport is essentially a storage device for your logs. Each winston logger can have multiple transports (see: Transports) configured at different levels (see: Logging levels).

What is Winston logger in node JS?

Winston is the most popular logging library for Node. js. It aims to make logging more flexible and extensible by decoupling different aspects such as log levels, formatting, and storage so that each API is independent and many combinations are supported. It also uses Node.


1 Answers

I was wondering the same thing and found an okay solution (though not ideal IMO, so perhaps someone else can weigh in on this).

You can completely customize the logger output by providing your transport object a formatter function. This might be better for File transports than Console, since you would have to manually colorize your font if that's what you wanted.

Here is a relatively simple formatter function that you can use (and adjust for your needs):

// Define options for Date#toLocaleTimeString call we will use.
var twoDigit = '2-digit';
var options = {
  day: twoDigit,
  month: twoDigit,
  year: twoDigit,
  hour: twoDigit,
  minute: twoDigit,
  second: twoDigit
};

function formatter(args) {
  var dateTimeComponents = new Date().toLocaleTimeString('en-us', options).split(',');
  var logMessage = dateTimeComponents[0] + dateTimeComponents[1] + ' - ' + args.level + ': ' + args.message;
  return logMessage;
}

And to use this formatter in your transport, simply adjust your code to pass the function in:

winston.loggers.add('category1', {
  file: {
    filename: '/path/to/some/file',
    json: false,
    formatter: formatter
  }
});

It's worth mentioning that the property args.meta will be set to any object argument that is passed into a log method call. So you would have to come up with a strategy for handling those objects passed in (or simply print the entire object as JSON). For example:

var error = {
  name: 'MongoError',
  code: 11000,
  err: 'insertDocument :: caused by :: 11000 E11000 duplicate key error...'
}
logger.info('Some error ocurred: ', error);

Would result in args.meta being set to the error variable.

As you can see, there is a fair amount to deal with when handling log messages this way. I wouldn't be surprised if there was a better way of doing these things, but hopefully this helps you (or someone else) out.

like image 162
Default Avatar answered Oct 19 '22 01:10

Default