log: support custom time format configuration (#1401)
This commit is contained in:
parent
6e06ed1087
commit
e4381a5d25
@ -14,9 +14,12 @@ import (
|
|||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
timeFormat = "2006-01-02T15:04:05-0700"
|
||||||
|
termTimeFormat = "01-02|15:04:05.000"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
timeFormat = "2006-01-02T15:04:05-0700"
|
|
||||||
termTimeFormat = "01-02|15:04:05.000"
|
|
||||||
floatFormat = 'f'
|
floatFormat = 'f'
|
||||||
termMsgJust = 40
|
termMsgJust = 40
|
||||||
termCtxMaxPadding = 40
|
termCtxMaxPadding = 40
|
||||||
@ -482,3 +485,11 @@ func escapeString(s string) string {
|
|||||||
}
|
}
|
||||||
return strconv.Quote(s)
|
return strconv.Quote(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetTermTimeFormat(format string) {
|
||||||
|
termTimeFormat = format
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetTimeFormat(format string) {
|
||||||
|
timeFormat = format
|
||||||
|
}
|
||||||
|
@ -502,8 +502,13 @@ func (c *Config) warnOnce(w *bool, format string, args ...interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type LogConfig struct {
|
type LogConfig struct {
|
||||||
FileRoot string
|
FileRoot *string
|
||||||
FilePath string
|
FilePath *string
|
||||||
MaxBytesSize uint
|
MaxBytesSize *uint
|
||||||
Level string
|
Level *string
|
||||||
|
|
||||||
|
// TermTimeFormat is the time format used for console logging.
|
||||||
|
TermTimeFormat *string
|
||||||
|
// TimeFormat is the time format used for file logging.
|
||||||
|
TimeFormat *string
|
||||||
}
|
}
|
||||||
|
24
node/node.go
24
node/node.go
@ -86,13 +86,25 @@ func New(conf *Config) (*Node, error) {
|
|||||||
conf.DataDir = absdatadir
|
conf.DataDir = absdatadir
|
||||||
}
|
}
|
||||||
if conf.LogConfig != nil {
|
if conf.LogConfig != nil {
|
||||||
logFilePath := ""
|
if conf.LogConfig.TermTimeFormat != nil && *conf.LogConfig.TermTimeFormat != "" {
|
||||||
if conf.LogConfig.FileRoot == "" {
|
log.SetTermTimeFormat(*conf.LogConfig.TermTimeFormat)
|
||||||
logFilePath = path.Join(conf.DataDir, conf.LogConfig.FilePath)
|
}
|
||||||
} else {
|
|
||||||
logFilePath = path.Join(conf.LogConfig.FileRoot, conf.LogConfig.FilePath)
|
if conf.LogConfig.TimeFormat != nil && *conf.LogConfig.TimeFormat != "" {
|
||||||
|
log.SetTimeFormat(*conf.LogConfig.TimeFormat)
|
||||||
|
}
|
||||||
|
|
||||||
|
if conf.LogConfig.FileRoot != nil && conf.LogConfig.FilePath != nil &&
|
||||||
|
conf.LogConfig.MaxBytesSize != nil && conf.LogConfig.Level != nil {
|
||||||
|
// log to file
|
||||||
|
logFilePath := ""
|
||||||
|
if *conf.LogConfig.FileRoot == "" {
|
||||||
|
logFilePath = path.Join(conf.DataDir, *conf.LogConfig.FilePath)
|
||||||
|
} else {
|
||||||
|
logFilePath = path.Join(*conf.LogConfig.FileRoot, *conf.LogConfig.FilePath)
|
||||||
|
}
|
||||||
|
log.Root().SetHandler(log.NewFileLvlHandler(logFilePath, *conf.LogConfig.MaxBytesSize, *conf.LogConfig.Level))
|
||||||
}
|
}
|
||||||
log.Root().SetHandler(log.NewFileLvlHandler(logFilePath, conf.LogConfig.MaxBytesSize, conf.LogConfig.Level))
|
|
||||||
}
|
}
|
||||||
if conf.Logger == nil {
|
if conf.Logger == nil {
|
||||||
conf.Logger = log.New()
|
conf.Logger = log.New()
|
||||||
|
Loading…
Reference in New Issue
Block a user