This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
log_test.go
154 lines (128 loc) · 2.66 KB
/
log_test.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//go:build !windows
// +build !windows
package well
import (
"bytes"
"encoding/json"
"flag"
"io"
"os"
"syscall"
"testing"
"time"
"github.com/BurntSushi/toml"
"github.com/cybozu-go/log"
)
func TestLogConfig(t *testing.T) {
t.Parallel()
var c1, c2 struct {
Log LogConfig `toml:"log" json:"log"`
}
_, err := toml.DecodeFile("testdata/log.toml", &c1)
if err != nil {
t.Fatal(err)
}
if c1.Log.Filename != "/abc/def" {
t.Error(`c1.Log.Filename != "/abc/def"`)
}
if c1.Log.Level != "debug" {
t.Error(`c1.Log.Level != "debug"`)
}
if c1.Log.Format != "json" {
t.Error(`c1.Log.Format != "json"`)
}
f, err := os.Open("testdata/log.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
err = json.NewDecoder(f).Decode(&c2)
if err != nil {
t.Fatal(err)
}
if c2.Log.Filename != "/abc/def" {
t.Error(`c2.Log.Filename != "/abc/def"`)
}
if c2.Log.Level != "debug" {
t.Error(`c2.Log.Level != "debug"`)
}
if c2.Log.Format != "json" {
t.Error(`c2.Log.Format != "json"`)
}
}
func TestLogConfigApply(t *testing.T) {
t.Parallel()
c := &LogConfig{
Filename: "",
Level: "info",
Format: "json",
}
err := c.Apply()
if err != nil {
t.Fatal(err)
}
logger := log.DefaultLogger()
if logger.Threshold() != log.LvInfo {
t.Error(`logger.Threshold() != log.LvInfo`)
}
if logger.Formatter().String() != "json" {
t.Error(`logger.Formatter().String() != "json"`)
}
c.Format = "bad_format"
err = c.Apply()
if err == nil {
t.Error(c.Format + " should cause an error")
}
c.Level = "bad_level"
c.Format = "json"
err = c.Apply()
if err == nil {
t.Error(c.Level + " should cause an error")
}
}
func TestLogFlags(t *testing.T) {
t.Parallel()
t.Skip("this test redirects log outputs to a temp file.")
c := &LogConfig{
Filename: "",
Level: "info",
Format: "json",
}
f, err := os.CreateTemp("", "gotest")
if err != nil {
t.Fatal(err)
}
f.Close()
defer os.Remove(f.Name())
flag.Set("logfile", f.Name())
flag.Set("loglevel", "debug")
flag.Set("logformat", "plain")
err = c.Apply()
if err != nil {
t.Fatal(err)
}
logger := log.DefaultLogger()
if logger.Threshold() != log.LvDebug {
t.Error(`logger.Threshold() != log.LvDebug`)
}
if logger.Formatter().String() != "plain" {
t.Error(`logger.Formatter().String() != "plain"`)
}
err = log.Critical("hoge fuga", nil)
if err != nil {
t.Fatal(err)
}
syscall.Kill(os.Getpid(), syscall.SIGUSR1)
time.Sleep(10 * time.Millisecond)
g, err := os.Open(f.Name())
if err != nil {
t.Fatal(err)
}
data, err := io.ReadAll(g)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(data, []byte("hoge fuga")) {
t.Error(`!bytes.Contains(data, []byte("hoge fuga"))`)
}
}