diff --git a/internal/default.go b/internal/default.go index 4f48e7f..c0c554b 100644 --- a/internal/default.go +++ b/internal/default.go @@ -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 ) diff --git a/log.go b/log.go index 713168a..8bf8a24 100644 --- a/log.go +++ b/log.go @@ -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, } } @@ -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, diff --git a/logger.go b/logger.go index 68ec008..7d880af 100644 --- a/logger.go +++ b/logger.go @@ -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 @@ -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) diff --git a/logger_test.go b/logger_test.go index a942d80..1134f1b 100644 --- a/logger_test.go +++ b/logger_test.go @@ -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")