NodeJS Log Collection

Configure your logger

To send your logs to Datadog, log to a file and tail that file with your Datadog Agent. Use the Winston logging library to log from your NodeJS application.

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",
    //...
  }
}

Log to a file

In your bootstrap file or in your code, declare the logger in the following way:


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 confirm that Winston is logging 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

Once log collection is enabled, set up custom log collection to tail your log files and send new logs to Datadog.

  1. Create a nodejs.d/ folder in the conf.d/ Agent configuration directory.
  2. Create a conf.yaml file in nodejs.d/ with the following content:
init_config:

instances:

##Log section
logs:

  - type: file
    path: "<FILE_NAME_PATH>.log"
    service: nodejs
    source: nodejs
    sourcecategory: sourcecode
  1. Restart the Agent.
  2. Run the Agent’s status subcommand and look for nodejs under the Checks section to confirm logs are successfully submitted to Datadog.

If logs are in JSON format, Datadog automatically parses the log messages to extract log attributes. Use the Log Explorer to view and troubleshoot your logs.

Connect your service across logs and traces

If APM is enabled for this application, connect your logs and traces by automatically adding trace IDs, span IDs, env, service, and version to your logs by following the APM NodeJS instructions.

Note: If the APM tracer injects service into your logs, it overrides the value set in the Agent configuration.

Agentless logging

You can stream your logs from your application to Datadog without installing an Agent on your host. However, it is recommended that you use an Agent to forward your logs as it provides a native connection management.

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

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

const httpTransportOptions = {
  host: 'http-intake.logs.datadoghq.com',
  path: '/api/v2/logs?dd-api-key=<DATADOG_API_KEY>&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: You can also use the community supported Datadog Transport.

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

const httpTransportOptions = {
  host: 'http-intake.logs.datadoghq.eu',
  path: '/api/v2/logs?dd-api-key=<DATADOG_API_KEY>&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' });

Troubleshooting

If you have DNS lookup errors in your application this could be due 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