Skip to content

Commit

Permalink
Support to change the default time format when using the default form…
Browse files Browse the repository at this point in the history
…atter.
  • Loading branch information
edoger committed Nov 16, 2020
1 parent 92ec511 commit c147602
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
3 changes: 3 additions & 0 deletions internal/default.go
Expand Up @@ -37,6 +37,9 @@ var (
// DefaultNowFunc is the default now function for all logger instances.
DefaultNowFunc = time.Now

// DefaultTimeFormat is the default time format for all logger instances.
DefaultTimeFormat = time.RFC3339

// ErrorWriter is used to output logger internal error messages.
ErrorWriter io.Writer = os.Stderr
)
Expand Down
38 changes: 20 additions & 18 deletions log.go
Expand Up @@ -175,28 +175,30 @@ type Log interface {
// The core type defines the collection of shared attributes within the log,
// and each independent Logger shares the same core instance.
type core struct {
name string
level Level
formatter Formatter
writer io.Writer
pool sync.Pool
hooks HookBag
nowFunc func() time.Time
exitFunc func(int)
panicFunc func(string)
name string
level Level
formatter Formatter
writer io.Writer
pool sync.Pool
hooks HookBag
timeFormat string
nowFunc func() time.Time
exitFunc func(int)
panicFunc func(string)
}

// Create a new core instance and bind the logger name.
func newCore(name string) *core {
return &core{
name: name,
level: TraceLevel,
writer: os.Stdout,
pool: sync.Pool{New: func() interface{} { return new(logEntity) }},
hooks: NewHookBag(),
nowFunc: internal.DefaultNowFunc,
exitFunc: internal.DefaultExitFunc,
panicFunc: internal.DefaultPanicFunc,
name: name,
level: TraceLevel,
writer: os.Stdout,
pool: sync.Pool{New: func() interface{} { return new(logEntity) }},
hooks: NewHookBag(),
timeFormat: internal.DefaultTimeFormat,
nowFunc: internal.DefaultNowFunc,
exitFunc: internal.DefaultExitFunc,
panicFunc: internal.DefaultPanicFunc,
}
}

Expand Down Expand Up @@ -287,7 +289,7 @@ func (o *log) log(level Level, message string) {
}
err = json.NewEncoder(&entity.buffer).Encode(map[string]interface{}{
"name": entity.name,
"time": entity.time.Format(time.RFC3339),
"time": entity.time.Format(o.core.timeFormat),
"level": level.String(),
"message": message,
"fields": fields,
Expand Down
15 changes: 15 additions & 0 deletions logger.go
Expand Up @@ -56,6 +56,10 @@ type Logger interface {
// If the given log formatter is nil, we will record the log in JSON format.
SetFormatter(Formatter) Logger

// SetDefaultTimeFormat sets the default log time format for the current logger.
// If the given time format is empty string, internal.DefaultTimeFormat is used.
SetDefaultTimeFormat(string) Logger

// AddHook adds the given log hook to the current logger.
AddHook(Hook) Logger

Expand Down Expand Up @@ -140,6 +144,17 @@ func (o *logger) SetFormatter(formatter Formatter) Logger {
return o
}

// SetDefaultTimeFormat sets the default log time format for the current logger.
// If the given time format is empty string, internal.DefaultTimeFormat is used.
func (o *logger) SetDefaultTimeFormat(format string) Logger {
if format == "" {
o.core.timeFormat = internal.DefaultTimeFormat
} else {
o.core.timeFormat = format
}
return o
}

// AddHook adds the given log hook to the current logger.
func (o *logger) AddHook(hook Hook) Logger {
o.core.hooks.Add(hook)
Expand Down
7 changes: 7 additions & 0 deletions logger_test.go
Expand Up @@ -117,6 +117,13 @@ func TestLogger_SetFormatter(t *testing.T) {
}
}

func TestLogger_SetDefaultTimeFormat(t *testing.T) {
o := New("test")
if o.SetDefaultTimeFormat("2006-01-02 15:04:05") == nil {
t.Fatal("Logger.SetDefaultTimeFormat(): nil")
}
}

func TestLogger_Log(t *testing.T) {
w := new(bytes.Buffer)
o := New("test")
Expand Down

0 comments on commit c147602

Please sign in to comment.