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

Isn't `slog.Fatal()` meant to call `os.Exit(1)`? #143

Closed yermulnik closed 6 months ago

yermulnik commented 6 months ago

I'm expecting slog.Fatal to call os.Exit(1) to reflect log.Fatal(), but this is not happening. Is this expected or am I doing something incorrect?

package main

import (
        "github.com/gookit/slog"
)

func main() {
        slog.Info("info log message")
        slog.Fatal("fatal log message")
        slog.Info("info log message")
}
> go run ./main.go ; echo $?
[2024/04/05T19:13:52.904] [application] [INFO] [main.go:8,main] info log message
[2024/04/05T19:13:52.904] [application] [FATAL] [main.go:9,main] fatal log message
[2024/04/05T19:13:52.904] [application] [INFO] [main.go:10,main] info log message
0
yermulnik commented 6 months ago

https://github.com/gookit/slog/blob/master/common.go#L56-L58

    // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
    // logging level <= FatalLevel.
    FatalLevel Level = 200
yermulnik commented 6 months ago

I'd expect Panic to panic(), Fatal to os.Exit(1), and everything else to just print. Am I misinterpreting the idea and the code?

inhere commented 6 months ago

hi @yermulnik logger.ExitFunc is not set by default, so Fatal will not exit.

You can set it:

// for std logger
slog.SetExitFunc(os.Exit)

// for new logger
l := slog.New(func(l *slog.Logger) {
    l.ExitFunc = os.Exit
})
yermulnik commented 6 months ago

@inhere Thanks for the pointer 👍🏻