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 ( ...@@ -20,6 +20,7 @@ const (
LevelFlagName = "log.level" LevelFlagName = "log.level"
FormatFlagName = "log.format" FormatFlagName = "log.format"
ColorFlagName = "log.color" ColorFlagName = "log.color"
PidFlagName = "log.pid"
) )
func CLIFlags(envPrefix string) []cli.Flag { func CLIFlags(envPrefix string) []cli.Flag {
...@@ -51,6 +52,12 @@ func CLIFlagsWithCategory(envPrefix string, category string) []cli.Flag { ...@@ -51,6 +52,12 @@ func CLIFlagsWithCategory(envPrefix string, category string) []cli.Flag {
EnvVars: opservice.PrefixEnvVar(envPrefix, "LOG_COLOR"), EnvVars: opservice.PrefixEnvVar(envPrefix, "LOG_COLOR"),
Category: category, 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 { ...@@ -187,6 +194,7 @@ type CLIConfig struct {
Level slog.Level Level slog.Level
Color bool Color bool
Format FormatType Format FormatType
Pid bool
} }
// AppOut returns an io.Writer to write app output to, like logs. // 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 { ...@@ -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. // 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 { func NewLogger(wr io.Writer, cfg CLIConfig) log.Logger {
h := NewLogHandler(wr, cfg) 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. // SetGlobalLogHandler sets the log handles as the handler of the global default logger.
...@@ -237,5 +249,6 @@ func ReadCLIConfig(ctx *cli.Context) CLIConfig { ...@@ -237,5 +249,6 @@ func ReadCLIConfig(ctx *cli.Context) CLIConfig {
if ctx.IsSet(ColorFlagName) { if ctx.IsSet(ColorFlagName) {
cfg.Color = ctx.Bool(ColorFlagName) cfg.Color = ctx.Bool(ColorFlagName)
} }
cfg.Pid = ctx.Bool(PidFlagName)
return cfg 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