-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_test.go
102 lines (86 loc) · 3.07 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
package log_test
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/johandry/log"
"github.com/mgutz/ansi"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// removeTimestamp remove the timestamp form the log entry and the trailling
// newline character.
func removeTimestamp(logMessage string) string {
noNewLine := strings.TrimSuffix(logMessage, "\n")
noTimeStamp := strings.Split(noNewLine, "]")[1]
noResetColor := strings.TrimPrefix(noTimeStamp, ansi.Reset)
return noResetColor
}
func newLogger(b *bytes.Buffer, v *viper.Viper) *log.Logger {
v.Set(log.OutputKey, b)
l := log.New(v)
return l
}
func TestConfigWithViperNoColor(t *testing.T) {
var expectedLogMessage string
var actualLogMessage string
var b bytes.Buffer
v := viper.New()
v.Set(log.ForceColorsKey, false)
v.Set(log.LevelKey, "debug")
l := newLogger(&b, v)
l.Prefix("test").WithFields(logrus.Fields{"key": "value", "env": "test testing"}).Info("Information")
expectedLogMessage = " INFO Test: Information env=\"test testing\" key=value"
actualLogMessage = removeTimestamp(b.String())
if actualLogMessage != expectedLogMessage {
t.Errorf("Expected '%s', but got '%s'", expectedLogMessage, actualLogMessage)
}
b.Reset()
l.Prefix("test").Warn("Warning")
expectedLogMessage = " WARN Test: Warning"
actualLogMessage = removeTimestamp(b.String())
if actualLogMessage != expectedLogMessage {
t.Errorf("Expected '%s', but got '%s'", expectedLogMessage, actualLogMessage)
}
b.Reset()
l.Prefix("test").Error("Error")
expectedLogMessage = " ERROR Test: Error"
actualLogMessage = removeTimestamp(b.String())
if actualLogMessage != expectedLogMessage {
t.Errorf("Expected '%s', but got '%s'", expectedLogMessage, actualLogMessage)
}
b.Reset()
}
func TestConfigWithViperColor(t *testing.T) {
var b bytes.Buffer
var expectedLogMessage string
var actualLogMessage string
v := viper.New()
v.Set(log.OutputKey, &b)
v.Set(log.ForceColorsKey, true)
v.Set(log.LevelKey, "debug")
l := log.New(v)
// l.Prefix("test").WithFields(logrus.Fields{"key": "value", "env": "test testing"}).Info("Information")
// expectedLogMessage = fmt.Sprintf(" %sINFO%s Test: Information %senv%s=\"test testing\" %skey%s=value", ansi.Green, ansi.Reset, ansi.Green, ansi.Reset, ansi.Green, ansi.Reset)
// actualLogMessage = removeTimestamp(b.String())
// if actualLogMessage != expectedLogMessage {
// t.Errorf("Expected '%s', but got '%s'", expectedLogMessage, actualLogMessage)
// }
// b.Reset()
//
// l.Prefix("test").Warn("Warning")
// expectedLogMessage = fmt.Sprintf(" %sWARN%s Test: Warning", ansi.Yellow, ansi.Reset)
// actualLogMessage = removeTimestamp(b.String())
// if actualLogMessage != expectedLogMessage {
// t.Errorf("Expected '%s', but got '%s'", expectedLogMessage, actualLogMessage)
// }
// b.Reset()
l.Prefix("test").Error("Error")
expectedLogMessage = fmt.Sprintf(" %sERROR%s Test: Error", ansi.Red, ansi.Reset)
actualLogMessage = removeTimestamp(b.String())
if actualLogMessage != expectedLogMessage {
t.Errorf("Expected '%s', but got '%s'", expectedLogMessage, actualLogMessage)
}
b.Reset()
}