File: nrlogxi.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 (58 lines) | stat: -rw-r--r-- 1,557 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
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

// Package nrlogxi supports https://github.com/mgutz/logxi.
//
// Wrap your logxi Logger using nrlogxi.New to send agent log messages through
// logxi.
package nrlogxi

import (
	log "github.com/mgutz/logxi/v1"
	"github.com/newrelic/go-agent/v3/internal"
	newrelic "github.com/newrelic/go-agent/v3/newrelic"
)

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

type shim struct {
	e log.Logger
}

func (l *shim) Error(msg string, context map[string]interface{}) {
	l.e.Error(msg, convert(context)...)
}
func (l *shim) Warn(msg string, context map[string]interface{}) {
	l.e.Warn(msg, convert(context)...)
}
func (l *shim) Info(msg string, context map[string]interface{}) {
	l.e.Info(msg, convert(context)...)
}
func (l *shim) Debug(msg string, context map[string]interface{}) {
	l.e.Debug(msg, convert(context)...)
}
func (l *shim) DebugEnabled() bool {
	return l.e.IsDebug()
}

func convert(c map[string]interface{}) []interface{} {
	output := make([]interface{}, 0, 2*len(c))
	for k, v := range c {
		output = append(output, k, v)
	}
	return output
}

// New returns a newrelic.Logger which forwards agent log messages to the
// provided logxi Logger.
func New(l log.Logger) newrelic.Logger {
	return &shim{
		e: l,
	}
}

// ConfigLogger configures the newrelic.Application to send log messsages to the
// provided logxi logger.
func ConfigLogger(l log.Logger) newrelic.ConfigOption {
	return newrelic.ConfigLogger(New(l))
}