sloghelper

package
v0.10.3 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 12 Imported by: 7

Documentation

Overview

Package sloghelper provides helpers around the standard library log/slog package.

It includes logger construction, context storage, error attributes with stack information when available, and a handler wrapper for shared handling hooks.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrorKey = "error"
)

Functions

func Apply

func Apply(opt HandlerOption)

func Error

func Error(err error) slog.Attr

func ErrorValue

func ErrorValue(err error) slog.Value

func FromContext

func FromContext(ctx context.Context) *slog.Logger

func NewContext

func NewContext(ctx context.Context, logger *slog.Logger) context.Context
Example
package main

import (
	"context"
	"fmt"
	"io"
	"log/slog"

	"github.com/go-jimu/components/sloghelper"
)

func main() {
	logger := slog.New(slog.NewTextHandler(io.Discard, nil))
	ctx := sloghelper.NewContext(context.Background(), logger)

	fmt.Println(sloghelper.FromContext(ctx) == logger)

}
Output:
true

func NewHandler

func NewHandler(hdl slog.Handler, opts ...HandlerOption) slog.Handler

func NewLog

func NewLog(opt Options) *slog.Logger

Types

type HandleFunc

type HandleFunc func(context.Context, *slog.Record)

type Handler

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

func (*Handler) Apply

func (ch *Handler) Apply(opt HandlerOption)

func (*Handler) Enabled

func (ch *Handler) Enabled(ctx context.Context, level slog.Level) bool

func (*Handler) Handle

func (ch *Handler) Handle(ctx context.Context, r slog.Record) error

func (*Handler) WithAttrs

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

func (*Handler) WithGroup

func (ch *Handler) WithGroup(name string) slog.Handler

type HandlerOption

type HandlerOption func(*Handler)

func WithHandleFunc

func WithHandleFunc(fn HandleFunc) HandlerOption

type Options

type Options struct {
	Level      string `json:"level" toml:"level" yaml:"level"`
	Output     string `json:"output" toml:"output" yaml:"output"`
	MaxSize    int    `json:"max_size" toml:"max_size" yaml:"max_size"`
	MaxAge     int    `json:"max_age" toml:"max_age" yaml:"max_age"`
	MaxBackups int    `json:"max_backups" toml:"max_backups" yaml:"max_backups"`
	LocalTime  bool   `json:"local_time" toml:"local_time" yaml:"local_time"`
	Compress   bool   `json:"compress" toml:"compress" yaml:"compress"`
}

type StackTracer

type StackTracer interface {
	StackTrace() errors.StackTrace
}

Jump to

Keyboard shortcuts

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