-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 customize timestamp format? #1134
Comments
you can read the source code to help yourself customize the format. i can give you an example of mine.
|
This is so extremly complicated for the simplest requirement of just printing a timestamp (which should be default anyway) :/ |
Here's what I do... //logger.js const winston = require('winston');
const moment = require('moment');
// create formatter for dates used as timestamps
//const tsFormat = () => (new Date()).toLocaleTimeString();
const tsFormat = () => moment().format('YYYY-MM-DD hh:mm:ss').trim();
// define a logger with 2 transports - console and a file
const logger = new (winston.Logger)({
transports: [
// colorize the output to the console
new (winston.transports.Console)({
timestamp: tsFormat,
colorize: true
}),
new winston.transports.File({
filename: './logs/ttracker.log',
timestamp: tsFormat, // makes timestamp 'pretty'
json: false // makes log format just like console output
})
]
});
// set logging level one of { error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5 }
logger.level = 'debug';
module.exports = logger; |
P.S. I only set timestamp: to a function because I don't like the default format which you get by setting timestamp: true, i.e. 2017-12-05T08:22:09.179Z |
@jimwhurr - for me, your code (node v6.11.0) produces:
|
Taking the examples here, I created a simpler standalone example: https://gist.github.com/a-h/d02bd4ff238e5923fcf5369233e51401 const winston = require('winston');
const MESSAGE = Symbol.for('message');
const jsonFormatter = (logEntry) => {
const base = { timestamp: new Date() };
const json = Object.assign(base, logEntry)
logEntry[MESSAGE] = JSON.stringify(json);
return logEntry;
}
const logger = winston.createLogger({
level: 'info',
format: winston.format(jsonFormatter)(),
transports: new winston.transports.Console(),
});
logger.info('message content', { "context": "index.js", "metric": 1 })
logger.info('message content 2') Output:
|
Hi, here is another option for logging with timestamp (tested on 3.0.0-rc0):
And for log:
May be it will be helpful. |
you can customize the timestamp with a format string:
|
This should be on the docs. @felipemullen Thank you! |
@felipemullen thanks for providing that sample. Agree with @ricardoaat this should be in the docs, so ... now it is: examples/custom-timestamp.js |
@youngkylejan oh oh.....That's good help...
|
I'm new to Winston and, gotta say, the docs are surprisingly hard to follow :( Somehow, I ended up with the following apparently simpler solution: winston.createLogger({
level: ...
format: winston.format.printf(info => `${new Date().toISOString()} ${info.message}`),
transports: ...
}); |
@hosseinGanjyar |
This worked for me. Setup:
|
This is all for winston 3.0.0 and @types/winston 2.3.9
this returns a timestamp that is just the time in the current timezone.
then
probably doesn't need level twice. but that's what I'm using currently. |
Life saver. Many thanks!!! |
For those who are here to find out how to customize format string besides |
…ly as an example. Fixes winstonjs#1134.
All those examples remove rest parameters in logs. If you want to use format.simple() and only add timestamp to the beginning and remove from rest, this might works for you: const simpleFormat = format.simple()
const MESSAGE = Symbol.for('message')
const simpleTimestamp = format(info => {
const { timestamp, ...rest } = info
const simpled = simpleFormat.transform(rest)
if (typeof simpled !== 'boolean') {
// @ts-ignore
simpled[MESSAGE] = `${timestamp} ${simpled[MESSAGE]}`
}
return simpled
})
logger.add(new transports.Console({
format: format.combine(
format.timestamp(),
format.colorize(),
simpleTimestamp(),
),
})) |
I'm using 3.0.0-rc1 and the docs aren't clear on how to customize the timestamp's format. Please help.
Thanks,
Alvaro
The text was updated successfully, but these errors were encountered: