log

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultLogLevel = slog.LevelInfo
	DefaultFormat   = FormatPretty
)

Variables

View Source
var (
	DefaultWriter            = os.Stderr
	DefaultTimeFormat        = time.StampMilli
	DefaultPrettyReplaceAttr = func(groups []string, a slog.Attr) slog.Attr {
		if a.Key == "pkg" {
			return tint.Attr(243, a)
		}
		if a.Key == "status" {
			code := a.Value.Int64()
			switch {
			case code >= 200 && code < 300:
				return tint.Attr(2, a)
			case code >= 400 && code < 500:
				return tint.Attr(3, a)
			case code >= 500 && code < 600:
				return tint.Attr(1, a)
			}
		}
		return a
	}
)

Functions

func Config added in v0.11.0

func Config(opts Options) error

Config initializes and applies the global logger configuration.

It sets up the global slog.Logger according to the provided Options, controlling:

  • output destination (stdout, file, or any io.Writer);
  • log format (plain text, JSON, or colorized "pretty");
  • optional attribute transformation via ReplaceAttr.

Behavior by format:

  • FormatText Writes human-readable logs using slog.TextHandler. Suitable for local development or text log files.

  • FormatJSON Writes structured JSON logs using slog.JSONHandler. Recommended for production environments and log aggregation systems.

  • FormatPretty Writes developer-friendly, colorized logs using tint.Handler. Colors are automatically disabled if the output is not a terminal.

ReplaceAttr:

  • If not nil, it is applied to each attribute before output. Can be used to mask sensitive data, rename fields, or modify messages.
  • If nil, attributes are written as-is.

Global effect:

  • Config replaces the backend of the global logger.
  • All loggers created via log.New, log.With, or log.WithGroup immediately start using the new configuration.

Example:

log.Config(log.Options{
    Writer: os.Stdout,
    Format: log.FormatPretty,
})

func Debug added in v0.11.0

func Debug(msg string, args ...any)

Debug logs a message at Debug level. Example: log.Debug("debug message", "key", 123)

func DebugContext added in v0.11.0

func DebugContext(ctx context.Context, msg string, args ...any)

DebugContext logs a Debug-level message with context. Example: log.DebugContext(ctx, "debug with ctx")

func Enabled added in v0.11.0

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

Enabled checks whether logging at the given level is enabled.

func Error added in v0.11.0

func Error(msg string, args ...any)

Error logs a message at Error level. Example: log.Error("failed to save file", "file", filename)

func ErrorContext added in v0.11.0

func ErrorContext(ctx context.Context, msg string, args ...any)

ErrorContext logs an Error-level message with context. Example: log.ErrorContext(ctx, "request failed")

func GetLevel added in v0.11.0

func GetLevel() slog.Level

GetLevel returns the current global log level.

func Handler added in v0.11.0

func Handler() slog.Handler

Handler returns the current global slog.Handler. Useful for integration or testing.

func Info added in v0.11.0

func Info(msg string, args ...any)

Info logs a message at Info level. Example: log.Info("user logged in", "user", "alice")

func InfoContext added in v0.11.0

func InfoContext(ctx context.Context, msg string, args ...any)

InfoContext logs an Info-level message with context. Example: log.InfoContext(ctx, "operation completed")

func Log added in v0.11.0

func Log(ctx context.Context, level slog.Level, msg string, args ...any)

Log outputs a message at the specified level with optional arguments. Example: log.Log(ctx, slog.LevelInfo, "message", "key", 123)

func LogAttrs added in v0.11.0

func LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)

LogAttrs outputs a message with structured slog attributes. Example: log.LogAttrs(ctx, slog.LevelInfo, "user login",

slog.String("user", "alice"), slog.Int("id", 42))

func New

func New(pkg string) *slog.Logger

New creates a logger scoped to a specific package or component. Example: log.New("users").Info("user created")

func SetBackend added in v0.11.0

func SetBackend(h slog.Handler)

SetBackend replaces the current backend for the global logger. Usually, Config() is sufficient.

func SetDefaultState added in v0.11.0

func SetDefaultState()

SetDefaultState resets the global logger to its default configuration. Typically used at startup or in tests.

func SetLevel added in v0.4.0

func SetLevel(l slog.Level)

SetLevel sets the global log level. Example: log.SetLevel(slog.LevelDebug)

func SetLevelStr added in v0.4.0

func SetLevelStr(l string) error

SetLevelStr sets the global log level from a string. Example: log.SetLevelStr("INFO")

func Warn added in v0.11.0

func Warn(msg string, args ...any)

Warn logs a message at Warn level. Example: log.Warn("disk space low")

func WarnContext added in v0.11.0

func WarnContext(ctx context.Context, msg string, args ...any)

WarnContext logs a Warn-level message with context. Example: log.WarnContext(ctx, "rate limit approaching")

func With added in v0.11.0

func With(args ...any) *slog.Logger

With creates a logger with additional context fields. Example: log.With("user", "alice", "role", "admin").Info("login")

func WithGroup added in v0.11.0

func WithGroup(name string) *slog.Logger

WithGroup creates a logger with a group for attributes. Example: log.WithGroup("db").Info("query executed")

Types

type Options added in v0.10.0

type Options struct {
	Writer      io.Writer
	Format      OutputFormat
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}

type OutputFormat added in v0.10.0

type OutputFormat string
const (
	// FormatText writes plain text logs, human-readable, suitable for local or file output.
	FormatText OutputFormat = "text"

	// FormatJSON writes structured JSON logs, recommended for production and log aggregation.
	FormatJSON OutputFormat = "json"

	// FormatPretty writes colorized, developer-friendly logs, automatically disables
	// color if output is not a terminal.
	FormatPretty OutputFormat = "pretty"
)

type ProxyHandler

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

func NewProxyHandler

func NewProxyHandler() *ProxyHandler

func (*ProxyHandler) Enabled

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

func (*ProxyHandler) Handle

func (h *ProxyHandler) Handle(ctx context.Context, r slog.Record) error

func (*ProxyHandler) WithAttrs

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

func (*ProxyHandler) WithGroup

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

Jump to

Keyboard shortcuts

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