Node Logging

Node Logging

Infinum uses the Graylog for logging all requests which are processed by our apps.

Setup

Install gelf-pro package via npm/yarn:

npm install -s gelf-pro

Configuration

Node-only environment

Set configuration on your app logger instance:

const log = require('gelf-pro');

log.setConfig({
  fields: {
    application: 'app-name'
  },
  adapterOptions: {
    host: 'host-name' // graylog host should be configured via secrets
  }
});

Isomorphic environment

If your code is running both in node and in the browser (sharing logic, server-side rendering, etc.), you need to make sure gelf is running only in node. For the client, we're often using loglevel for better logging:

const isServer = typeof window === 'undefined';

let log;
if (isServer) {
  log = require('gelf-pro');

  log.setConfig({
    fields: {
      application: 'app-name'
    },
    adapterOptions: {
      host: 'host-name' // graylog host should be configured via secrets
    }
  });
} else {
  log = require('loglevel');

  // Set level to debug for development or to silent for production
  log.setLevel(process.env.NODE_ENV === 'development' ? 1 : 5);
}

module.exports = log;

❗❗❗Note: gelf-pro and loglevel don't have the same API, so you should only use error, info and debug methods. In the future, we might create a gelf plugin for loglevel which would then abstract those differences.

Usage

log.info('Hello world');
log.error('Something bad has happened')

Documentation

Read more about logging here: * gelf-pro JS API * Graylog & GELF * loglevel