github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrlogrus/nrlogrus.go (about)

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