logutil

package
v1.111.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package logutil provides general logging utilities for log/slog integration.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LevelName

func LevelName(l LogLevel) string

LevelName returns the name associated with the specified level.

func NewLogFromSlog

func NewLogFromSlog(logger *slog.Logger) *log.Logger

NewLogFromSlog creates a standard log.Logger that writes to the provided slog.Logger.

func ValidFormat

func ValidFormat(f LogFormat) bool

ValidFormat returns true if the log format is valid.

func ValidLevel

func ValidLevel(l LogLevel) bool

ValidLevel returns true if the log level is valid.

Types

type Attr

type Attr = slog.Attr

Attr is a type alias for slog.Attr.

type Config

type Config struct {
	Out        io.Writer
	Format     LogFormat
	Level      LogLevel
	CommonAttr []Attr
	HookFn     HookFunc
}

Config holds common logger parameters.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config instance with default settings.

func NewConfig

func NewConfig(opts ...Option) (*Config, error)

NewConfig returns a new configuration with the applied options.

func (*Config) SlogDefaultLogger

func (c *Config) SlogDefaultLogger() *slog.Logger

SlogDefaultLogger set and return a slog logger based on the Config settings.

func (*Config) SlogHandler

func (c *Config) SlogHandler() slog.Handler

SlogHandler returns a new slog Handler based on the Config settings.

func (*Config) SlogLogger

func (c *Config) SlogLogger() *slog.Logger

SlogLogger returns a slog logger based on the Config settings.

type HookFunc

type HookFunc func(level LogLevel, message string)

HookFunc is used to intercept the log message before passing it to the underlying handler.

type LogFormat

type LogFormat int8

LogFormat represents the logging output format.

const (
	FormatNone    LogFormat = -1 // Discard the logs.
	FormatJSON    LogFormat = 0  // Prints the logs in JSON format.
	FormatConsole LogFormat = 1  // Prints the logs in a human friendly format.
)

func ParseFormat

func ParseFormat(f string) (LogFormat, error)

ParseFormat converts a string to a log format.

type LogLevel

type LogLevel = slog.Level

LogLevel is an alias for slog.Level to represent extended log severity levels.

const (
	LevelEmergency LogLevel = 64 // (+) 0 - Emergency - System is unusable.
	LevelAlert     LogLevel = 32 // (+) 1 - Alert - Immediate action required.
	LevelCritical  LogLevel = 16 // (+) 2 - Critical - Critical conditions.
	LevelError     LogLevel = 8  // (=) 3 - Error - Error conditions.
	LevelWarning   LogLevel = 4  // (=) 4 - Warning - Warning conditions.
	LevelNotice    LogLevel = 2  // (+) 5 - Notice - Normal but noteworthy events.
	LevelInfo      LogLevel = 0  // (=) 6 - Informational - General informational messages.
	LevelDebug     LogLevel = -4 // (=) 7 - Debug - Detailed debugging information.
	LevelTrace     LogLevel = -8 // (+) Additional Trace level when supported.
)

Extended slog levels.

func ParseLevel

func ParseLevel(l string) (LogLevel, error)

ParseLevel converts syslog standard level strings to log/slog levels. Syslog uses eight severity levels, ranging from 0 (Emergency) to 7 (Debug). The lower the number, the higher the priority.

type Option

type Option func(*Config) error

Option is a type alias for a function that updates the Config.

func WithCommonAttr

func WithCommonAttr(a ...Attr) Option

WithCommonAttr adds common attributes to the logger.

func WithFormat

func WithFormat(f LogFormat) Option

WithFormat overrides the log format.

func WithFormatStr

func WithFormatStr(f string) Option

WithFormatStr overrides the log format.

func WithHookFn

func WithHookFn(f HookFunc) Option

WithHookFn adds a function to intercept the log message before passing it to the underlying handler.

func WithLevel

func WithLevel(l LogLevel) Option

WithLevel overrides the log level.

func WithLevelStr

func WithLevelStr(l string) Option

WithLevelStr overrides the log level.

func WithOutWriter

func WithOutWriter(w io.Writer) Option

WithOutWriter overrides the output io.Writer.

type SlogHookHandler

type SlogHookHandler struct {
	slog.Handler
	// contains filtered or unexported fields
}

SlogHookHandler is a slog.Handler that wraps another handler to add custom logic.

func NewSlogHookHandler

func NewSlogHookHandler(h slog.Handler, f HookFunc) *SlogHookHandler

NewSlogHookHandler adds a hook function to the slog Handler.

func (SlogHookHandler) Handle

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

Handle intercepts the log record, modifies the message, and then passes it to the underlying handler.

type SlogWriter

type SlogWriter struct {
	Logger *slog.Logger
}

SlogWriter is a custom io.Writer that writes to slog.Logger at the error level.

func NewSlogWriter

func NewSlogWriter(logger *slog.Logger) *SlogWriter

NewSlogWriter creates a new SlogWriter with the provided slog.Logger.

func (SlogWriter) Write

func (w SlogWriter) Write(p []byte) (int, error)

Write writes the log message to the slog.Logger at the error level.

Jump to

Keyboard shortcuts

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