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

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  // Package nrzap supports https://github.com/uber-go/zap
     5  //
     6  // Wrap your zap Logger using nrzap.Transform to send agent log messages to zap.
     7  package nrzap
     8  
     9  import (
    10  	newrelic "github.com/newrelic/go-agent"
    11  	"github.com/newrelic/go-agent/internal"
    12  	"go.uber.org/zap"
    13  )
    14  
    15  func init() { internal.TrackUsage("integration", "logging", "zap") }
    16  
    17  type shim struct{ logger *zap.Logger }
    18  
    19  func transformAttributes(atts map[string]interface{}) []zap.Field {
    20  	fs := make([]zap.Field, 0, len(atts))
    21  	for key, val := range atts {
    22  		fs = append(fs, zap.Any(key, val))
    23  	}
    24  	return fs
    25  }
    26  
    27  func (s *shim) Error(msg string, c map[string]interface{}) {
    28  	s.logger.Error(msg, transformAttributes(c)...)
    29  }
    30  func (s *shim) Warn(msg string, c map[string]interface{}) {
    31  	s.logger.Warn(msg, transformAttributes(c)...)
    32  }
    33  func (s *shim) Info(msg string, c map[string]interface{}) {
    34  	s.logger.Info(msg, transformAttributes(c)...)
    35  }
    36  func (s *shim) Debug(msg string, c map[string]interface{}) {
    37  	s.logger.Debug(msg, transformAttributes(c)...)
    38  }
    39  func (s *shim) DebugEnabled() bool {
    40  	ce := s.logger.Check(zap.DebugLevel, "debugging")
    41  	return ce != nil
    42  }
    43  
    44  // Transform turns a *zap.Logger into a newrelic.Logger.
    45  func Transform(l *zap.Logger) newrelic.Logger { return &shim{logger: l} }