Skip to content

Latest commit

 

History

History
271 lines (198 loc) · 16.3 KB

configure-logs-context-nodejs.mdx

File metadata and controls

271 lines (198 loc) · 16.3 KB
title tags metaDescription redirects freshnessValidatedDate
Node.js agent logs in context
Logs
Enable log management in New Relic
Logs in context for Node.js
Winston
Pino
For apps monitored by the Node.js agent, learn how to link log data with related data across the rest of the New Relic platform.
/docs/logs/new-relic-logs/enable-logs-context/enable-logs-context-nodejs
/docs/logs/enable-logs/logs-context-node/configure-logs-context-node
/docs/logs/enable-log-monitoring-new-relic/logs-context-nodejs/configure-logs-context-nodejs
/docs/logs/new-relic-logs/enable-logs-context/enable-logs-context-winston
/docs/logs/enable-logs/logs-context-node/nodejs-configure-winston
/docs/logs/enable-log-monitoring-new-relic/logs-context-nodejs/nodejs-configure-winston
/docs/logs/enable-log-management-new-relic/logs-context-nodejs/nodejs-configure-winston
/docs/logs/enable-log-management-new-relic/logs-context-nodejs/configure-logs-context-nodejs
/docs/logs/enable-log-management-new-relic/logs-context-nodejs
/docs/logs/enable-log-management-new-relic/configure-logs-context/configure-logs-context-nodejs
never

With our Node.js APM agent, you can get logs in context, which lets you see your app logs in the context of your other New Relic data. For general information on this feature, see APM logs in context.

Got lots of Node.js logs? Check out our [tutorial on how to optimize and manage them](/docs/tutorial-large-logs/get-started-managing-large-logs/).

Automatic logs in context options [#automatic]

With application logging, your APM agent automatically collects and contextualizes your logs.

If you're using a supported framework, you have three options to configure APM logs in context to send your app's logs and linking metadata automatically to New Relic.

Supported frameworks for automatic logs in context include:

  • Winston 3.0.0 or higher.
  • Pino 7.0.0 or higher.
  • Bunyan 1.8.12 or higher (since agent version 9.3.0)

If you're not using a supported framework, you may instead use the agent's log forwarding API method to perform your own instrumentation. For details, see the Node.js agent API docs.

Agent releases 8.16.0 and higher have this feature enabled in the agent configuration file by default.
Agent log forwarding will cause an increase in the consumption of data when a [supported framework](/docs/logs/logs-context/node-configure-logs-context-all#automatic) is detected. The amount depends on the application and amount of logs it produces. This feature can be disabled. See [Disable automatic logging](/docs/logs/logs-context/disable-automatic-logging/) for more information about your options.

If you already have a log forwarding solution in place, you should disable this feature.
This is the simplest approach, and it's a great choice for developers who may not have the access or interest in setting up a log forwarder, or for accounts that want to see the power of logs and other linking metadata in context of their apps, without a lot of overhead.
Using this option, the agent reports your entire log and adds our attributes `span.id`, `trace.id`, `hostname`, `entity.guid`, and `entity.name`. These attributes link your logs to spans, traces, and other telemetry, making it easier to troubleshoot.

All you need to do is install an agent version with log forwarding capabilities ([Node.js agent 8.11.0 or higher](/docs/release-notes/agent-release-notes/nodejs-release-notes/)). If forwarding is disabled, you can use this configuration:

newrelic.js:

```js
application_logging: {
  forwarding: {
    enabled: true
  }
}
```

Environment variable:

```
NEW_RELIC_APPLICATION_LOGGING_FORWARDING_ENABLED=true
```

This option is on by default since agent version 8.16.0.

<DNT>**Optional adjustments:**</DNT>

Once this is enabled, you also have control over the maximum number of logs sent to New Relic every minute. The default value is 10,000. If more than 10,000 logs are received in a 60-second window, your logs will begin to be sampled.

Set it to a higher number to receive more logs. Set it to a lower number to receive fewer logs. Any integer up to 100,000 is valid. Increasing this value may increase the agent's utilization of CPU and RAM.

newrelic.js:

```js
application_logging: {
  forwarding: {
    max_samples_stored: 10000
  }
}
```

Environment variable:

```
NEW_RELIC_APPLICATION_LOGGING_FORWARDING_MAX_SAMPLES_STORED: 10000
```

If you have an existing log forwarding solution and are updating your agent to use automatic logs in context, be sure to <DNT>**disable your manual log forwarder**</DNT>. Otherwise, your app will be sending double log lines. Depending on your account, this could result in double billing. For more information, follow the procedures to disable your [specific log forwarder](/docs/logs/forward-logs/enable-log-management-new-relic#log-forwarding).

<Collapser id="2-decorate" title="Option 2: Let the agent decorate your logs."

Already have a log forwarder you like? We've got you covered! Language agents can decorate your logs with the linking metadata needed to provide access to automatic logs-in-context features.

This option should not be used with in-agent forwarding. Using an [external log forwarder](/docs/logs/forward-logs/enable-log-management-new-relic#log-forwarding) to send logs to New Relic while in-agent forwarding is enabled will cause your logs to be sent up twice to New Relic. Depending on your account, this may result in double billing.

This option should also not be used with [the manual log decorating formatter](#3-old-formatter). If you have references to the manual formatter in your codebase, please remove them before enabling this option.

1. If you want to use this option, make sure you have the in-agent forwarding configuration option disabled.

   newrelic.js:

   ```js
   application_logging: {
     forwarding: {
       enabled: false
     }
   }
   ```

2. Enable log decorating in your configuration, then relaunch the agent to start decorating your logs.

   newrelic.js:

   ```js
   application_logging: {
     local_decorating: {
       enabled: true
     }
   }
   ```

   Environment variable:

   ```
   NEW_RELIC_APPLICATION_LOGGING_LOCAL_DECORATING_ENABLED=true
   ```

   Our decorator adds five attributes to every log message: `entity.guid`, `entity.name`, `hostname`, `trace.id`, and  `span.id`. Example:

   ```
   This is my log message. NR-LINKING|{entity.guid}|{hostname}|{trace.id}|{span.id}|{entity.name}|
   ```

  Some attributes may be empty if the log occurred outside a transaction or if they are not applicable to your application's context.

   We recommend this option over manually using one of our log enrichers.

<Collapser id="3-old-formatter" title="Option 3: Use the manual process to forward and decorate logs."

Before language agents had the ability to forward and decorate logs, you could enable logs in context by updating your application to use a framework-specific log enricher. This option is still supported, but is no longer encouraged. For instructions to use this approach, see [Manual logs in context option](#configure-nodejs).

Also, this method requires that you install a log forwarder before enabling logs in context. If you do not have a log forwarder, the New Relic UI will prompt you to use our [infrastructure agent](/docs/logs/forward-logs/forward-your-logs-using-infrastructure-agent/).

If you decide to use your existing log forwarding solution and later decide to update your agent to use automatic logs in context, be sure to <DNT>**disable your manual log forwarder**</DNT>. Otherwise, your app will be sending double log lines. Depending on your account, this could result in double billing. For more information, follow the procedures to disable your [specific log forwarder](/docs/logs/forward-logs/enable-log-management-new-relic#log-forwarding).

Secure your data [#obfuscation]

Your logs may include sensitive information protected by HIPAA or other compliance protocols. By default we obfuscate number patterns that appear to be for items such as credit cards or Social Security numbers, but you may need to hash or mask additional information.

For more information, see our documentation about obfuscation expressions and rules. You can hash or mask your log data by using the New Relic UI or by using NerdGraph, our GraphQL API.

Explore your data [#view-ui]

To make the most of your logging data:

  • On the APM Summary page, click your Web transaction time chart to view logs associated with a specific point in time.
  • Check your app's Errors inbox to view the logs associated with your errors.
  • Use distributed tracing to see the logs associated with individual traces.
  • Explore more logging data across your platform with our logs UI.
  • Set up alerts based on log output and severity.
  • Query your data using our specialized UI for logs data, and create dashboards with the results.

Troubleshooting tips [#troubleshooting]

Typically your logs will start to appear less than a minute after you enable logs in context. Check your app's Triage > Logs section. You will also start seeing log patterns related to the error there.

If you don't see any logs for errors or traces, there may not be any for your app. Try refreshing the UI page, or change the selected time period.

Disable automatic logging [#disable-automatic]

APM logs in context automatically forwards APM agent log data. It is enabled by default. If you do enable application logging, it can have a negative impact on your security, compliance, billing, or system performance. For more information, or if you need to adjust the default setting, follow the procedures to disable automatic logging.

Manual logs in context option [#configure-nodejs]

To enable logs in context for apps monitored by Node.js, you can use our manual installation option.

  1. Make sure you have already set up logging in New Relic. This includes configuring a supported log forwarder that collects your application logs and extends the metadata that is forwarded to New Relic.

  2. Install or update to the latest Node.js agent version, and enable distributed tracing. Use Node.js agent version 6.2.0 or higher for logs in context.

  3. Install a supported framework to enrich your log data, or directly use the agent's log forwarding API.

  4. In your agent configuration, set application_config.enabled to false. (Otherwise, the agent will automatically instrument your logger and calling these enrichers yourself will do nothing.)

  5. Configure logs in context for Node.js using the appropriate log extension.

    1. To install the Winston log enricher, enter the following command into your terminal or command line interface:
         ```bash
         npm install @newrelic/winston-enricher
         ```
       2. In your application code, update your logging configuration to add the `newrelicFormatter`:
    
         ```js
         // index.js
         require('newrelic')
         const winston = require('winston')
         const newrelicFormatter = require('@newrelic/winston-enricher')(winston)
         ```
    
         The New Relic formatter can be used individually or combined with other formatters as the final format.
    
         ```js
         format: winston.format.combine(
           winston.format.label({label: 'test'}),
           newrelicFormatter()
         )
         ```
     </Collapser>
    
     <Collapser
       id="nodejs-pino"
       title="Node.js configuration with Pino"
     >
       1. To install the Pino log enricher, enter the following command into your terminal or command line interface:
    
         ```bash
         npm install @newrelic/pino-enricher
         ```
       2. In your application code, update your logging configuration to add the New Relic Pino plugin:
    
         ```js
         // index.js
         require('newrelic')
         const nrPino = require('@newrelic/pino-enricher')
         const pino = require('pino')
         const logger = pino(nrPino())
         ```
    
         The New Relic formatter can be used individually or combined with other formatters as the final format.
     </Collapser>
    
  6. To verify that you have configured the log appender correctly, run your application, then check your logs data in New Relic using the query operator has:span.id has:trace.id.

If everything is configured correctly and your data is being forwarded to New Relic with the enriched metadata, your logs should now be emitted as JSON and contain trace.id and span.id fields. If you don't see log data in the UI, follow the troubleshooting procedures. If you'd like to see a working example of logs in context, take a look at our example application.

What's next? [#what-next]

After you set up logs in context, make the most of your logging data: