logging

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 12 Imported by: 6 modules, 6 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-backend-logging

Structured logging for Go services on top of the standard library's log/slog. Part of the go-backend-* family.

  • Console: one readable line per entry, 2026-01-02T10:00:00.000+11:00 [my-app] [info] message key=value, coloured when stdout is a terminal.
  • Files: JSON lines, rotated daily as <date>_<app>_<host>_<level>.log, with a stable symlink <app>_<level>.log pointing at today's file. There are two files: verbose gets everything at or above the minimum level, error gets errors only. Old files can be gzipped and pruned.
  • Levels: error, warn, info, debug and an extra verbose level below debug.
  • Context: attributes stored on a context.Context (request ID, correlation ID, user, ...) are added to every entry logged with that context.

Install

go get github.com/ahmedbaig/go-backend-logging

Usage

cfg := logging.DefaultConfig()
log, err := logging.New(cfg, "my-app")
if err != nil {
	panic(err)
}
defer log.Close()

log.Info("server started", "port", 8080)
log.Verbose("cache warm", "entries", 1200)
log.Error("payment failed", logging.Err(err))

ctx = logging.WithAttrs(ctx, slog.String("requestId", id))
log.InfoContext(ctx, "handled")

Logger embeds *slog.Logger, so it can be passed anywhere a *slog.Logger is expected (log.Logger).

Config

Loaded as JSON, typically with go-backend-config under the name <app>-logger.

Key Default
logDirectory /app/var/logs where log files go
minimumLogLevel verbose error, warn, info, debug, verbose
logToFile true write the JSON files
logToConsole true write console lines to stdout
zippedArchive false gzip yesterday's files after rotation
maxFiles 14 files kept per level (0 = keep all)
consoleColor auto force colour on or off

Building blocks

The pieces used by New are exported for custom setups: NewConsoleHandler, NewJSONHandler, NewContextHandler, Fanout (send one record to several handlers) and NewDailyFile (an io.WriteCloser with daily rotation).

License

MIT

Documentation

Index

Constants

View Source
const (
	LevelVerbose = slog.Level(-8)
	LevelDebug   = slog.LevelDebug
	LevelInfo    = slog.LevelInfo
	LevelWarn    = slog.LevelWarn
	LevelError   = slog.LevelError
)

Variables

This section is empty.

Functions

func AttrsFrom

func AttrsFrom(ctx context.Context) []slog.Attr

func Err

func Err(err error) slog.Attr

func Fanout

func Fanout(hs ...slog.Handler) slog.Handler

func LevelName

func LevelName(l slog.Level) string

func NewConsoleHandler

func NewConsoleHandler(w io.Writer, app string, min slog.Leveler, color bool) slog.Handler

func NewContextHandler

func NewContextHandler(next slog.Handler) slog.Handler

func NewJSONHandler

func NewJSONHandler(w io.Writer, app string, min slog.Leveler) slog.Handler

func ParseLevel

func ParseLevel(s string) (slog.Level, error)

func WithAttrs

func WithAttrs(ctx context.Context, attrs ...slog.Attr) context.Context

Types

type Config

type Config struct {
	LogDirectory    string `json:"logDirectory"`
	ZippedArchive   bool   `json:"zippedArchive"`
	MinimumLogLevel string `json:"minimumLogLevel" validate:"omitempty,oneof=error warn info debug verbose"`
	LogToFile       bool   `json:"logToFile"`
	LogToConsole    bool   `json:"logToConsole"`
	MaxFiles        int    `json:"maxFiles" validate:"min=0"`
	ConsoleColor    *bool  `json:"consoleColor,omitempty"`
}

func DefaultConfig

func DefaultConfig() Config

type DailyFile

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

func NewDailyFile

func NewDailyFile(o DailyFileOptions) (*DailyFile, error)

func (*DailyFile) Close

func (d *DailyFile) Close() error

func (*DailyFile) Write

func (d *DailyFile) Write(p []byte) (int, error)

type DailyFileOptions

type DailyFileOptions struct {
	Dir      string
	Pattern  string
	Symlink  string
	Compress bool
	MaxFiles int
	Now      func() time.Time
}

type Logger

type Logger struct {
	*slog.Logger
	// contains filtered or unexported fields
}

func New

func New(cfg Config, app string) (*Logger, error)

func (*Logger) Close

func (l *Logger) Close() error

func (*Logger) Verbose

func (l *Logger) Verbose(msg string, args ...any)

func (*Logger) VerboseContext

func (l *Logger) VerboseContext(ctx context.Context, msg string, args ...any)

Jump to

Keyboard shortcuts

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