logger

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package logger provides structured logging utilities wrapping log/slog.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg Config) *slog.Logger

New creates a new structured logger with sensible defaults.

Default configuration:

  • Level: info
  • Format: json
  • Output: os.Stderr

Example:

log := logger.New(logger.Config{
    Level:  "debug",
    Format: "text",
})
log.Info("server started", "port", 8080)

func NewDefault

func NewDefault() *slog.Logger

NewDefault creates a logger with default settings (info level, JSON format, stderr).

func NewFile

func NewFile(appName string) (*slog.Logger, func(), error)

NewFile creates a logger that writes to a temp file.

This is useful for TUI applications where stdout must remain clean. The logger writes JSON-formatted structured logs.

Returns the logger, a cleanup function to close the file, and any error. The cleanup function should be deferred immediately after calling this.

Log file location: {os.TempDir()}/{appName}.log

Example:

log, cleanup, err := logger.NewFile("myapp")
if err != nil {
    return err
}
defer cleanup()

log.Info("application started")
// Logs written to /tmp/myapp.log (or equivalent)

func Path

func Path(appName string) string

Path returns the path where file logs are written.

Example:

path := logger.Path("myapp")
// Returns: /tmp/myapp.log (on Unix systems)

Types

type Config

type Config struct {
	Level  string    // debug, info, warn, error (default: info)
	Format string    // json, text (default: json)
	Output io.Writer // default: os.Stderr
}

Config configures structured logging behavior.

Jump to

Keyboard shortcuts

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