DEV Community

Cover image for gookit/slog - release v0.5.2, Lightweight, configurable, extensible logging library
Inhere
Inhere

Posted on

1

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

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)

Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
...
Enter fullscreen mode Exit fullscreen mode

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
...
Enter fullscreen mode Exit fullscreen mode

More usage

More usage please see README

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay