v3.10.0+incompatible Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2021 License: Apache-2.0 Imports: 8 Imported by: 0


_integrations/logcontext/nrlogrusplugin GoDoc

Package nrlogrusplugin decorates logs for sending to the New Relic backend.

import ""

For more information, see godocs.



Package nrlogrusplugin decorates logs for sending to the New Relic backend.

Use this package if you want to enable the New Relic logging product and see your log messages in the New Relic UI.

Since Logrus is completely api-compatible with the stdlib logger, you can replace your `"log"` imports with `log ""` and follow the steps below to enable the logging product for use with the stdlib Go logger.

Using `logger.WithField` ( and `logger.WithFields` ( is supported. However, if the field key collides with one of the keys used by the New Relic Formatter, the value will be overwritten. Reserved keys are those found in the `logcontext` package (

Supported types for `logger.WithField` and `logger.WithFields` field values are numbers, booleans, strings, and errors. Func types are dropped and all other types are converted to strings.

Requires v1.4.0 of the Logrus package or newer.


For the best linking experience be sure to enable Distributed Tracing:

cfg := NewConfig("Example Application", "__YOUR_NEW_RELIC_LICENSE_KEY__")
cfg.DistributedTracer.Enabled = true

To enable log decoration, set your log's formatter to the `nrlogrusplugin.ContextFormatter`

logger := log.New()

or if you are using the logrus standard logger


The logger will now look for a newrelic.Transaction inside its context and decorate logs accordingly. Therefore, the Transaction must be added to the context and passed to the logger. For example, this logging call

logger.Info("Hello New Relic!")

must be transformed to include the context, such as:

ctx := newrelic.NewContext(context.Background(), txn)
logger.WithContext(ctx).Info("Hello New Relic!")


When properly configured, your log statements will be in JSON format with one message per line:

{"message":"Hello New Relic!","log.level":"info","":"469a04f6c1278593","":"9f365c71f0f04a98","entity.type":"SERVICE","entity.guid":"MTE3ODUwMHxBUE18QVBQTElDQVRJT058Mjc3MDU2Njc1","hostname":"my.hostname","timestamp":1568917432034,"":"Example Application"}

If the `` key is missing, be sure that Distributed Tracing is enabled and that the Transaction context has been added to the logger using `WithContext` (



This section is empty.


This section is empty.


This section is empty.


type ContextFormatter

type ContextFormatter struct{}

ContextFormatter is a `logrus.Formatter` that will format logs for sending to New Relic.

func (ContextFormatter) Format

func (f ContextFormatter) Format(e *logrus.Entry) ([]byte, error)

Format renders a single log entry.


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL