Commit d87c88d5 authored by zhiqiangxu's avatar zhiqiangxu Committed by GitHub

add `log.pid` to enable showing pid in log (#10722)

* show pid in log

* add Pid to cli config
parent c4226bf4
......@@ -20,6 +20,7 @@ const (
LevelFlagName = "log.level"
FormatFlagName = "log.format"
ColorFlagName = "log.color"
PidFlagName = "log.pid"
)
func CLIFlags(envPrefix string) []cli.Flag {
......@@ -51,6 +52,12 @@ func CLIFlagsWithCategory(envPrefix string, category string) []cli.Flag {
EnvVars: opservice.PrefixEnvVar(envPrefix, "LOG_COLOR"),
Category: category,
},
&cli.BoolFlag{
Name: PidFlagName,
Usage: "Show pid in the log",
EnvVars: opservice.PrefixEnvVar(envPrefix, "LOG_PID"),
Category: category,
},
}
}
......@@ -187,6 +194,7 @@ type CLIConfig struct {
Level slog.Level
Color bool
Format FormatType
Pid bool
}
// AppOut returns an io.Writer to write app output to, like logs.
......@@ -208,7 +216,11 @@ func NewLogHandler(wr io.Writer, cfg CLIConfig) slog.Handler {
// The log handler of the logger is a LvlSetter, i.e. the log level can be changed as needed.
func NewLogger(wr io.Writer, cfg CLIConfig) log.Logger {
h := NewLogHandler(wr, cfg)
return log.NewLogger(h)
l := log.NewLogger(h)
if cfg.Pid {
l = l.With("pid", os.Getpid())
}
return l
}
// SetGlobalLogHandler sets the log handles as the handler of the global default logger.
......@@ -237,5 +249,6 @@ func ReadCLIConfig(ctx *cli.Context) CLIConfig {
if ctx.IsSet(ColorFlagName) {
cfg.Color = ctx.Bool(ColorFlagName)
}
cfg.Pid = ctx.Bool(PidFlagName)
return cfg
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment