DEV Community

Cover image for slog - Lightweight, configurable, extensible Go logging library
Inhere
Inhere

Posted on

8 5

slog - Lightweight, configurable, extensible Go logging library

slog is a lightweight, configurable, extensible Go logging library.

image

Features

  • Simple, directly available without configuration
  • Support common log level processing.
    • eg: trace debug info notice warn error fatal panic
  • Support any extension of Handler Formatter as needed
  • Supports adding multiple Handler log processing at the same time, outputting logs to different places
  • Support to custom log message Formatter
    • Built-in json text two log record formatting Formatter
  • Support to custom build log messages Handler
    • The built-in handler.Config handler.Builder can easily and quickly build the desired log handler
  • Has built-in common log write handler program
    • console output logs to the console, supports color output
    • writer output logs to the specified io.Writer
    • file output log to the specified file, optionally enable buffer to buffer writes
    • simple output log to the specified file, write directly to the file without buffering
    • rotate_file outputs logs to the specified file, and supports splitting files by time and size, and buffer buffered writing is enabled by default
    • See ./handler folder for more built-in implementations

Output logs to file

  • Support enabling buffer for log writing
  • Support splitting log files by time and size
  • Support configuration to compress log files via gzip
  • Support clean old log files by BackupNum BackupTime

Git repository

Install

go get github.com/gookit/slog
Enter fullscreen mode Exit fullscreen mode

Quick start

package main

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

func main() {
    slog.Trace("this is a log message")
    slog.Debug("this is a log message")
    slog.Info("this is a log message")
    slog.Notice("this is a log message")
    slog.Warn("this is a log message")
    slog.Error("this is a log message")
    slog.Fatal("this is a log message")
}
Enter fullscreen mode Exit fullscreen mode

output preview:

image

Logs to file

Using slog to output logs to files is very convenient, and supports multiple files, splitting by time, etc.

package main

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

func main() {
    defer slog.MustFlush()

    // DangerLevels contains: slog.PanicLevel, slog.ErrorLevel, slog.WarnLevel
    h1 := handler.MustRotateFile("/tmp/logs/app_error.log", rotatefile.EveryHour, 
        handler.WithLogLevels(slog.DangerLevels),
    )

    // NormalLevels contains: slog.InfoLevel, slog.NoticeLevel, slog.DebugLevel, slog.TraceLevel
    h2 := handler.MustRotateFile("/tmp/logs/app_info.log", rotatefile.EveryHour,
        handler.WithLogLevels(slog.NormalLevels),
    )

    slog.PushHandler(h1)
    slog.PushHandler(h2)

    // add logs
    slog.Info("info message text")
    slog.Error("error message text")
}
Enter fullscreen mode Exit fullscreen mode

See logs dir:

$ ls /tmp/logs
app_error.log
app_info.log
Enter fullscreen mode Exit fullscreen mode

More usage

More usage please see README

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (1)

Collapse
 
samber profile image
Samuel Berthe •

Go 1.21 is going to bring a new structured logging library called "slog".

I will be pretty similar to gookit/slog but without formatter and provided handlers will only be able to write to stdout or stdin.

The library has been built to be extended very easily with customer slog.Handler.

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more