Skip to content

Commit

Permalink
Add the InitDefaultLogTimeFormat function to set the global default l…
Browse files Browse the repository at this point in the history
…og time format (#18)
  • Loading branch information
mstmdev authored Jun 27, 2023
1 parent f80637a commit c702c1c
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 28 deletions.
4 changes: 2 additions & 2 deletions base_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions console_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 15 additions & 4 deletions content/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
37 changes: 27 additions & 10 deletions content/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

}
4 changes: 2 additions & 2 deletions file_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion formatter/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion formatter/text/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -14,7 +13,7 @@ import (
var (
concurrencyCount = 3
concurrencyTimeout = time.Second * 5
testTimeFormat = content.DefaultLogTimeFormat
testTimeFormat = time.RFC3339
)

func testLogs(t *testing.T) {
Expand Down
5 changes: 1 addition & 4 deletions multi_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package log
import (
"errors"
"testing"
"time"

"github.com/no-src/log/formatter"
"github.com/no-src/log/level"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c702c1c

Please sign in to comment.