File: nrlogrus_test.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 (49 lines) | stat: -rw-r--r-- 1,212 bytes parent folder | download | duplicates (2)
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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package nrlogrus

import (
	"bytes"
	"strings"
	"testing"

	"github.com/sirupsen/logrus"
)

func bufferToStringAndReset(buf *bytes.Buffer) string {
	s := buf.String()
	buf.Reset()
	return s
}

func TestLogrus(t *testing.T) {
	buf := &bytes.Buffer{}
	l := logrus.New()
	l.SetOutput(buf)
	l.SetLevel(logrus.DebugLevel)
	lg := Transform(l)
	lg.Debug("elephant", map[string]interface{}{"color": "gray"})
	s := bufferToStringAndReset(buf)
	if !strings.Contains(s, "elephant") || !strings.Contains(s, "gray") {
		t.Error(s)
	}
	if enabled := lg.DebugEnabled(); !enabled {
		t.Error(enabled)
	}
	// Now switch the level and test that debug is no longer enabled.
	l.SetLevel(logrus.InfoLevel)
	lg.Debug("lion", map[string]interface{}{"color": "yellow"})
	s = bufferToStringAndReset(buf)
	if strings.Contains(s, "lion") || strings.Contains(s, "yellow") {
		t.Error(s)
	}
	if enabled := lg.DebugEnabled(); enabled {
		t.Error(enabled)
	}
	lg.Info("tiger", map[string]interface{}{"color": "orange"})
	s = bufferToStringAndReset(buf)
	if !strings.Contains(s, "tiger") || !strings.Contains(s, "orange") {
		t.Error(s)
	}
}