File: logging_test.go

package info (click to toggle)
golang-github-pion-logging 0.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, forky, sid, trixie
  • size: 104 kB
  • sloc: sh: 93; makefile: 2
file content (114 lines) | stat: -rw-r--r-- 2,963 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package logging_test

import (
	"bytes"
	"os"
	"strings"
	"testing"

	"github.com/pion/logging"
)

func testNoDebugLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
	var outBuf bytes.Buffer
	logger.WithOutput(&outBuf)

	logger.Debug("this shouldn't be logged")
	if outBuf.Len() > 0 {
		t.Error("Debug was logged when it shouldn't have been")
	}
	logger.Debugf("this shouldn't be logged")
	if outBuf.Len() > 0 {
		t.Error("Debug was logged when it shouldn't have been")
	}
}

func testDebugLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
	var outBuf bytes.Buffer
	logger.WithOutput(&outBuf)

	dbgMsg := "this is a debug message"
	logger.Debug(dbgMsg)
	if !strings.Contains(outBuf.String(), dbgMsg) {
		t.Errorf("Expected to find %q in %q, but didn't", dbgMsg, outBuf.String())
	}
	logger.Debugf(dbgMsg)
	if !strings.Contains(outBuf.String(), dbgMsg) {
		t.Errorf("Expected to find %q in %q, but didn't", dbgMsg, outBuf.String())
	}
}

func testWarnLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
	var outBuf bytes.Buffer
	logger.WithOutput(&outBuf)

	warnMsg := "this is a warning message"
	logger.Warn(warnMsg)
	if !strings.Contains(outBuf.String(), warnMsg) {
		t.Errorf("Expected to find %q in %q, but didn't", warnMsg, outBuf.String())
	}
	logger.Warnf(warnMsg)
	if !strings.Contains(outBuf.String(), warnMsg) {
		t.Errorf("Expected to find %q in %q, but didn't", warnMsg, outBuf.String())
	}
}

func testErrorLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
	var outBuf bytes.Buffer
	logger.WithOutput(&outBuf)

	errMsg := "this is an error message"
	logger.Error(errMsg)
	if !strings.Contains(outBuf.String(), errMsg) {
		t.Errorf("Expected to find %q in %q, but didn't", errMsg, outBuf.String())
	}
	logger.Errorf(errMsg)
	if !strings.Contains(outBuf.String(), errMsg) {
		t.Errorf("Expected to find %q in %q, but didn't", errMsg, outBuf.String())
	}
}

func TestDefaultLoggerFactory(t *testing.T) {
	f := logging.DefaultLoggerFactory{
		Writer:          os.Stdout,
		DefaultLogLevel: logging.LogLevelWarn,
		ScopeLevels: map[string]logging.LogLevel{
			"foo": logging.LogLevelDebug,
		},
	}

	logger := f.NewLogger("baz")
	bazLogger, ok := logger.(*logging.DefaultLeveledLogger)
	if !ok {
		t.Error("Invalid logger type")
	}

	testNoDebugLevel(t, bazLogger)
	testWarnLevel(t, bazLogger)

	logger = f.NewLogger("foo")
	fooLogger, ok := logger.(*logging.DefaultLeveledLogger)
	if !ok {
		t.Error("Invalid logger type")
	}

	testDebugLevel(t, fooLogger)
}

func TestDefaultLogger(t *testing.T) {
	logger := logging.
		NewDefaultLeveledLoggerForScope("test1", logging.LogLevelWarn, os.Stdout)

	testNoDebugLevel(t, logger)
	testWarnLevel(t, logger)
	testErrorLevel(t, logger)
}

func TestSetLevel(t *testing.T) {
	logger := logging.
		NewDefaultLeveledLoggerForScope("testSetLevel", logging.LogLevelWarn, os.Stdout)

	testNoDebugLevel(t, logger)
	logger.SetLevel(logging.LogLevelDebug)
	testDebugLevel(t, logger)
}