-
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
3.0.0-rc1 - Unable to log timestamps #1175
Comments
No updates on this issue yet? I am facing same issue. Works on winston@2.4.0 but not on winston@3.0.0-rc1 |
Have a look at the formats you can use with const { createLogger, format, transports } = require('winston')
const logger = createLogger({
level: 'info',
transports: new transports.Console({
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD' // Optional for choosing your own timestamp format.
}),
format.json()
)
})
})
logger.info('Server started') The example prints out the following:
Or with the formatting option:
|
why |
I just want a simple at the beginning of line just as it was before, I don't want json formatting |
On version 2.3.1 I can achieve: With: const winston = require('winston');
const logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ timestamp: true, colorize: true}),
],
exceptionHandlers: [
new (winston.transports.Console)({ timestamp: true, colorize: true }),
],
level: 'INFO',
levels: {
FATAL: 0,
ERROR: 1,
WARN: 2,
INFO: 3,
DEBUG: 4
},
colors: {
INFO: 'blue',
WARN: 'green',
ERROR: 'yellow',
FATAL: 'red'
}
});
module.exports = logger |
I was hoping to use the "simple" line-of-text logging. I tried following the docs:
But that winds up mixing text and JSON like:
I was hoping to prefix each line with the timestamp, and for the timestamp to be printed alone. Something like:
but the comments above indicate that's only possible using the older style of passing options to the Any suggestions? Or am I asking the wrong question? That is, do log analyzers prefer JSON these days? TIA. |
Use the const { createLogger, format, transports } = require('winston')
const { colorize, combine, timestamp, printf } = format
// Define your custom format with printf.
const myFormat = printf(info => {
return `${info.timestamp} ${info.level}: ${info.message}`
})
const logger = createLogger({
format: combine(
timestamp(),
colorize(),
myFormat
),
transports: [new transports.Console()]
})
logger.info('foo') // -> 2018-02-21T03:55:19.236Z info: foo |
@ChrisAlderson you are the man, also if you want to display the splat I enhanced your answer with: const myFormat = printf(info => |
@ChrisAlderson, thanks so much! The documentation did not seem clear on this to me. Much appreciated! |
Thanks for helping folks out @ChrisAlderson. Contributions on documentation to improve explaining how formats work would be most welcome 👍 |
Note: be careful when using timestamp with metadata, apparently using both format functions leads to putting the time stamp as info.metadata.timestamp instead of info.timestamp |
In case someone needs to log additional metadata, including error's stack, the snippet below does the job (PS: example uses a simple output format). The snipped below targets @armadillojim expected output (described earlier ) and is a tinny improvement based on @ChrisAlderson's answer. As described by @OzzyTheGiant, when using timestamp with metadata, import winston from 'winston';
// Creates the default formatter.
// We deconstruct "metadata" into "timestamp" and "meta", and use them in the default formatter.
const logFormat = winston.format.printf((info) => {
const { timestamp, ...meta } = info.metadata;
return `${timestamp} ${info.level}: ${info.message} ${JSON.stringify(meta)}`;
});
// Creates the logger. "padLevels", "colorize" and "padLevels" are optional.
logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.padLevels(),
winston.format.metadata(),
winston.format.errors({ stack: true }),
logFormat,
winston.format.colorize({ all: true })
),
transports: [new winston.transports.Console()],
});
logger.info('Simple log');
logger.info('Logger Info', { logLevel: 'info', colorize: { all: true } });
logger.error('', new Error('Logger error')); Output: // 2019-05-11 00:20:53 info: Simple log {}
// 2019-05-11 00:20:53 info: Logger Info {"logLevel":"info","colorize":{"all":true}}
// 2019-05-11 00:20:53 error: Logger error {"stack":"Logger error\n at ... |
This is still the thing 2 year later. this works this doesn't for writing logs to file |
Apparently winston needs json() to be included after timestamp() for this to work: winstonjs/winston#1175 (comment)
I followed the example from the documentation, and created a logger as shown below:
But, I can seem to get the time stamps working. I get the following output in black and white:
Based on the documentation at https://github.com/winstonjs/winston/blob/master/docs/transports.md#console-transport, setting timestamp flag to true should put a default timestamp.
I even tried by returning a custom timestamp by passing a function but even that doesn't show up. Perhaps, I missed something but it seems like none of the options work, ie. colorize, showLevel, timestamp.
Or is the documentation out of sync?
The text was updated successfully, but these errors were encountered: