Skip to main content

gookit/slog - release v0.5.2, Lightweight, configurable, extensible logging library

· 2 min read
inhere

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.

Github https://github.com/gookit/slog

v0.5.2 changelog

Full changelog: https://github.com/gookit/slog/compare/v0.5.1...v0.5.2

Feature

Update

New feature usage

Custom log file permissions

The log file permission flag created by configuring the FilePerm setting of hander.Config.

h1 := handler.MustFileHandler("/tmp/error.log",
handler.WithLogLevels(slog.DangerLevels),
handler.WithFilePerm(0644), // <- sets log file permissions
)

slog.PushHandler(h1)

Split files using ModeCreate mode

about RotateMode:

  • ModeRename By default, every time rename handles rotation.
  • ModeCreate Create files only by split time

Setting RotateMode=ModeCreate allows log files to be created only by split time.

h1 := handler.MustRotateFile(
"/tmp/error.log",
rotatefile.EveryHour, // split by hour
handler.WithLogLevels(slog.DangerLevels),
handler.WithRotateMode(rotatefile.ModeCreate), // set RotateMode=ModeCreate
)

slog.PushHandler(h1)

Configure the logfile as /tmp/error.log in the above example. When set to ModeCreate mode, the file will not actually be created, Instead, it will be created according to the actual split time:

/tmp/error.log.20230618_1500
/tmp/error.log.20230618_1600
/tmp/error.log.20230618_1700
...

Tips: The ModeCreate mode can be applied to command-line tool applications. Because they exit after each execution, ModeRename may not be able to split files on time.

And using ModeRename mode will have the following effect:

/tmp/error.log # <- Logs are always written to this file
/tmp/error.log.20230618_1500
/tmp/error.log.20230618_1600
/tmp/error.log.20230618_1700
...

More usage

More usage please see README