-
Notifications
You must be signed in to change notification settings - Fork 728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pkg/logutil: add json log format #626
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,23 +30,33 @@ import ( | |
const ( | ||
defaultLogTimeFormat = "2006/01/02 15:04:05" | ||
defaultLogMaxSize = 300 // MB | ||
defaultLogMaxBackups = 3 | ||
defaultLogMaxAge = 28 // days | ||
defaultLogFormat = "text" | ||
defaultLogLevel = log.InfoLevel | ||
|
||
logDirMode = 0755 | ||
) | ||
|
||
// FileLogConfig serializes file log related config in toml/json. | ||
type FileLogConfig struct { | ||
// Log filename, leave empty to disable file log. | ||
Filename string `toml:"filename" json:"filename"` | ||
// Is log rotate enabled. TODO. | ||
LogRotate bool `toml:"log-rotate" json:"log-rotate"` | ||
// Max size for a single file, in MB. | ||
MaxSize int `toml:"max-size" json:"max-size"` | ||
// Max log keep days, default is never deleting. | ||
MaxDays int `toml:"max-days" json:"max-days"` | ||
// Maximum number of old log files to retain. | ||
MaxBackups int `toml:"max-backups" json:"max-backups"` | ||
} | ||
|
||
// LogConfig serializes log related config in toml/json. | ||
type LogConfig struct { | ||
// Log level. | ||
Level string `toml:"level" json:"level"` | ||
// Log file. | ||
// Log format. one of json, text, or console. | ||
Format string `toml:"format" json:"format"` | ||
// Disable automatic timestamps in output. | ||
DisableTimestamp bool `toml:"disable-timestamp" json:"disable-timestamp"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any benefit to disabling timestamp? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ELK system will add a timestamp and it has more accuracy. |
||
// File log config. | ||
File FileLogConfig `toml:"file" json:"file"` | ||
} | ||
|
||
|
@@ -130,7 +140,9 @@ func stringToLogLevel(level string) log.Level { | |
} | ||
|
||
// textFormatter is for compatability with ngaut/log | ||
type textFormatter struct{} | ||
type textFormatter struct { | ||
DisableTimestamp bool | ||
} | ||
|
||
// Format implements logrus.Formatter | ||
func (f *textFormatter) Format(entry *log.Entry) ([]byte, error) { | ||
|
@@ -140,9 +152,11 @@ func (f *textFormatter) Format(entry *log.Entry) ([]byte, error) { | |
} else { | ||
b = &bytes.Buffer{} | ||
} | ||
b.WriteString(entry.Time.Format(defaultLogTimeFormat)) | ||
if !f.DisableTimestamp { | ||
fmt.Fprintf(b, "%s ", entry.Time.Format(defaultLogTimeFormat)) | ||
} | ||
if file, ok := entry.Data["file"]; ok { | ||
fmt.Fprintf(b, " %s:%v:", file, entry.Data["line"]) | ||
fmt.Fprintf(b, "%s:%v:", file, entry.Data["line"]) | ||
} | ||
fmt.Fprintf(b, " [%s] %s", entry.Level.String(), entry.Message) | ||
for k, v := range entry.Data { | ||
|
@@ -154,33 +168,48 @@ func (f *textFormatter) Format(entry *log.Entry) ([]byte, error) { | |
return b.Bytes(), nil | ||
} | ||
|
||
// setLogOutput sets output path for all logs. | ||
func setLogOutput(filename string) error { | ||
// PD log | ||
if st, err := os.Stat(filename); err == nil { | ||
func stringToLogFormatter(format string, disableTimestamp bool) log.Formatter { | ||
switch strings.ToLower(format) { | ||
case "text": | ||
return &textFormatter{ | ||
DisableTimestamp: disableTimestamp, | ||
} | ||
case "json": | ||
return &log.JSONFormatter{ | ||
TimestampFormat: defaultLogTimeFormat, | ||
DisableTimestamp: disableTimestamp, | ||
} | ||
case "console": | ||
return &log.TextFormatter{ | ||
FullTimestamp: true, | ||
TimestampFormat: defaultLogTimeFormat, | ||
DisableTimestamp: disableTimestamp, | ||
} | ||
default: | ||
return &textFormatter{} | ||
} | ||
} | ||
|
||
// InitFileLog initializes file based logging options. | ||
func InitFileLog(cfg *FileLogConfig) error { | ||
if st, err := os.Stat(cfg.Filename); err == nil { | ||
if st.IsDir() { | ||
return errors.New("can't use directory as log file name") | ||
} | ||
} | ||
dir := path.Dir(filename) | ||
err := os.MkdirAll(dir, logDirMode) | ||
if err != nil { | ||
return errors.Trace(err) | ||
if cfg.MaxSize == 0 { | ||
cfg.MaxSize = defaultLogMaxSize | ||
} | ||
|
||
// use lumberjack to logrotate | ||
output := &lumberjack.Logger{ | ||
Filename: filename, | ||
MaxSize: defaultLogMaxSize, // megabytes | ||
MaxBackups: defaultLogMaxBackups, | ||
MaxAge: defaultLogMaxAge, // days | ||
Filename: cfg.Filename, | ||
MaxSize: cfg.MaxSize, | ||
MaxBackups: cfg.MaxBackups, | ||
MaxAge: cfg.MaxDays, | ||
LocalTime: true, | ||
} | ||
|
||
if _, err := output.Write([]byte{}); err != nil { | ||
return errors.Errorf("log file is not writable: %v", err) | ||
} | ||
|
||
log.SetOutput(output) | ||
return nil | ||
} | ||
|
@@ -189,7 +218,11 @@ func setLogOutput(filename string) error { | |
func InitLogger(cfg *LogConfig) error { | ||
log.SetLevel(stringToLogLevel(cfg.Level)) | ||
log.AddHook(&contextHook{}) | ||
log.SetFormatter(&textFormatter{}) | ||
|
||
if cfg.Format == "" { | ||
cfg.Format = defaultLogFormat | ||
} | ||
log.SetFormatter(stringToLogFormatter(cfg.Format, cfg.DisableTimestamp)) | ||
|
||
// etcd log | ||
capnslog.SetFormatter(&redirectFormatter{}) | ||
|
@@ -198,7 +231,7 @@ func InitLogger(cfg *LogConfig) error { | |
return nil | ||
} | ||
|
||
err := setLogOutput(cfg.File.Filename) | ||
err := InitFileLog(&cfg.File) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
em, I think most of the time, users don't use console even they run PD in the console. TEXT and JSON may be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
colorful output. can be remove from comment, leave the function in code.