dynamiclevelhandler

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2026 License: Apache-2.0 Imports: 3 Imported by: 3

README

dynamic-level-handler

Golang wrapper for slog.Handler that supports level override for concrete logger. This functionality was originally implemented in Neptunus to support different logging levels of pipelines and plugins.

But... Why?

Have you ever wanted to enable only warnings or errors for specific parts of an application? And then enable debug for those concrete parts? I have. This package was created with that idea in mind.

DynamicHandlerWrapper wraps the passed slog.Handler and allows you to set a personal logging level via slog.Leveler. If level has not been overridden, all calls are passed to the basic handler.

Examples

import (
	"log/slog"

	dynamic "github.com/gekatateam/dynamic-level-handler"
)

// create basic handler
h := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
    Level: slog.LevelInfo
})

// wrap basic handler
dynamicHandler := dynamic.New(h)

// create common logger for all app
logger := slog.New(dynamicHandler)

......

// create child logger with some attributes
// then override it's level
pipelineLogger := logger.With(slog.Group("pipeline",
    "id", "cronjobs",
))
// you don't need to think about what specific handler type is passed here
// this function only do something if handler type is `DynamicLevelHandler`
dynamic.OverrideLevel(pipelineLogger.Handler(), slog.LevelWarn)

......

// create another child logger, set it's personal level
pluginLogger := pipelineLogger.log.With(slog.Group("input",
    "plugin", "cronjob",
    "name", "first cron",
))
dynamic.OverrideLevel(pluginLogger.Handler(), slog.LevelDebug)

Documentation

Index

Constants

View Source
const LevelUnassigned slog.Level = math.MaxInt

Variables

This section is empty.

Functions

func OverrideLevel

func OverrideLevel(h slog.Handler, newLevel slog.Leveler) (applied bool)

Types

type DynamicLevelHandler

type DynamicLevelHandler struct {
	// contains filtered or unexported fields
}

func New

func (*DynamicLevelHandler) Enabled

func (h *DynamicLevelHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*DynamicLevelHandler) Handle

func (h *DynamicLevelHandler) Handle(ctx context.Context, record slog.Record) error

func (*DynamicLevelHandler) Override

func (h *DynamicLevelHandler) Override(newLevel slog.Leveler)

func (*DynamicLevelHandler) Reset added in v1.1.0

func (h *DynamicLevelHandler) Reset()

func (*DynamicLevelHandler) WithAttrs

func (h *DynamicLevelHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*DynamicLevelHandler) WithGroup

func (h *DynamicLevelHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL