-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdefault_test.go
More file actions
136 lines (108 loc) · 3.13 KB
/
default_test.go
File metadata and controls
136 lines (108 loc) · 3.13 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2025 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logit
import (
"bytes"
"io"
"log/slog"
"testing"
"github.com/FishGoddess/logit/handler"
)
// go test -v -cover -count=1 -test.cpu=1 -run=^TestSetDefault$
func TestSetDefault(t *testing.T) {
logger := NewLogger()
defaultLogger.Store(&logger)
logger = NewLogger()
SetDefault(logger)
gotLogger := defaultLogger.Load()
if gotLogger == nil {
t.Fatal("gotLogger == nil")
}
if *gotLogger != logger {
t.Fatalf("gotLogger %+v != logger %+v", gotLogger, logger)
}
}
// go test -v -cover -count=1 -test.cpu=1 -run=^TestDefault$
func TestDefault(t *testing.T) {
logger := NewLogger()
defaultLogger.Store(&logger)
gotLogger := Default()
if gotLogger != logger {
t.Fatalf("gotLogger %+v != logger %+v", gotLogger, logger)
}
}
// go test -v -cover -count=1 -test.cpu=1 -run=^TestDefaultLogger$
func TestDefaultLogger(t *testing.T) {
handlerName := t.Name()
newHandler := func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
return slog.NewTextHandler(w, opts)
}
handler.Register(handlerName, newHandler)
buffer := bytes.NewBuffer(make([]byte, 0, 1024))
logger := NewLogger(
WithDebugLevel(), WithHandler(handlerName), WithWriter(buffer), WithSource(), WithPID(),
)
SetDefault(logger)
Debug("debug msg", "key1", 1)
Info("info msg", "key2", 2)
Warn("warn msg", "key3", 3)
Error("error msg", "key4", 4)
opts := &slog.HandlerOptions{AddSource: true, Level: slog.LevelDebug}
wantBuffer := bytes.NewBuffer(make([]byte, 0, 1024))
slogLogger := slog.New(newHandler(wantBuffer, opts)).With(keyPID, pid)
slogLogger.Debug("debug msg", "key1", 1)
slogLogger.Info("info msg", "key2", 2)
slogLogger.Warn("warn msg", "key3", 3)
slogLogger.Error("error msg", "key4", 4)
got := removeTimeAndSource(buffer.String())
want := removeTimeAndSource(wantBuffer.String())
if got != want {
t.Fatalf("got %s != want %s", got, want)
}
}
// go test -v -cover -count=1 -test.cpu=1 -run=^TestDefaultLoggerSync$
func TestDefaultLoggerSync(t *testing.T) {
syncer := &testSyncer{
synced: false,
}
logger := &Logger{
syncer: syncer,
}
SetDefault(logger)
Sync()
if !syncer.synced {
t.Fatal("syncer.synced is wrong")
}
}
// go test -v -cover -count=1 -test.cpu=1 -run=^TestDefaultLoggerClose$
func TestDefaultLoggerClose(t *testing.T) {
syncer := &testSyncer{
synced: false,
}
closer := &testCloser{
closed: false,
}
logger := &Logger{
syncer: syncer,
closer: closer,
}
SetDefault(logger)
Close()
if !syncer.synced {
t.Fatal("syncer.synced is wrong")
}
if !closer.closed {
t.Fatal("closer.closed is wrong")
}
}