Home

Awesome

Log4JS - GELF appender

This is an optional appender for log4js-node.

npm install @log4js-node/gelf

The GELF appender supports sending log messages over UDP to a GELF compatible server such as Graylog. It uses node's core UDP support and does not require any other dependencies. If you use this appender, remember to call log4js.shutdown when your application terminates, so that all messages will have been sent to the server and the UDP socket can be closed. The appender supports passing custom fields to the server in both the config, and in individual log messages (see examples below).

Configuration

Example (default config)

log4js.configure({
  appenders: {
    gelf: { type: '@log4js-node/gelf' }
  },
  categories: {
    default: { appenders: ['gelf'], level: 'info' }
  }
});

This will send log messages to a server at localhost:12201.

Example (custom fields in config)

log4js.configure({
  appenders: {
    gelf: { type: '@log4js-node/gelf', host: 'gelf.server', customFields: { '_something': 'yep' } }
  },
  categories: {
    default: { appenders: ['gelf'], level: 'info' }
  }
});

This will result in all log messages having the custom field _something set to 'yep'.

Example (custom fields in log message)

log4js.configure({
  appenders: {
    gelf: { type: '@log4js-node/gelf', customFields: { '_thing': 'isathing' } }
  },
  categories: {
    default: { appenders: ['gelf'], level: 'info' }
  }
});
const logger = log4js.getLogger();
logger.error({ GELF: true, _thing2: 'alsoathing' }, 'oh no, something went wrong');

This will result in a log message with the custom fields _thing and _thing2. Note that log message custom fields will override config custom fields.