Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

winston:how to change timestamp format

I am using winston to add log details in node.js, i used the following procedure to add the logs

 var winston = require('winston');           winston.remove(winston.transports.Console);  winston.add(winston.transports.Console, {'timestamp':true,'colorize':true);  winston.log('info','jjjj'); 

the output that i got is

2012-12-21T09:32:05.428Z - info: jjjj 

I need to specify a format for mytimestamp , is there any provision to do so in winston any help will be much appreciated

like image 894
Amanda G Avatar asked Dec 21 '12 09:12

Amanda G


People also ask

How do I add a timestamp in Winston?

When you include Winston, it usually defaults to adding a Console transport. In order to get timestamps to work in this default case, I needed to either: Remove the console transport and add again with the timestamp option. Create your own Logger object with the timestamp option set to true.

What is the format of timestamp?

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision.

What is Winston logger?

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


2 Answers

The timestamp option can be a function that returns what you wish it to be saved as...

Line 4:

winston.add(winston.transports.Console, {'timestamp':function() {return '111111111'; },'colorize':true}); 

Source here: https://github.com/flatiron/winston/pull/120

like image 82
Ben Evans Avatar answered Sep 21 '22 06:09

Ben Evans


winston@3 version

winston.createLogger({   format: winston.format.combine(     winston.format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),     winston.format.prettyPrint()   ),   transports: [     new winston.transports.Console()   ] }) 

To support timezone, you need to change format to a function which winston will call.

const timezoned = () => {   return new Date().toLocaleString('en-US', {     timeZone: 'Asia/Shanghai'   }); };  const logger = createLogger({   format: combine(     timestamp({       format: timezonedTime     })   ),   transport: [     new transports.Console(),   ] }); 
like image 40
Chris Peng Avatar answered Sep 21 '22 06:09

Chris Peng