Skip to content

Commit 8813a22

Browse files
committed
test case additions
1 parent 1ac2835 commit 8813a22

File tree

2 files changed

+137
-16
lines changed

2 files changed

+137
-16
lines changed

log_test.go

Lines changed: 136 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,38 @@ package log
66

77
import (
88
"fmt"
9+
"os"
10+
"path/filepath"
911
"strings"
1012
"testing"
1113
)
1214

1315
func TestDefaultStandardLogger(t *testing.T) {
16+
SetLevel(LevelInfo)
17+
_ = SetPattern("%time:2006-01-02 15:04:05.000 %level:-5 %line %custom:- %message")
1418
Trace("I shoudn't see this msg, because standard logger level is DEBUG")
1519
Debug("I would like to see this message, debug is useful for dev")
1620
Info("Yes, I would love to")
1721
Warn("Yes, yes it's an warning")
1822
Error("Yes, yes, yes - finally an error")
23+
fmt.Println()
1924

20-
fmt.Printf("First round: %#v\n\n", Stats())
25+
t.Logf("First round: %#v\n\n", Stats())
2126

27+
SetLevel(LevelDebug)
28+
_ = SetPattern("%time:2006-01-02 15:04:05.000 %level:-5 %shortfile %line %custom:- %message")
2229
Tracef("I shoudn't see this msg: %v", 4)
2330
Debugf("I would like to see this message, debug is useful for dev: %v", 3)
2431
Infof("Yes, I would love to: %v", 2)
2532
Warnf("Yes, yes it's an warning: %v", 1)
2633
Errorf("Yes, yes, yes - finally an error: %v", 0)
2734

28-
fmt.Printf("Second round: %#v\n\n", Stats())
35+
t.Logf("Second round: %#v\n\n", Stats())
36+
37+
err := SetPattern("%level:-5 %shortfile %line %unknown")
38+
if err == nil {
39+
t.Error("Expected error got nil")
40+
}
2941
}
3042

3143
func TestNewCustomUTCConsoleReceiver(t *testing.T) {
@@ -38,7 +50,7 @@ receiver = "console"
3850
level = "debug"
3951
4052
# if not suppiled then default pattern is used
41-
pattern = "%utctime:2006-01-02 15:04:05.000 %level:-5 %shortfile:-25 %custom:- %message"
53+
pattern = "%utctime:2006-01-02 15:04:05.000 %level:-5 %line %shortfile:-25 %custom:- %message"
4254
`
4355
logger, err := New(config)
4456
if err != nil {
@@ -53,9 +65,9 @@ pattern = "%utctime:2006-01-02 15:04:05.000 %level:-5 %shortfile:-25 %custom:- %
5365
logger.Error("Yes, yes, yes - finally an error")
5466

5567
stats := logger.Stats()
56-
fmt.Printf("First round: %#v\n\n", stats)
57-
if stats.bytes != 417 {
58-
t.Errorf("Expected: 417, got: %v", stats.bytes)
68+
t.Logf("First round: %#v\n", stats)
69+
if stats.bytes != 433 {
70+
t.Errorf("Expected: 433, got: %v\n", stats.bytes)
5971
}
6072

6173
logger.Tracef("I shoudn't see this msg, because standard logger level is DEBUG: %v", 4)
@@ -65,18 +77,18 @@ pattern = "%utctime:2006-01-02 15:04:05.000 %level:-5 %shortfile:-25 %custom:- %
6577
logger.Errorf("Yes, yes, yes - finally an error: %v", 0)
6678

6779
stats = logger.Stats()
68-
fmt.Printf("Second round: %#v\n\n", stats)
69-
if stats.bytes != 846 {
70-
t.Errorf("Expected: 846, got: %v", stats.bytes)
80+
t.Logf("Second round: %#v\n", stats)
81+
if stats.bytes != 878 {
82+
t.Errorf("Expected: 878, got: %v\n", stats.bytes)
7183
}
7284

7385
Tracef("I shoudn't see this msg: %v", 46583)
7486
Debugf("I would like to see this message, debug is useful for dev: %v", 334545)
7587

7688
stats = logger.Stats()
77-
fmt.Printf("Third round: %#v\n\n", stats)
78-
if stats.bytes != 846 {
79-
t.Errorf("Expected: 846, got: %v", stats.bytes)
89+
t.Logf("Third round: %#v\n", stats)
90+
if stats.bytes != 878 {
91+
t.Errorf("Expected: 878, got: %v\n", stats.bytes)
8092
}
8193
}
8294

@@ -99,7 +111,7 @@ receiver = "CONSOLE"
99111
logger.Error("Yes, yes, yes - finally an error")
100112

101113
stats := logger.Stats()
102-
fmt.Printf("First round: %#v\n\n", stats)
114+
t.Logf("First round: %#v\n", stats)
103115
if stats.bytes != 313 {
104116
t.Errorf("Expected: 313, got: %v", stats.bytes)
105117
}
@@ -111,7 +123,7 @@ receiver = "CONSOLE"
111123
logger.Errorf("Yes, yes, yes - finally an error: %v", 0)
112124

113125
stats = logger.Stats()
114-
fmt.Printf("Second round: %#v\n\n", stats)
126+
t.Logf("Second round: %#v\n", stats)
115127
if stats.bytes != 638 {
116128
t.Errorf("Expected: 638, got: %v", stats.bytes)
117129
}
@@ -120,13 +132,71 @@ receiver = "CONSOLE"
120132
Debugf("I would like to see this message, debug is useful for dev: %v", 334545)
121133

122134
stats = logger.Stats()
123-
fmt.Printf("Third round: %#v\n\n", stats)
135+
t.Logf("Third round: %#v\n", stats)
124136
if stats.bytes != 638 {
125137
t.Errorf("Expected: 638, got: %v", stats.bytes)
126138
}
127139
}
128140

141+
func TestNewCustomFileReceiverDailyRotation(t *testing.T) {
142+
defer cleaupFiles("*.log")
143+
144+
fileLoggerConfig := `
145+
# file logger configuration
146+
# "FILE" uppercasse works too
147+
receiver = "file"
148+
149+
# "debug" lowercase works too and if not supplied then defaults to DEBUG
150+
level = "info"
151+
152+
# if not suppiled then default pattern is used
153+
pattern = "%utctime:2006-01-02 15:04:05.000 %level:-5 %longfile %line %custom:- %message"
154+
155+
file = "daily-aah-filename.log"
156+
157+
rotate {
158+
mode = "daily"
159+
}
160+
`
161+
162+
logger, err := New(fileLoggerConfig)
163+
if err != nil {
164+
t.Errorf("Unexpected error: %v", err)
165+
t.FailNow()
166+
}
167+
168+
for i := 0; i < 25; i++ {
169+
logger.Trace("I shoudn't see this msg, because standard logger level is DEBUG")
170+
logger.Debug("I would like to see this message, debug is useful for dev")
171+
logger.Info("Yes, I would love to")
172+
logger.Warn("Yes, yes it's an warning")
173+
logger.Error("Yes, yes, yes - finally an error")
174+
}
175+
176+
_ = logger.SetPattern("%time:2006-01-02 15:04:05.000 %level:-5 %longfile %line %custom:- %message")
177+
for i := 0; i < 25; i++ {
178+
logger.Tracef("I shoudn't see this msg, because standard logger level is DEBUG: %v", 4)
179+
logger.Debugf("I would like to see this message, debug is useful for dev: %v", 3)
180+
logger.Infof("Yes, I would love to: %v", 2)
181+
logger.Warnf("Yes, yes it's an warning: %v", 1)
182+
logger.Errorf("Yes, yes, yes - finally an error: %v", 0)
183+
}
184+
185+
// Close scenario
186+
logger.Close()
187+
if !logger.Closed() {
188+
t.Errorf("Expected 'true', got %v", logger.Closed())
189+
}
190+
191+
logger.Info("This won't be written to file")
192+
193+
// once again
194+
logger.Close()
195+
}
196+
129197
func TestNewCustomFileReceiverLinesRotation(t *testing.T) {
198+
defer cleaupFiles("*.log")
199+
130200
fileLoggerConfig := `
131201
# file logger configuration
132202
# "FILE" uppercasse works too
@@ -173,6 +243,8 @@ rotate {
173243
}
174244

175245
func TestNewCustomFileReceiverSizeRotation(t *testing.T) {
246+
defer cleaupFiles("*.log")
247+
176248
fileLoggerConfig := `
177249
# file logger configuration
178250
# "FILE" uppercasse works too
@@ -267,3 +339,52 @@ func TestNewMisc(t *testing.T) {
267339
t.FailNow()
268340
}
269341
}
342+
343+
func TestLevelUnknown(t *testing.T) {
344+
var level Level
345+
if level.String() != "ERROR" {
346+
t.Errorf("Expected level 'ERROR', got '%v'", level)
347+
}
348+
349+
level = 9 // Unknown log level
350+
if level.String() != "Unknown" {
351+
t.Errorf("Expected level 'Unknown', got '%v'", level)
352+
}
353+
}
354+
355+
func TestStats(t *testing.T) {
356+
stats := ReceiverStats{
357+
lines: 200,
358+
bytes: 764736,
359+
}
360+
361+
if stats.Bytes() != 764736 {
362+
t.Errorf("Expected '764736' bytes, got '%v' bytes", stats.Bytes())
363+
}
364+
365+
if stats.Lines() != 200 {
366+
t.Errorf("Expected '200' lines, got '%v' lines", stats.Lines())
367+
}
368+
}
369+
370+
func cleaupFiles(match string) {
371+
pwd, _ := os.Getwd()
372+
373+
dir, err := os.Open(pwd)
374+
if err != nil {
375+
return
376+
}
377+
378+
infos, err := dir.Readdir(-1)
379+
if err != nil {
380+
return
381+
}
382+
383+
for _, info := range infos {
384+
if !info.IsDir() {
385+
if found, _ := filepath.Match(match, info.Name()); found {
386+
_ = os.Remove(filepath.Join(pwd, info.Name()))
387+
}
388+
}
389+
}
390+
}

stats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2016 Jeevanandam M (https://github.com/jeevatkm)
2-
// resty source code and usage is governed by a MIT style
2+
// go-aah/log source code and usage is governed by a MIT style
33
// license that can be found in the LICENSE file.
44

55
package log

0 commit comments

Comments
 (0)