NodeJS log collection
Datadog's Research Report: The State of Serverless Report: The State of Serverless

NodeJS log collection

Overview

Using Winston to log from your NodeJS application gets you all the features you need to build up your logging strategy.

Winston is available through NPM, to get started, you want to add the dependency to your code:

npm install --save winston

package.json is updated with the corresponding dependencies:

{
  "name": "...",

  //...
  "dependencies": {
    //...
    "winston": "x.y.z",
    //...
  }
}

Setup

Inject trace IDs in your logs: If APM is enabled for this application and you wish to improve the correlation between application logs and traces, follow APM NodeJS logging instructions to automatically add trace and span IDs in your logs.

Log to file

In your bootstrap file or somewhere in your code, declare the logger as follow:

const { createLogger, format, transports } = require('winston');

const logger = createLogger({
  level: 'info',
  exitOnError: false,
  format: format.json(),
  transports: [
    new transports.File({ filename: `${appRoot}/logs/<FILE_NAME>.log` }),
  ],
});

module.exports = logger;

// Example logs
logger.log('info', 'Hello simple log!');
logger.info('Hello log with metas',{color: 'blue' });
var winston = require('winston');

var logger = new (winston.Logger)({
    transports: [
        new (winston.transports.File)({
            name: '<LOGGER_NAME>',
            filename: '<FILE_NAME>.log',
      json: true,
            level: 'info'
        })
    ]
});

// Example logs
logger.log('info', 'Hello simple log!');
logger.info('Hello log with metas',{color: 'blue' });

Check the content of the <FILE_NAME>.log file to see that Winston already took care of logging everything in JSON:

{"level":"info","message":"Hello simple log!","timestamp":"2015-04-23T16:52:05.337Z"}
{"color":"blue","level":"info","message":"Hello log with metas","timestamp":"2015-04-23T16:52:05.339Z"}

Configure your Datadog Agent

Create a nodejs.d/conf.yaml file in your conf.d/ folder with the following content:

init_config:

instances:

##Log section
logs:

  - type: file
    path: "<FILE_NAME_PATH>.log"
    service: nodejs
    source: nodejs
    sourcecategory: sourcecode

Agentless Logging

You can stream your logs from your application to Datadog without installing an Agent on your Host. Note that using an Agent to forward your logs is recommended as it provides a native connection management.

Use Winston HTTP transport to send your logs directly through the Datadog Log API. In your bootstrap file or somewhere in your code, declare the logger as follow:

const { createLogger, format, transports } = require('winston');

const httpTransportOptions = {
  host: 'http-intake.logs.datadoghq.com',
  path: '/v1/input/<APIKEY>?ddsource=nodejs&service=<APPLICATION_NAME>',
  ssl: true
};

const logger = createLogger({
  level: 'info',
  exitOnError: false,
  format: format.json(),
  transports: [
    new transports.Http(httpTransportOptions),
  ],
});

module.exports = logger;

// Example logs
logger.log('info', 'Hello simple log!');
logger.info('Hello log with metas',{color: 'blue' });

Note: To send logs to Datadog EU site, set the host property to http-intake.logs.datadoghq.eu

If you are using US site, you can also check the community supported Datadog Transport.

Troubleshooting

In case you have dns lookup errors or crash in your application this could be linked to logstash exceptions not being caught. A handler should be added as follows:

var logstash = new winston.transports.Logstash({ ... });
logstash.on('error', function(err) {
    console.error(err); // replace with your own functionality here
});

Make sure that the parameter max_connect_retries is not set to 1 (the default is 4).

Further Reading