Node.js: Configure with Winston

New Relic offers a Winston extension for New Relic Logs, allowing you link to your log data with related data across the rest of the New Relic platform. This document explains how to configure logs in context and start getting log data.

Compatibility and Requirements

Access to logs in context requires a New Relic APM Pro subscription and​ enabling of distributed tracing. Before enabling distributed tracing, read the transition guide to learn about the effects on existing APM features and set-up recommendations.

To use New Relic logs in context with Winston, ensure your configuration meets the following requirements:

Configure logs in context with New Relic Logs

To configure New Relic logs in context with Winston:

  1. Enable New Relic Logs with a compatible log forwarding plugin.
  2. Install or update the Node.js agent.
  3. Configure the Winston extension.
  4. Check for logging data.

Enable New Relic Logs

Confirm that you have New Relic Logs enabled, with a compatible log forwarding plugin installed to send your application logs to New Relic.

Install or update the Node.js agent

Install or update to the most recent Node.js agent version, and enable Distributed tracing.

Configure the Winston extension

To configure logs in context with the Winston extension, complete the following steps:

  1. To install the New Relic Winston log enricher, enter the following command into your terminal or command line interface:

    npm install @newrelic/winston-enricher
  2. In your application code, update your logging configuration to add the newrelicFormatter as shown below:

    // index.js
    require('newrelic')
    const newrelicFormatter = require('@newrelic/winston-enricher')

    The New Relic formatter can be used individually or combined with other formatters as the final format.

    format: winston.format.combine(
      winston.format.label({label: 'test'}),
      newrelicFormatter()
    )

Check for logging data

To verify that you have configured the extension correctly, run your application and verify that the logging you have configured contains the following:

  1. Includes trace.id and span.id fields
  2. Is properly-formatted JSON lines

If everything is configured correctly and your data is being reported, you should see data logs in the New Relic Logs UI using the query operator has: span.id/trace.id.

Troubleshooting

Problem

Not all log data in a message or for a specific attribute is being displayed.

Cause

The stack trace will be written to the error.stack property. To accommodate the 4000 character log line limit for New Relic Logs, the stack and trace properties will be removed and the message, error.message and error.stack values will be truncated to 1024 characters.

What's next?

Now that you've set up APM logs in context, here are some potential next steps:

For more help

Recommendations for learning more: