mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 03:05:05 +08:00
update default logging time format using SetTimeFormat (#2611)
This commit is contained in:
parent
dc0467e934
commit
ff6f5ce237
@ -130,21 +130,26 @@ func (l *Logger) print(ctx context.Context, level int, stack string, values ...i
|
||||
|
||||
// Time.
|
||||
timeFormat := ""
|
||||
if l.config.Flags&F_TIME_DATE > 0 {
|
||||
timeFormat += "2006-01-02"
|
||||
}
|
||||
if l.config.Flags&F_TIME_TIME > 0 {
|
||||
if timeFormat != "" {
|
||||
timeFormat += " "
|
||||
if l.config.TimeFormat != "" {
|
||||
timeFormat = l.config.TimeFormat
|
||||
} else {
|
||||
if l.config.Flags&F_TIME_DATE > 0 {
|
||||
timeFormat += "2006-01-02"
|
||||
}
|
||||
timeFormat += "15:04:05"
|
||||
}
|
||||
if l.config.Flags&F_TIME_MILLI > 0 {
|
||||
if timeFormat != "" {
|
||||
timeFormat += " "
|
||||
if l.config.Flags&F_TIME_TIME > 0 {
|
||||
if timeFormat != "" {
|
||||
timeFormat += " "
|
||||
}
|
||||
timeFormat += "15:04:05"
|
||||
}
|
||||
if l.config.Flags&F_TIME_MILLI > 0 {
|
||||
if timeFormat != "" {
|
||||
timeFormat += " "
|
||||
}
|
||||
timeFormat += "15:04:05.000"
|
||||
}
|
||||
timeFormat += "15:04:05.000"
|
||||
}
|
||||
|
||||
if len(timeFormat) > 0 {
|
||||
input.TimeFormat = now.Format(timeFormat)
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ type Config struct {
|
||||
Handlers []Handler `json:"-"` // Logger handlers which implement feature similar as middleware.
|
||||
Writer io.Writer `json:"-"` // Customized io.Writer.
|
||||
Flags int `json:"flags"` // Extra flags for logging output features.
|
||||
TimeFormat string `json:"timeFormat"` // Logging time format
|
||||
Path string `json:"path"` // Logging directory path.
|
||||
File string `json:"file"` // Format pattern for logging file.
|
||||
Level int `json:"level"` // Output level.
|
||||
@ -58,6 +59,7 @@ func DefaultConfig() Config {
|
||||
c := Config{
|
||||
File: defaultFileFormat,
|
||||
Flags: F_TIME_STD,
|
||||
TimeFormat: "",
|
||||
Level: LEVEL_ALL,
|
||||
CtxKeys: []interface{}{},
|
||||
StStatus: 1,
|
||||
@ -249,6 +251,11 @@ func (l *Logger) SetFile(pattern string) {
|
||||
l.config.File = pattern
|
||||
}
|
||||
|
||||
// SetTimeFormat sets the time format for the logging time.
|
||||
func (l *Logger) SetTimeFormat(timeFormat string) {
|
||||
l.config.TimeFormat = timeFormat
|
||||
}
|
||||
|
||||
// SetStdoutPrint sets whether output the logging contents to stdout, which is true in default.
|
||||
func (l *Logger) SetStdoutPrint(enabled bool) {
|
||||
l.config.StdoutPrint = enabled
|
||||
|
@ -10,8 +10,10 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
@ -89,6 +91,26 @@ func Test_SetFile(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SetTimeFormat(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
w := bytes.NewBuffer(nil)
|
||||
l := glog.NewWithWriter(w)
|
||||
|
||||
l.SetTimeFormat("2006-01-02T15:04:05.000Z07:00")
|
||||
l.Debug(ctx, "test")
|
||||
|
||||
t.AssertGE(len(strings.Split(w.String(), "[DEBU]")), 1)
|
||||
datetime := strings.Trim(strings.Split(w.String(), "[DEBU]")[0], " ")
|
||||
|
||||
_, err := time.Parse("2006-01-02T15:04:05.000Z07:00", datetime)
|
||||
t.AssertNil(err)
|
||||
_, err = time.Parse("2006-01-02 15:04:05.000", datetime)
|
||||
t.AssertNE(err, nil)
|
||||
_, err = time.Parse("Mon, 02 Jan 2006 15:04:05 -0700", datetime)
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SetLevel(t *testing.T) {
|
||||
defaultLog := glog.DefaultLogger().Clone()
|
||||
defer glog.SetDefaultLogger(defaultLog)
|
||||
|
Loading…
x
Reference in New Issue
Block a user