winston-graylog2 icon indicating copy to clipboard operation
winston-graylog2 copied to clipboard

Logging Objects or Errors is not working

Open Starfox64 opened this issue 7 years ago • 8 comments

'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.

Graylog

Starfox64 avatar Apr 22 '17 11:04 Starfox64

Hi @Starfox64 -- can you reliably reproduce the error? Would appreciate a PR if you don't mind :)

odino avatar Apr 22 '17 13:04 odino

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);

toleabivol avatar Sep 15 '17 11:09 toleabivol

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.

urbanslug avatar Mar 23 '18 14:03 urbanslug

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/

urbanslug avatar Mar 27 '18 05:03 urbanslug

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.

marjan2k avatar Mar 18 '19 18:03 marjan2k

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.

canozmen avatar Jan 02 '20 07:01 canozmen

Folks having these types of issues on more recent releases should check out setting Winston formatters and examples in the end-to-end tests.

jeremy-j-ackso avatar Feb 06 '20 20:02 jeremy-j-ackso

antone found a solution for this??

fetnimohamed avatar Aug 17 '21 09:08 fetnimohamed