namshi / winston-graylog2

Graylog2 transport for winston, a nodejs logging module
MIT License
126 stars 62 forks source link

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 ^winston-graylog2@1.1.0.

$ 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

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 winston@3.x 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.

winston@3.x 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, winston@3.2.0 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