Skip to content

lukephillippi/slogic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slogic

🧠 Composable filtering logic for log/slog

Test Go Reference

Overview

The slogic package provides surgical control over what gets logged in your Go applications.

Built on top of the standard library's log/slog package, it provides a composable filtering system that lets you:

  • ✅ Dynamically filter out logs based on level, time, message content, and/or any key-value attribute
  • ✅ Formulate bespoke filtering rules with logical operators (And, Or, Not)
  • ✅ Apply filters to any log/slog Handler implementation
  • ✅ Implement custom filters via a simple Filter interface

It's lightweight, dependency-free, and integrates seamlessly with any existing log/slog-based logging setup.

Installing

  1. First, use go get to install the latest version of the package:

    go get -u go.luke.ph/slogic@latest
  2. Next, include the package in your application:

    import "go.luke.ph/slogic"

Usage

The slogic package provides the core filtering functionality, while the slogic/filter package offers a rich set of pre-built Filters suitable for a variety of common use cases.

Combine these with logical operators (And, Or, Not) to create sophisticated filtering rules:

// 1. Keep all ERROR logs
// 2. Keep latency WARN logs
// 3. Filter all other ≤ WARN logs
handler := slogic.NewHandler(
    slog.NewTextHandler(os.Stdout, nil),
    slogic.And(
        slogic.Not(
            slogic.Or(
                filter.IfLevelEquals(slog.LevelError),
                slogic.And(
                    filter.IfLevelEquals(slog.LevelWarn),
                    filter.IfAttrExists("latency_ms"),
                ),
            ),
        ),
        filter.IfLevelAtMost(slog.LevelWarn),
    ),
)

slog.SetDefault(slog.New(handler))

License

The package is released under the Unlicense license.

References

About

🧠 Composable filtering logic for log/slog

Topics

Resources

License

Stars

Watchers

Forks

Languages