You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some use cases, you want to suppress (discard) all the logs outputs. However, log/slog does not support this quiet log level, but you can create a custom one implementing the quiet behavior.
My original InitLogger method without support for quiet
This is my original InitLogger method, with the supported log/slog levels debug, info, warn, error
I call the method like this, for example in my cobra CLI:
// RootCmd represents the base command when called without any subcommandsvarRootCmd=&cobra.Command{
Use: [...]
Run: func(cmd*cobra.Command, args []string) {
logger.InitLogger(LogLevel)
And I set the log level thanks to a cobra flag --log-level=info|debug...
funcinit() {
// Here you will define your flags and configuration settings.// Cobra supports persistent flags, which, if defined here,// will be global for your application.// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.app.yaml)")RootCmd.PersistentFlags().StringVar(&LogLevel, "log-level", "info", "set the log level (debug, info, warn, error)")
My workaround: adding a custom quiet log level
The workaround is fine. It is just a bit less elegant since I need to define slog.NewTextHandler for each 5 cases of the switch case. I do not know if I can achieve something better.
// InitLogger initializes the logger with the given level. Valid log levels are// "debug", "info", "warn", "error", and "quiet". If the log level is invalid,// an error is returned.funcInitLogger(levelstring) error {
varhandler slog.Handlerswitchlevel {
case"debug":
handler=slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})
case"info":
handler=slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})
case"warn":
handler=slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelWarn})
case"error":
handler=slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelError})
case"quiet":
// Create a logger with a nil handler// Use io.Discard to suppress all outputhandler=slog.NewTextHandler(io.Discard, nil)
default:
returnfmt.Errorf("invalid log level: %s", level)
}
// Set the logger to use the chosen handlerslog.SetDefault(slog.New(handler))
slog.Info("Logger initialized", "log-level", level)
returnnil
}
I believe it should be possible to add this quiet log level somewhere in the file src/log/slog/level.go.
But I am not a Golang contributor, and I do not know how to implement this.
cc @jba , since you made the proposal #56345, you might have an opinion or you migh know who could be interested.
I seem to be duplicating #62005
I missed it while searching the current issues.
The text was updated successfully, but these errors were encountered:
Proposal Details
In some use cases, you want to suppress (discard) all the logs outputs. However,
log/slog
does not support this quiet log level, but you can create a custom one implementing the quiet behavior.My original
InitLogger
method without support forquiet
This is my original
InitLogger
method, with the supportedlog/slog
levelsdebug
,info
,warn
,error
I call the method like this, for example in my cobra CLI:
And I set the log level thanks to a cobra flag
--log-level=info|debug...
My workaround: adding a custom
quiet
log levelThe workaround is fine. It is just a bit less elegant since I need to define
slog.NewTextHandler
for each 5 cases of the switch case. I do not know if I can achieve something better.I believe it should be possible to add this
quiet
log level somewhere in the filesrc/log/slog/level.go
.But I am not a Golang contributor, and I do not know how to implement this.
cc @jba , since you made the proposal #56345, you might have an opinion or you migh know who could be interested.
I seem to be duplicating #62005
I missed it while searching the current issues.
The text was updated successfully, but these errors were encountered: