Skip to content

namshi/winston-graylog2

Repository files navigation

Build Status

winston-graylog2

A graylog2 transport for winston based on the node-graylog2 Library

Installation

Recently rewritten to use ES6 for better compatibility with Winston@3, so please use only with NodeJS >=6.4.0.

If you need to support older versions of Node, please use a version compatible with ^[email protected].

$ npm install winston
$ npm install winston-graylog2

Usage

var winston = require('winston');
var WinstonGraylog2 = require('winston-graylog2');

var options = { ...<your config options here>... };
winston.add(new WinstonGraylog2(options));

or

var winston
var WinstonGraylog2 = require('winston-graylog2');

var options = { ...<your config options here>... };
var logger = winston.createLogger({
  exitOnError: false,
  transports: [
    new WinstonGraylog2(options),
  ],
});

Options

  • name: Transport name
  • level: Level of messages this transport should log. (default: info)
  • silent: Boolean flag indicating whether to suppress output. (default: false)
  • handleExceptions: Boolean flag, whenever to handle uncaught exceptions. (default: false)
  • exceptionsLevel: Level of exceptions logs when handleExceptions is true. (default: error)
  • graylog:
    • servers; list of graylog2 servers
      • host: your server address (default: localhost)
      • port: your server port (default: 12201)
    • hostname: the name of this host (default: os.hostname())
    • facility: the facility for these log messages (default: "Node.js")
    • bufferSize: max UDP packet size, should never exceed the MTU of your system (default: 1400)
  • staticMeta: meta data to be always used by each logging message, for instance environment (development, staging, live)

Older versions of winston-graylog2 allowed the prelog and processMeta options for pre-processing logs and metadata attached to messages (such as stack traces). Winston 3 has implemented independent, custom formatters for handling these things, and they are no longer supported at the transport level. See the Winston docs for details on formatters.

example:

{
  name: 'Graylog',
  level: 'debug',
  silent: false,
  handleExceptions: false,
  graylog: {
    servers: [{host: 'localhost', port: 12201}, {host: 'remote.host', port: 12201}],
    hostname: 'myServer',
    facility: 'myAwesomeApp',
    bufferSize: 1400
  },
  staticMeta: {env: 'staging'}
}

Upgrading from earlier versions of winston-graylog2

Since [email protected] relies heavily on a very powerful set of formatters there are a few formatting actions that winston-graylog2 no longer needs to do. The two primary cases are the inclusion of a meta object, and converting errors so that the stack trace is included in the log message rather than just the name of the error.

[email protected] includes an excellent formatter for dealing with meta, conveniently named metadata. To use it, you can either grab it from the winston.format object, or use the one on logform.format. See the metadata formatter docs for more details.

For formatting Errors, [email protected] includes the errors formatter. To use it, you can either grab it from the winston.format object, or use the one on logform.format. If you want to include the stack trace in the log message, be sure to pass this formatter the { stack: true } option. See the errrors formatter docs for more details.

In order to get functionality identical to earlier versions of winston-graylog2, use both of these formatters together.

var winston = require('winston');
var { format } = winston;
var WinstonGraylog2 = require('winston-graylog2');

var options = { ...<your config options here>... };
var logger = winston.createLogger({
  exitOnError: false,
  format: format.combine(
    format.errors({ stack: true }),
    format.metadata(),
  ),
  transports: [
    new WinstonGraylog2(options)  ],
});

logger.info({ message: 'this is an info message', answer: 42 });
// or equivalently
logger.info('this is an info message', { answer: 42 });

logger.error({ message: new Error('FakeError'), somenumber: 96 });
// or equivalently
logger.error(new Error('FakeError'), { somenumber: 96 });

Log Levels

Supported log levels, as from node-graylog2, are the following

Winston Level Graylog2 level
emerg emergency
alert alert
crit critical
error error
warning warning
warn warning
notice notice
info info
debug debug

All other possible winston levels, or custom levels, will default to info