This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwelog_test.go
72 lines (48 loc) · 1.84 KB
/
welog_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
package welog
import (
"bytes"
"strings"
"testing"
"time"
"github.com/fatih/color"
)
func TestBasicMode(t *testing.T) {
var buf bytes.Buffer
logger := GenerateLoggerBuilder().Build()
logger.SetOutput(&buf)
timestamp := "[" + time.Now().Format("2006-01-02 15:04:05") + "]"
logger.Debug("Debug message")
infoMessage := "Info message"
logger.Info(infoMessage)
warnMessage := "Warn message"
logger.Warn(warnMessage)
errorMessage := "Error message"
logger.Error(errorMessage)
infoLog := timestamp + " " + "[INFO]" + " " + infoMessage + "\n"
warnLog := timestamp + " " + "[WARN]" + " " + warnMessage + "\n"
errorLog := timestamp + " " + "[ERROR]" + " " + errorMessage + "\n"
expected := infoLog + warnLog + errorLog
if !strings.Contains(buf.String(), expected) {
t.Errorf("Log output does not contain expected messages. Got:\n%s\nExpected:\n%s", buf.String(), expected)
}
}
func TestColorfulMode(t *testing.T) {
var buf bytes.Buffer
logger := GenerateLoggerBuilder().WithLogMode(Colorful).Build()
logger.SetOutput(&buf)
timestamp := "[" + time.Now().Format("2006-01-02 15:04:05") + "]"
logger.Debug("Debug message")
infoMessage := "Info message"
logger.Info(infoMessage)
warnMessage := "Warn message"
logger.Warn(warnMessage)
errorMessage := "Error message"
logger.Error(errorMessage)
infoLog := timestamp + " " + "[" + color.New(color.FgGreen).SprintFunc()("INFO") + "]" + " " + infoMessage + "\n"
warnLog := timestamp + " " + "[" + color.New(color.FgYellow).SprintFunc()("WARN") + "]" + " " + warnMessage + "\n"
errorLog := timestamp + " " + "[" + color.New(color.FgRed).SprintFunc()("ERROR") + "]" + " " + errorMessage + "\n"
expected := infoLog + warnLog + errorLog
if !strings.Contains(buf.String(), expected) {
t.Errorf("Log output does not contain expected messages. Got:\n%s\nExpected:\n%s", buf.String(), expected)
}
}