Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to add timestamp to logged output? #906

Closed
vondiplo opened this issue Aug 6, 2016 · 5 comments
Closed

how to add timestamp to logged output? #906

vondiplo opened this issue Aug 6, 2016 · 5 comments

Comments

@vondiplo
Copy link

vondiplo commented Aug 6, 2016

No description provided.

@ssbrewster
Copy link

ssbrewster commented Aug 8, 2016

You can do it manually if you want fine-grained control over your output:

var moment = require('moment'),
      timestamp = moment().format('DD-MM-YYYY HH:mm:ss');

logger.error(timestamp + 'error string');

Or the easy (and probably more consistent way) is just to set timestamp: true in your transport:

new winston.transports.Console({
            level: 'debug',
            handleExceptions: true,
            json: false,
            colorize: true,
            timestamp: true
        })
    ]

I haven't tried it but winston-logentries the transport for logentries.com looks good.

@osukaa
Copy link

osukaa commented Aug 19, 2016

I normally create a custom logger

module.exports = new (winston.Logger)({
  level: 'info',
  handleExceptions: false,
  transports: [
    new (winston.transports.Console)({
      timestamp() {
        return moment().format('YYYY-MM-DD HH:mm:ss.SSSS');
      },
      formatter(params) {
        // Options object will be passed to the format function.
        // It's general properties are: timestamp, level, message, meta.
        const meta = params.meta !== undefined ? util.inspect(params.meta, { depth: null }) : '';
        return `[${params.timestamp}] [${params.level}] [${pkg.name}] *** ${params.message} ${meta}`;
      },
    }),
  ],
});

@attila123
Copy link

attila123 commented Apr 6, 2017

Thanks @osukaa . I realized I need to require() 'moment' (3rd party module) and 'util' (Node.js) module.
But where does pkg come from? It is not defined. I guess it is your own stuff.
There is also a typo in the code because params.timestamp function is not called, should be: params.timestamp().
Here is the metadata is logged a bit more differently:
https://github.com/winstonjs/winston/blob/master/README.md#user-content-custom-log-format
Did not take the time to understand both, so don't know which one is better, or what the point is to log metadata.

@NTShop
Copy link

NTShop commented May 29, 2017

const winston = require( 'winston' );
const tsFormat = () => ( new Date() ).toLocaleDateString() + ' - ' + ( new Date() ).toLocaleTimeString();
const logger = new (winston.Logger)({
	transports: [
		// colorize the output to the console
		new ( winston.transports.Console )({ 
			timestamp: tsFormat,
			colorize: true,
			level: env === 'development' ? 'verbose' : 'info',
		})
	]
});

@DABH
Copy link
Contributor

DABH commented May 26, 2018

In winston@3 this can be achieved using custom formatters (in fact there's an example in the docs now: https://github.com/winstonjs/winston/blob/master/examples/custom-timestamp.js). Related discussion in #1134 for example. Thanks!

@DABH DABH closed this as completed May 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants