newrelic_logrus

package module
v0.0.0-...-e3411e0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2022 License: MIT Imports: 3 Imported by: 0

README

New Relic Logrus Hook

This is a simple logrus hook that lets existing logrus applications hook into New Relic error reporting using the New Relic Go Agent.

New Relic relies on having errors associated witha transaction. But logrus doesn't know which transaction its being called from. So, you have two options:

  • Pass nothing and a new New Relic transaction will be created and the error associatied with that.
  • Use WithFields(logrus.Fields{"txn": tx}).Error(...) and pass in your existing transaction. This will be used instead.

If you use the second strategy, you should also use the included Formatter to remove the txn field from the logs. The Formatter can also masque any fields you wish to prevent from being logged in the clear.

In either case, if any fields are supplied to the log line via WithFields, they are reported as custom attributes on the errorTxn and will be visible in New Relic.

Usage

You can install this like any other logrus hook. Assuming that application is your newrelic.Application from the Go agent, you can "hook" it up like this:

log.AddHook(
	newrelic_logrus.NewNewRelicLogrusHook(
		application,
		[]log.Level{log.ErrorLevel, log.FatalLevel},
	),
)

The following will enable filtered txn from your logs, and will also allow filtering of any fields entitled password. You may specify any underlying logrus formatter you like. In this case we use the normal TextFormatter.

formatter := NewFormatter([]string{"password"}, &log.TextFormatter{})
log.SetFormatter(formatter)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter struct {
	// contains filtered or unexported fields
}

A Formatter implements the logrus.Formatter interface and allows filtering out certain fields.

func NewFormatter

func NewFormatter(fieldList []string, formatter logrus.Formatter) *Formatter

NewFormatter creates a properly configure formatter

func (*Formatter) Format

func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error)

Format takes a Logrus entry and then formats it into the correct output. It does that by manipulating the fields and then calling the underlying formatter it was constructed with.

type NewRelicLogrusHook

type NewRelicLogrusHook struct {
	Application newrelic.Application
	LogLevels   []logrus.Level
	// contains filtered or unexported fields
}

func NewNewRelicLogrusHook

func NewNewRelicLogrusHook(app newrelic.Application, levels []logrus.Level) *NewRelicLogrusHook

func (*NewRelicLogrusHook) Fire

func (n *NewRelicLogrusHook) Fire(entry *logrus.Entry) error

func (*NewRelicLogrusHook) Levels

func (n *NewRelicLogrusHook) Levels() []logrus.Level

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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