diff --git a/base_logger.go b/base_logger.go index 8b685b6..d650209 100644 --- a/base_logger.go +++ b/base_logger.go @@ -67,7 +67,7 @@ func (l *baseLogger) init(w Writer, lvl level.Level, appendTime bool) { l.lvl = lvl l.f = formatter.Default() l.appendTime = appendTime - l.setTimeFormat(content.DefaultLogTimeFormat) + l.setTimeFormat(content.DefaultLogTimeFormat()) } func (l *baseLogger) setFormatter(f formatter.Formatter) { @@ -78,7 +78,7 @@ func (l *baseLogger) setFormatter(f formatter.Formatter) { func (l *baseLogger) setTimeFormat(f string) { if len(f) == 0 { - f = content.DefaultLogTimeFormat + f = content.DefaultLogTimeFormat() } l.timeFormat = f } diff --git a/console_logger_test.go b/console_logger_test.go index 9df90e8..3b9a642 100644 --- a/console_logger_test.go +++ b/console_logger_test.go @@ -15,8 +15,8 @@ func TestConsoleLogger(t *testing.T) { timeFormat string }{ {"TextFormatter", formatter.TextFormatter, false, testTimeFormat}, - {"JsonFormatter", formatter.JsonFormatter, false, ""}, - {"TextFormatter Concurrency", formatter.TextFormatter, true, testTimeFormat}, + {"JsonFormatter", formatter.JsonFormatter, false, testTimeFormat}, + {"TextFormatter Concurrency", formatter.TextFormatter, true, ""}, {"JsonFormatter Concurrency", formatter.JsonFormatter, true, ""}, } for _, tc := range testCases { diff --git a/content/time.go b/content/time.go index 0738da0..bdb0cda 100644 --- a/content/time.go +++ b/content/time.go @@ -2,9 +2,8 @@ package content import "time" -const ( - // DefaultLogTimeFormat the default log time format - DefaultLogTimeFormat = "2006-01-02 15:04:05" +var ( + defaultLogTimeFormat = "2006-01-02 15:04:05" ) // Time the custom Time for log @@ -13,9 +12,21 @@ type Time struct { format string } +// InitDefaultLogTimeFormat init the global default log time format +func InitDefaultLogTimeFormat(f string) { + if len(f) > 0 { + defaultLogTimeFormat = f + } +} + +// DefaultLogTimeFormat return the default log time format +func DefaultLogTimeFormat() string { + return defaultLogTimeFormat +} + // NewTime convert time.Time to content.Time pointer with default format func NewTime(time time.Time) *Time { - return NewTimeWithFormat(time, DefaultLogTimeFormat) + return NewTimeWithFormat(time, DefaultLogTimeFormat()) } // NewTimeWithFormat convert time.Time to content.Time pointer with custom format diff --git a/content/time_test.go b/content/time_test.go index 2af9633..288d7ac 100644 --- a/content/time_test.go +++ b/content/time_test.go @@ -6,16 +6,33 @@ import ( ) func TestTime_MarshalText(t *testing.T) { - now := time.Now() - expect := now.Format(DefaultLogTimeFormat) - ti := NewTime(time.Now()) - data, err := ti.MarshalText() - if err != nil { - t.Errorf("Time.MarshalText error => %v", err) - return + testCases := []struct { + name string + format string + }{ + {"default", ""}, + {"RFC3339", time.RFC3339}, + {"RFC3339Nano", time.RFC3339Nano}, } - actual := string(data) - if expect != actual { - t.Errorf("test Time.MarshalText failed, expect to get %s, but actual get %s", expect, actual) + for _, tc := range testCases { + now := time.Now() + if len(tc.format) > 0 { + InitDefaultLogTimeFormat(tc.format) + } else { + tc.format = DefaultLogTimeFormat() + } + + expect := now.Format(tc.format) + ti := NewTime(now) + data, err := ti.MarshalText() + if err != nil { + t.Errorf("Time.MarshalText error => %v", err) + return + } + actual := string(data) + if expect != actual { + t.Errorf("test Time.MarshalText failed, expect to get %s, but actual get %s", expect, actual) + } } + } diff --git a/file_logger_test.go b/file_logger_test.go index 891c058..00c7684 100644 --- a/file_logger_test.go +++ b/file_logger_test.go @@ -18,8 +18,8 @@ func TestFileLogger(t *testing.T) { timeFormat string }{ {"TextFormatter", formatter.TextFormatter, false, testTimeFormat}, - {"JsonFormatter", formatter.JsonFormatter, false, ""}, - {"TextFormatter Concurrency", formatter.TextFormatter, true, testTimeFormat}, + {"JsonFormatter", formatter.JsonFormatter, false, testTimeFormat}, + {"TextFormatter Concurrency", formatter.TextFormatter, true, ""}, {"JsonFormatter Concurrency", formatter.JsonFormatter, true, ""}, } for _, tc := range testCases { diff --git a/formatter/json/json_test.go b/formatter/json/json_test.go index a20aa2d..5e46828 100644 --- a/formatter/json/json_test.go +++ b/formatter/json/json_test.go @@ -10,7 +10,7 @@ import ( ) func TestJsonFormatter_Serialize(t *testing.T) { - logTime, _ := time.ParseInLocation(content.DefaultLogTimeFormat, "2022-06-25 23:59:59", time.UTC) + logTime, _ := time.ParseInLocation("2006-01-02 15:04:05", "2022-06-25 23:59:59", time.UTC) logTimeP := content.NewTime(logTime) testCases := []struct { diff --git a/formatter/text/text_test.go b/formatter/text/text_test.go index f3c7ccb..624cf24 100644 --- a/formatter/text/text_test.go +++ b/formatter/text/text_test.go @@ -10,7 +10,7 @@ import ( ) func TestTextFormatter_Serialize(t *testing.T) { - logTime, _ := time.ParseInLocation(content.DefaultLogTimeFormat, "2022-06-25 23:59:59", time.UTC) + logTime, _ := time.ParseInLocation("2006-01-02 15:04:05", "2022-06-25 23:59:59", time.UTC) logTimeP := content.NewTime(logTime) testCases := []struct { diff --git a/logger_test.go b/logger_test.go index a23f10d..4fbf6e2 100644 --- a/logger_test.go +++ b/logger_test.go @@ -5,7 +5,6 @@ import ( "testing" "time" - "github.com/no-src/log/content" "github.com/no-src/log/formatter" "github.com/no-src/log/internal/sync" "github.com/no-src/log/level" @@ -14,7 +13,7 @@ import ( var ( concurrencyCount = 3 concurrencyTimeout = time.Second * 5 - testTimeFormat = content.DefaultLogTimeFormat + testTimeFormat = time.RFC3339 ) func testLogs(t *testing.T) { diff --git a/multi_logger_test.go b/multi_logger_test.go index 93c15dd..0e0d1ac 100644 --- a/multi_logger_test.go +++ b/multi_logger_test.go @@ -3,7 +3,6 @@ package log import ( "errors" "testing" - "time" "github.com/no-src/log/formatter" "github.com/no-src/log/level" @@ -49,9 +48,7 @@ func TestMultiLogger_WithTimeFormat(t *testing.T) { format string }{ {"empty", ""}, - {"default", testTimeFormat}, - {"RFC3339", time.RFC3339}, - {"RFC3339Nano", time.RFC3339Nano}, + {"customized", testTimeFormat}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) {