File: nrzap.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (45 lines) | stat: -rw-r--r-- 1,352 bytes parent folder | download
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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

// Package nrzap supports https://github.com/uber-go/zap
//
// Wrap your zap Logger using nrzap.Transform to send agent log messages to zap.
package nrzap

import (
	newrelic "github.com/newrelic/go-agent"
	"github.com/newrelic/go-agent/internal"
	"go.uber.org/zap"
)

func init() { internal.TrackUsage("integration", "logging", "zap") }

type shim struct{ logger *zap.Logger }

func transformAttributes(atts map[string]interface{}) []zap.Field {
	fs := make([]zap.Field, 0, len(atts))
	for key, val := range atts {
		fs = append(fs, zap.Any(key, val))
	}
	return fs
}

func (s *shim) Error(msg string, c map[string]interface{}) {
	s.logger.Error(msg, transformAttributes(c)...)
}
func (s *shim) Warn(msg string, c map[string]interface{}) {
	s.logger.Warn(msg, transformAttributes(c)...)
}
func (s *shim) Info(msg string, c map[string]interface{}) {
	s.logger.Info(msg, transformAttributes(c)...)
}
func (s *shim) Debug(msg string, c map[string]interface{}) {
	s.logger.Debug(msg, transformAttributes(c)...)
}
func (s *shim) DebugEnabled() bool {
	ce := s.logger.Check(zap.DebugLevel, "debugging")
	return ce != nil
}

// Transform turns a *zap.Logger into a newrelic.Logger.
func Transform(l *zap.Logger) newrelic.Logger { return &shim{logger: l} }