Skip to content

Instantly share code, notes, and snippets.

@zhangyoufu
Created November 12, 2025 09:54
Show Gist options
  • Select an option

  • Save zhangyoufu/f1ff2681d4d166cf67e8260f63ed8dc8 to your computer and use it in GitHub Desktop.

Select an option

Save zhangyoufu/f1ff2681d4d166cf67e8260f63ed8dc8 to your computer and use it in GitHub Desktop.
if stdout is tty: log text to stderr; else: log json to stdout
import "github.com/mattn/go-isatty"
func init() {
if isatty.IsTerminal(os.Stdout.Fd()) {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment