slogger

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 5 Imported by: 0

README

slogger

Minimal wrapper around log/slog for creating configured loggers and propagating them through context.

Install

go get github.com/falmar/slogger

Usage

package main

import (
	"log/slog"

	"github.com/falmar/slogger"
)

func main() {
	// Default logger (text, stderr, info level)
	logger := slogger.New()

	// Custom config
	logger = slogger.New(slogger.Config{
		Format: slogger.FormatJSON,
		Level:  slog.LevelDebug,
	})

	// Store in context
	ctx := slogger.WithLogger(context.Background(), logger)

	// Retrieve from context
	logger = slogger.FromContext(ctx)
	logger.Info("hello")
}

License

MIT

Documentation

Overview

Package slogger provides a minimal wrapper around log/slog for creating configured loggers and propagating them through context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

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

func LevelFromText added in v0.0.2

func LevelFromText(level string) slog.Level

LevelFromText parses a level name to a slog.Level. It accepts "debug", "info", "warn", "warning", and "error" (case-insensitive). Unknown values default to slog.LevelInfo.

func New

func New(cfg ...Config) *slog.Logger

func WithLogger

func WithLogger(ctx context.Context, logger *slog.Logger) context.Context

Types

type Config

type Config struct {
	Writer io.Writer
	Level  slog.Level
	Format Format
}

type Format

type Format string
const (
	FormatText Format = "text"
	FormatJSON Format = "json"
)

Jump to

Keyboard shortcuts

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