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
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.
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.
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).
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
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(), ] });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With