-
Notifications
You must be signed in to change notification settings - Fork 41
/
log_test.go
86 lines (68 loc) · 1.89 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
package logger
import (
"os"
"path/filepath"
"testing"
)
var old = os.Stdout
var fname = filepath.Join(os.TempDir(), "stdout")
func TestDebugShoudWiteLogInJsonFormat(t *testing.T) {
temp := setStdout()
defer temp.Close()
Debug("log debug message")
got, _ := os.ReadFile(fname)
want := "{\"logLevel\":\"debug\",\"message\":\"log debug message\"}\n"
if want != string(got) {
t.Errorf("Expected %s to be => %s", want, got)
}
os.Stdout = old
}
func TestDebugfShoudWiteLogInJsonFormat(t *testing.T) {
temp := setStdout()
defer temp.Close()
Debugf("log %s debug message", "formatted")
got, _ := os.ReadFile(fname)
want := "{\"logLevel\":\"debug\",\"message\":\"log formatted debug message\"}\n"
if want != string(got) {
t.Errorf("Expected %s to be => %s", want, got)
}
os.Stdout = old
}
func TestInfoShoudWiteLogInJsonFormat(t *testing.T) {
temp := setStdout()
defer temp.Close()
Info("log info message")
got, _ := os.ReadFile(fname)
want := "{\"logLevel\":\"info\",\"message\":\"log info message\"}\n"
if want != string(got) {
t.Errorf("Expected %s to be => %s", want, got)
}
os.Stdout = old
}
func TestInfofShoudWiteLogInJsonFormat(t *testing.T) {
temp := setStdout()
defer temp.Close()
Infof("log %s info message", "formatted")
got, _ := os.ReadFile(fname)
want := "{\"logLevel\":\"info\",\"message\":\"log formatted info message\"}\n"
if want != string(got) {
t.Errorf("Expected %s to be => %s", want, got)
}
os.Stdout = old
}
func TestWarnfShoudWiteLogInJsonFormat(t *testing.T) {
temp := setStdout()
defer temp.Close()
Warnf("log %s warning message", "formatted")
got, _ := os.ReadFile(fname)
want := "{\"logLevel\":\"warning\",\"message\":\"log formatted warning message\"}\n"
if want != string(got) {
t.Errorf("Expected %s to be => %s", want, got)
}
os.Stdout = old
}
func setStdout() *os.File {
temp, _ := os.Create(fname)
os.Stdout = temp
return temp
}