Skip to content

Commit c702c1c

Browse files
authored
Add the InitDefaultLogTimeFormat function to set the global default log time format (#18)
1 parent f80637a commit c702c1c

File tree

9 files changed

+52
-28
lines changed

9 files changed

+52
-28
lines changed

base_logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (l *baseLogger) init(w Writer, lvl level.Level, appendTime bool) {
6767
l.lvl = lvl
6868
l.f = formatter.Default()
6969
l.appendTime = appendTime
70-
l.setTimeFormat(content.DefaultLogTimeFormat)
70+
l.setTimeFormat(content.DefaultLogTimeFormat())
7171
}
7272

7373
func (l *baseLogger) setFormatter(f formatter.Formatter) {
@@ -78,7 +78,7 @@ func (l *baseLogger) setFormatter(f formatter.Formatter) {
7878

7979
func (l *baseLogger) setTimeFormat(f string) {
8080
if len(f) == 0 {
81-
f = content.DefaultLogTimeFormat
81+
f = content.DefaultLogTimeFormat()
8282
}
8383
l.timeFormat = f
8484
}

console_logger_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ func TestConsoleLogger(t *testing.T) {
1515
timeFormat string
1616
}{
1717
{"TextFormatter", formatter.TextFormatter, false, testTimeFormat},
18-
{"JsonFormatter", formatter.JsonFormatter, false, ""},
19-
{"TextFormatter Concurrency", formatter.TextFormatter, true, testTimeFormat},
18+
{"JsonFormatter", formatter.JsonFormatter, false, testTimeFormat},
19+
{"TextFormatter Concurrency", formatter.TextFormatter, true, ""},
2020
{"JsonFormatter Concurrency", formatter.JsonFormatter, true, ""},
2121
}
2222
for _, tc := range testCases {

content/time.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package content
22

33
import "time"
44

5-
const (
6-
// DefaultLogTimeFormat the default log time format
7-
DefaultLogTimeFormat = "2006-01-02 15:04:05"
5+
var (
6+
defaultLogTimeFormat = "2006-01-02 15:04:05"
87
)
98

109
// Time the custom Time for log
@@ -13,9 +12,21 @@ type Time struct {
1312
format string
1413
}
1514

15+
// InitDefaultLogTimeFormat init the global default log time format
16+
func InitDefaultLogTimeFormat(f string) {
17+
if len(f) > 0 {
18+
defaultLogTimeFormat = f
19+
}
20+
}
21+
22+
// DefaultLogTimeFormat return the default log time format
23+
func DefaultLogTimeFormat() string {
24+
return defaultLogTimeFormat
25+
}
26+
1627
// NewTime convert time.Time to content.Time pointer with default format
1728
func NewTime(time time.Time) *Time {
18-
return NewTimeWithFormat(time, DefaultLogTimeFormat)
29+
return NewTimeWithFormat(time, DefaultLogTimeFormat())
1930
}
2031

2132
// NewTimeWithFormat convert time.Time to content.Time pointer with custom format

content/time_test.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,33 @@ import (
66
)
77

88
func TestTime_MarshalText(t *testing.T) {
9-
now := time.Now()
10-
expect := now.Format(DefaultLogTimeFormat)
11-
ti := NewTime(time.Now())
12-
data, err := ti.MarshalText()
13-
if err != nil {
14-
t.Errorf("Time.MarshalText error => %v", err)
15-
return
9+
testCases := []struct {
10+
name string
11+
format string
12+
}{
13+
{"default", ""},
14+
{"RFC3339", time.RFC3339},
15+
{"RFC3339Nano", time.RFC3339Nano},
1616
}
17-
actual := string(data)
18-
if expect != actual {
19-
t.Errorf("test Time.MarshalText failed, expect to get %s, but actual get %s", expect, actual)
17+
for _, tc := range testCases {
18+
now := time.Now()
19+
if len(tc.format) > 0 {
20+
InitDefaultLogTimeFormat(tc.format)
21+
} else {
22+
tc.format = DefaultLogTimeFormat()
23+
}
24+
25+
expect := now.Format(tc.format)
26+
ti := NewTime(now)
27+
data, err := ti.MarshalText()
28+
if err != nil {
29+
t.Errorf("Time.MarshalText error => %v", err)
30+
return
31+
}
32+
actual := string(data)
33+
if expect != actual {
34+
t.Errorf("test Time.MarshalText failed, expect to get %s, but actual get %s", expect, actual)
35+
}
2036
}
37+
2138
}

file_logger_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ func TestFileLogger(t *testing.T) {
1818
timeFormat string
1919
}{
2020
{"TextFormatter", formatter.TextFormatter, false, testTimeFormat},
21-
{"JsonFormatter", formatter.JsonFormatter, false, ""},
22-
{"TextFormatter Concurrency", formatter.TextFormatter, true, testTimeFormat},
21+
{"JsonFormatter", formatter.JsonFormatter, false, testTimeFormat},
22+
{"TextFormatter Concurrency", formatter.TextFormatter, true, ""},
2323
{"JsonFormatter Concurrency", formatter.JsonFormatter, true, ""},
2424
}
2525
for _, tc := range testCases {

formatter/json/json_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestJsonFormatter_Serialize(t *testing.T) {
13-
logTime, _ := time.ParseInLocation(content.DefaultLogTimeFormat, "2022-06-25 23:59:59", time.UTC)
13+
logTime, _ := time.ParseInLocation("2006-01-02 15:04:05", "2022-06-25 23:59:59", time.UTC)
1414
logTimeP := content.NewTime(logTime)
1515

1616
testCases := []struct {

formatter/text/text_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestTextFormatter_Serialize(t *testing.T) {
13-
logTime, _ := time.ParseInLocation(content.DefaultLogTimeFormat, "2022-06-25 23:59:59", time.UTC)
13+
logTime, _ := time.ParseInLocation("2006-01-02 15:04:05", "2022-06-25 23:59:59", time.UTC)
1414
logTimeP := content.NewTime(logTime)
1515

1616
testCases := []struct {

logger_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"testing"
66
"time"
77

8-
"github.com/no-src/log/content"
98
"github.com/no-src/log/formatter"
109
"github.com/no-src/log/internal/sync"
1110
"github.com/no-src/log/level"
@@ -14,7 +13,7 @@ import (
1413
var (
1514
concurrencyCount = 3
1615
concurrencyTimeout = time.Second * 5
17-
testTimeFormat = content.DefaultLogTimeFormat
16+
testTimeFormat = time.RFC3339
1817
)
1918

2019
func testLogs(t *testing.T) {

multi_logger_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package log
33
import (
44
"errors"
55
"testing"
6-
"time"
76

87
"github.com/no-src/log/formatter"
98
"github.com/no-src/log/level"
@@ -49,9 +48,7 @@ func TestMultiLogger_WithTimeFormat(t *testing.T) {
4948
format string
5049
}{
5150
{"empty", ""},
52-
{"default", testTimeFormat},
53-
{"RFC3339", time.RFC3339},
54-
{"RFC3339Nano", time.RFC3339Nano},
51+
{"customized", testTimeFormat},
5552
}
5653
for _, tc := range testCases {
5754
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)