Graylog: Using the Winston GELF package to send Node.js logs to Graylog

This documentation is part of the Using with Node.js guide. You can view the complete guide here: How to send logs from Node.js to Graylog.

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use Graylog cloud solution that provides a host of benefits, including:

  • Unlimited and dedicated SMTP email server included.
  • Effortless updates with just a click.
  • Customisable domain name secured with HTTPS (for example, https://logs.your-company.com).
  • Optimal performance and robust security powered by a private and dedicated VM.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's Graylog cloud hosting solution!

warning It is preferable to use the graylog2 package unless Winston is already part of your project. In that case, you can replace it with winston-gelf.

To install the Winston GELF package, run:

npm install winston-gelf

If Winston is not already included in your project, add it with:

npm install winston

Here is a basic configuration:

const winston = require('winston');
const winstonGelf = require('winston-gelf');
const process = require('process');

const logger = winston.createLogger({
  transports: [
    new winston.transports.Console(),
    new winstonGelf({
      // See all gelfPro options here: https://www.npmjs.com/package/gelf-pro
      gelfPro: {
        fields: {
          env: process.env.NODE_ENV || 'development'
        },
        adapterName: 'udp',
        adapterOptions: {
          host: '<XXXXXX>.stackhero-network.com', // Replace with your Graylog domain
          port: 12201,
        }
      }
    })
  ]
});

// Example of an informational log
logger.info('This is a log information');

// Example of an error log
try {
  throw Error('This is an example error');
}
catch(error) {
  logger.warn({ message: 'Error triggered', error });
}

Do not forget to configure your Graylog input (see details below).