gookit / slog

📑 Lightweight, configurable, extensible logging library written in Go. Support multi level, multi outputs and built-in multi file logger, buffers, clean, rotate-file handling.一个易于使用的,轻量级、可配置、可扩展的日志库。支持多个级别,输出到多文件;内置文件日志处理、自动切割、清理、压缩等增强功能
https://pkg.go.dev/github.com/gookit/slog
MIT License
393 stars 25 forks source link

Level handling is non-obvious #109

Closed nhi-vanye closed 1 year ago

nhi-vanye commented 1 year ago

On every logging system I've used across many languages if you set the log level to be Info, you also get Warning, Error, Fatal etc.

Not so with slog.

If I do handler.NewConsoleHandler(slog.Levels{slog.InfoLevel}) I just get Info messages...

So I'm expected to build by own list ? That's really annoying to do programmatically for no value that I can see...

inhere commented 1 year ago

hi @nhi-vanye

There are two ways to limit the log level to be recorded in slog:

  1. Use []level to restrict logging to specified level logs
  2. Use max level to limit the maximum log level

In the next version, I will provide some convenient methods to quickly use two methods:

// limit max log level, will log: panic ~ info level logs
h := handler.ConsoleWithLevel(slog.Info)

functions:


// ConsoleWithLevel create new ConsoleHandler and with max log level
func ConsoleWithLevel(level slog.Level) *ConsoleHandler

// ConsoleWithLevels create new ConsoleHandler and with limited log levels
func ConsoleWithLevels(levels []slog.Level) *ConsoleHandler
nhi-vanye commented 1 year ago

Thank you for very quick response.

I might consider changing the name to be ConsoleWithMaxLevel() to more closely match the behaviour as I think there's a good use-case for a corresponding ConsoleWithMinLevel(). That would make it very easy to separate production from debug logs with multiple handlers off a single Logger...

inhere commented 1 year ago

Released on the v0.5.3