Skip to content
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

feat req: log/slog: adding support for quiet log level #69227

Closed
Nicolas-Peiffer opened this issue Sep 3, 2024 · 2 comments
Closed

feat req: log/slog: adding support for quiet log level #69227

Nicolas-Peiffer opened this issue Sep 3, 2024 · 2 comments
Labels

Comments

@Nicolas-Peiffer
Copy link

Nicolas-Peiffer commented Sep 3, 2024

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 for quiet

This is my original InitLogger method, with the supported log/slog levels debug, info, warn, error

func InitLogger(level string) error {
	var logLevel slog.Level

	switch level {
	case "debug":
		logLevel = slog.LevelDebug
	case "info":
		logLevel = slog.LevelInfo
	case "warn":
		logLevel = slog.LevelWarn
	case "error":
		logLevel = slog.LevelError
	default:
		return fmt.Errorf("invalid log level: %s", level)
	}

	handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel})
	slog.SetDefault(slog.New(handler))

	slog.Info("Logger initialized", "log-level", level)
	return nil
}

I call the method like this, for example in my cobra CLI:

// RootCmd represents the base command when called without any subcommands
var RootCmd = &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...

func init() {
	// 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.
func InitLogger(level string) error {
	var handler slog.Handler

	switch level {
	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 output
		handler = slog.NewTextHandler(io.Discard, nil)
	default:
		return fmt.Errorf("invalid log level: %s", level)
	}

	// Set the logger to use the chosen handler
	slog.SetDefault(slog.New(handler))

	slog.Info("Logger initialized", "log-level", level)
	return nil
}

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.

@Nicolas-Peiffer Nicolas-Peiffer changed the title log/slog: adding support for quiet log level feat req: log/slog: adding support for quiet log level Sep 3, 2024
@gabyhelp
Copy link

gabyhelp commented Sep 3, 2024

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@Nicolas-Peiffer
Copy link
Author

closing, duplicate #62005

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants