winston-graylog2
winston-graylog2 copied to clipboard
Logging Objects or Errors is not working
'use strict';
const winston = require('winston');
const Graylog = require('winston-graylog2');
const logger = new winston.Logger({
level: 'debug',
transports: [
new Graylog({
level: 'debug',
handleExceptions: true,
graylog: {
servers: [{ host: '10.42.221.139', port: 12201 }],
facility: 'debug-test'
}
})
]
});
logger.info('Text Test');
logger.debug({foo: 'bar'});
logger.error(new Error());
logger.error(new Error('Test Error'));
setTimeout(() => {process.exit(0)}, 1000);
When running the following script, the first test is the only one inserted in Graylog.
Hi @Starfox64 -- can you reliably reproduce the error? Would appreciate a PR if you don't mind :)
I found out I have to do it with 2 parameters in order to get it working:
throw new Error('foo');
...
winston.error(error.toString(), error);
Logging objects
I'm also facing the same issue when logging objects. In my case the console transport and file transports can both handle a JS object as the message instead of a string. e.g logger.log('info', {message: 'Something', 'other': 'stuff'})
However for winston-graylog2 I have to set prelog: JSON.stringify
for it to handle an object as the message but this still loses value of the message key.
So @Starfox64 you could set the option prelog: JSON.stringify
but it still won't log anything under the message value.
Update
It does work but one has to pass an object as an extra argument to the log function e.g logger.log('info', 'the message', {more: 'stuff'})
.
However, given that other transports such as console and file still work when the message is in the object; it would be nice if this transport also supported this functionality.
Edit: What I am referring to is inspired by this series on strucutred logging https://honeycomb.io/blog/categories/logging/
Hello, this issue is still persisting as of 2.1.2.
Since we are relying on multiple transports, its a bit of a hassle to add objects via extra parameter only for graylog.
Hello, this issue is still persisting as of 2.1.2.
Since we are relying on multiple transports, its a bit of a hassle to add objects via extra parameter only for graylog.
Do you have a working config you can share for this. I still can't send separate fields even if I add extra parameters as an object as the last parameter.
I can only log facility, full_message, level, message, source, timestamp fields.
Folks having these types of issues on more recent releases should check out setting Winston formatters and examples in the end-to-end tests.
antone found a solution for this??