newrelicapm

Newrelic log forwarding issue


I have setup new relic for my local dev environment, I want to forward all my custom logs to new relic. For that I made the following change in my newrelic.js

application_logging: {
    forwarding: {
      enabled: true
    }
  } 

I am using 'winston-enricher' for logs and I do see properly formatted logs in my terminal console but these logs are not getting forwarded to new relic UI. I do see proper APM logs there, just not custom.

newrelic.js

'use strict'
/**
 * New Relic agent configuration.
 *
 * See lib/config/default.js in the agent distribution for a more complete
 * description of configuration variables and their potential values.
 */
exports.config = {
  /**
   * Array of application names.
   */
  app_name: ['app_name'],
  
  application_logging: {
    forwarding: {
      enabled: true
    }
  },
  /**
   * Your New Relic license key.
   */
  license_key: '<API_KEY>',
  /**
   * Whether the module is enabled.
   *
   * @env NEW_RELIC_ENABLED
   */
  agent_enabled: true,
  /**
   * This setting controls distributed tracing.
   * Distributed tracing lets you see the path that a request takes through your
   * distributed system. Enabling distributed tracing changes the behavior of some
   * New Relic features, so carefully consult the transition guide before you enable
   * this feature: https://docs.newrelic.com/docs/transition-guide-distributed-tracing
   * Default is false.
   */
  distributed_tracing: {
    /**
     * Enables/disables distributed tracing.
     *
     * @env NEW_RELIC_DISTRIBUTED_TRACING_ENABLED
     */
    enabled: true
  },
  logging: {
    /**
     * Level at which to log. 'trace' is most useful to New Relic when diagnosing
     * issues with the agent, 'info' and higher will impose the least overhead on
     * production applications.
     */
    level: 'info',
    /**
     * Where to put the log file -- by default just uses process.cwd +
     * 'newrelic_agent.log'. A special case is a filepath of 'stdout',
     * in which case all logging will go to stdout, or 'stderr', in which
     * case all logging will go to stderr.
     *
     * @env NEW_RELIC_LOG
     */
    filepath: require('path').join(process.cwd(), 'newrelic_agent.log'),
    /**
     * Whether to write to a log file at all
     *
     * @env NEW_RELIC_LOG_ENABLED
     */
    enabled: true
  },
  /**
   * When true, all request headers except for those listed in attributes.exclude
   * will be captured for all traces, unless otherwise specified in a destination's
   * attributes include/exclude lists.
   */
  allow_all_headers: true,
  attributes: {
    /**
     * Prefix of attributes to exclude from all destinations. Allows * as wildcard
     * at end.
     *
     * NOTE: If excluding headers, they must be in camelCase form to be filtered.
     *
     * @env NEW_RELIC_ATTRIBUTES_EXCLUDE
     */
    exclude: [
      'request.headers.cookie',
      'request.headers.authorization',
      'request.headers.proxyAuthorization',
      'request.headers.setCookie*',
      'request.headers.x*',
      'response.headers.cookie',
      'response.headers.authorization',
      'response.headers.proxyAuthorization',
      'response.headers.setCookie*',
      'response.headers.x*'
    ]
  }
}


Solution

  • There has been some confusion with the way the two features have been presented, namely, log enriching and agent forwarding.

    They are, in a way, mutually exclusive. You can either have the agent directly forward the logs or you can forward them yourself. If you forward them yourself, they have to be decorated so that they properly show up in the New Relic interface. You may have your reasons to forward the logs yourself, but if you don't, New Relic considers that the legacy option and recommends you don't use the winston enricher anymore.

    On the latest version of the agent, if you just disable the winston enricher and remove the application_logging.forwarding.enabled setting, it will default to forwarding your logs.

    Also note, that according to the docs, you need both application_logging.enabled and application_logging.forwarding.enabled. The former option at the higher level needs to be enabled for the latter and other features to be active, such as logging metrics.