handlers

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: GPL-3.0 Imports: 3 Imported by: 0

README

fastlog/handlers

Import path: github.com/InsideGallery/core/fastlog/handlers

handlers is the process-wide registry behind fastlog. Handler packages register either writer factories or complete slog.Handler factories, and fastlog.Config resolves configured output kinds through this registry.

Main APIs

  • FormatJSON and FormatText are the supported writer-backed output formats.
  • WriterFunc returns an io.Writer, optional *slog.HandlerOptions, and an error.
  • HandlerFunc returns a complete slog.Handler.
  • RegisterWriter(kind string, fn WriterFunc) registers writer-backed handlers.
  • RegisterHandlerFunc(kind string, fn HandlerFunc) registers complete handler factories.
  • Get(kind, format string, level slog.Level) returns a handler or ErrNotFoundHandler.
  • ErrNotFoundHandler reports an unknown output kind.

Usage

package example

import (
	"io"
	"log/slog"
	"os"

	"github.com/InsideGallery/core/fastlog/handlers"
)

func registerStdout() {
	handlers.RegisterWriter("stdout", func() (io.Writer, *slog.HandlerOptions, error) {
		return os.Stdout, nil, nil
	})
}

func newLogger() (*slog.Logger, error) {
	handler, err := handlers.Get("stdout", handlers.FormatJSON, slog.LevelInfo)
	if err != nil {
		return nil, err
	}

	return slog.New(handler), nil
}

Operational Notes

Get checks registered HandlerFunc values first. For those handlers, the format and level parameters are handled by the factory itself. Writer-backed handlers are wrapped in slog.TextHandler for text; all other formats use slog.JSONHandler. If a writer factory returns nil options, Get applies the requested level.

Documentation

Index

Constants

View Source
const (
	FormatText = "text"
	FormatJSON = "json"
)

Variables

View Source
var ErrNotFoundHandler = errors.New("not found handler")

Functions

func Get

func Get(kind, format string, level slog.Level) (slog.Handler, error)

Get returns a slog.Handler for the given kind and format. It first checks handler factories, then tries to build one from a registered writer.

func RegisterHandlerFunc added in v1.2.1

func RegisterHandlerFunc(kind string, fn HandlerFunc)

RegisterHandlerFunc registers a handler factory for the given output kind. The factory produces a complete slog.Handler — format parameter is ignored.

func RegisterWriter

func RegisterWriter(kind string, fn WriterFunc)

RegisterWriter registers a writer factory for the given output kind. The writer will be wrapped in slog.JSONHandler or slog.TextHandler based on format.

Types

type HandlerFunc added in v1.2.1

type HandlerFunc func() (slog.Handler, error)

HandlerFunc is a factory that returns a pre-built slog.Handler directly. Used by outputs (e.g. OTEL, Datadog) that manage their own handler construction.

type WriterFunc added in v1.2.1

type WriterFunc func() (io.Writer, *slog.HandlerOptions, error)

WriterFunc is a factory that returns an io.Writer and optional handler options.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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