1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// Package nrlogrus sends go-agent log messages to
// https://github.com/sirupsen/logrus.
//
// Use this package if you are using logrus in your application and would like
// the go-agent log messages to end up in the same place. If you are using
// the logrus standard logger, use ConfigStandardLogger when creating your
// application:
//
// app, err := newrelic.NewApplication(
// newrelic.ConfigFromEnvironment(),
// nrlogrus.ConfigStandardLogger(),
// )
//
// If you are using a particular logrus Logger instance, then use ConfigLogger:
//
// l := logrus.New()
// l.SetLevel(logrus.DebugLevel)
// app, err := newrelic.NewApplication(
// newrelic.ConfigFromEnvironment(),
// nrlogrus.ConfigLogger(l),
// )
//
// This package requires logrus version v1.1.0 and above.
package nrlogrus
import (
"github.com/newrelic/go-agent/v3/internal"
newrelic "github.com/newrelic/go-agent/v3/newrelic"
"github.com/sirupsen/logrus"
)
func init() { internal.TrackUsage("integration", "logging", "logrus") }
type shim struct {
e *logrus.Entry
l *logrus.Logger
}
func (s *shim) Error(msg string, c map[string]interface{}) {
s.e.WithFields(c).Error(msg)
}
func (s *shim) Warn(msg string, c map[string]interface{}) {
s.e.WithFields(c).Warn(msg)
}
func (s *shim) Info(msg string, c map[string]interface{}) {
s.e.WithFields(c).Info(msg)
}
func (s *shim) Debug(msg string, c map[string]interface{}) {
s.e.WithFields(c).Debug(msg)
}
func (s *shim) DebugEnabled() bool {
lvl := s.l.GetLevel()
return lvl >= logrus.DebugLevel
}
// StandardLogger returns a newrelic.Logger which forwards agent log messages to
// the logrus package-level exported logger.
func StandardLogger() newrelic.Logger {
return Transform(logrus.StandardLogger())
}
// Transform turns a *logrus.Logger into a newrelic.Logger.
func Transform(l *logrus.Logger) newrelic.Logger {
return &shim{
l: l,
e: l.WithFields(logrus.Fields{
"component": "newrelic",
}),
}
}
// ConfigLogger configures the newrelic.Application to send log messsages to the
// provided logrus logger.
func ConfigLogger(l *logrus.Logger) newrelic.ConfigOption {
return newrelic.ConfigLogger(Transform(l))
}
// ConfigStandardLogger configures the newrelic.Application to send log
// messsages to the standard logrus logger.
func ConfigStandardLogger() newrelic.ConfigOption {
return newrelic.ConfigLogger(StandardLogger())
}
|