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
|
// 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, assign the newrelic.Config.Logger field to
// nrlogrus.StandardLogger():
//
// cfg := newrelic.NewConfig("Your Application Name", "__YOUR_NEW_RELIC_LICENSE_KEY__")
// cfg.Logger = nrlogrus.StandardLogger()
//
// If you are using a particular logrus Logger instance, assign the
// newrelic.Config.Logger field to the the output of nrlogrus.Transform:
//
// l := logrus.New()
// l.SetLevel(logrus.DebugLevel)
// cfg := newrelic.NewConfig("Your Application Name", "__YOUR_NEW_RELIC_LICENSE_KEY__")
// cfg.Logger = nrlogrus.Transform(l)
//
// This package requires logrus version v1.1.0 and above.
package nrlogrus
import (
newrelic "github.com/newrelic/go-agent"
"github.com/newrelic/go-agent/internal"
"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",
}),
}
}
|