This repository was archived by the owner on Jul 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger_test.go
More file actions
91 lines (75 loc) · 2.4 KB
/
logger_test.go
File metadata and controls
91 lines (75 loc) · 2.4 KB
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
package logger
import (
"context"
"os"
"testing"
"github.com/openmfp/golang-commons/context/keys"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)
func TestLoggerInContext(t *testing.T) {
ctx := context.Background()
logger, _ := New(DefaultConfig())
ctx = SetLoggerInContext(ctx, logger)
retrievedLogger := LoadLoggerFromContext(ctx)
assert.NotNil(t, retrievedLogger)
}
func TestTestLoggerInContext(t *testing.T) {
ctx := context.Background()
logger, err := New(DefaultConfig())
assert.NoError(t, err)
ctx = SetLoggerInContext(ctx, logger)
retrievedLogger := LoadLoggerFromContext(ctx)
assert.NotNil(t, retrievedLogger)
}
func TestTestLoggerInContextFallback(t *testing.T) {
ctx := context.Background()
retrievedLogger := LoadLoggerFromContext(ctx)
assert.NotNil(t, retrievedLogger)
}
func TestNewFromZeroLog(t *testing.T) {
logger := NewFromZerolog(zerolog.New(os.Stdout))
assert.NotNil(t, logger)
}
func TestNewRequestLoggerFromZeroLog(t *testing.T) {
ctx := context.Background()
logger := NewRequestLoggerFromZerolog(ctx, zerolog.New(os.Stdout))
assert.NotNil(t, logger)
}
func TestNewRequestLoggerFromZeroLogCtxWithValue(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, keys.RequestIdCtxKey, "test")
logger := NewRequestLoggerFromZerolog(ctx, zerolog.New(os.Stdout))
assert.NotNil(t, logger)
}
func TestNewChildLoggerRequestLoggerFromZeroLog(t *testing.T) {
ctx := context.Background()
logger := NewRequestLoggerFromZerolog(ctx, zerolog.New(os.Stdout))
assert.NotNil(t, logger)
childLogger := logger.ChildLogger("child", "my-child")
assert.NotNil(t, childLogger)
}
func TestNewWithUnknownLogLevel(t *testing.T) {
logger, err := New(Config{Level: "unknown"})
assert.Nil(t, logger)
assert.Error(t, err)
}
func TestComponentLogger(t *testing.T) {
cfg := DefaultConfig()
cfg.NoJSON = true
logger, _ := New(cfg)
componentLogger := logger.ComponentLogger("my-component")
assert.NotNil(t, componentLogger)
componentLogger.Level(1).Debug().Msg("test")
componentLogger.Logr().Info("test")
}
func TestChildLoggerWithAttributes(t *testing.T) {
logger, _ := New(DefaultConfig())
_, err := logger.ChildLoggerWithAttributes("key", "value")
assert.NoError(t, err)
}
func TestMustChildLoggerWithAttributes(t *testing.T) {
logger, _ := New(DefaultConfig())
loggerAttributes := logger.MustChildLoggerWithAttributes("key", "value")
assert.NotNil(t, loggerAttributes)
}