logs

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: May 25, 2025 License: MIT Imports: 14 Imported by: 4

README

logs

Go Reference

Simple, pretty logger for your Go apps.

log := logs.Default()
log.WithGroup("grouped").Debug("debug line", "path", "console_test.go")
log.Info("some info")
log.Warn("some warning")
log.Error("an error", "err", errors.New("oh no"))

log screenshot

Features

  • Compatible with slog
  • Pretty console handler for terminals
  • Adds a level filter handler
  • Adds a concurrent multi-logger

Install

go get github.com/matthewmueller/logs

Example

// Go with the default logger
log := logs.Default()

// Or configure
log = logs.New(
  logs.Multi(
    logs.Filter(slog.LevelInfo, logs.Console(os.Stderr)),
    slog.NewJSONHandler(os.Stderr, nil),
  ),
)

log.WithGroup("hello").Debug("world", "args", 10)
log.Info("hello", "planet", "world", "args", 10)
log.Warn("hello", "planet", "world", "args", 10)
log.Error("hello world", "planet", "world", "args", 10)

// Integrates well with other libraries because log is still a *slog.Logger
var logger *slog.Logger = log
logger.WithGroup("hello").Debug("world", "args", 10)
logger.Info("hello", "planet", "world", "args", 10)
logger.Warn("hello", "planet", "world", "args", 10)
logger.Error("hello world", slog.String("planet", "world"), "args", 10)

Contributors

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	String   = slog.String
	Int      = slog.Int
	Int64    = slog.Int64
	Uint64   = slog.Uint64
	Float64  = slog.Float64
	Bool     = slog.Bool
	Any      = slog.Any
	Time     = slog.Time
	Duration = slog.Duration
)

Aliases for slog package types

View Source
var (
	// Aliases for slog package levels
	LevelDebug = slog.LevelDebug
	LevelInfo  = slog.LevelInfo
	LevelWarn  = slog.LevelWarn
	LevelError = slog.LevelError
)

Functions

func Discard added in v0.0.4

func Discard() *slog.Logger
Example
package main

import (
	"github.com/matthewmueller/logs"
)

func main() {
	log := logs.Discard()
	log.WithGroup("hello").Debug("world", "args", 10)
	log.Info("hello", "planet", "world", "args", 10)
	log.Warn("hello", "planet", "world", "args", 10)
	log.Error("hello world", logs.String("planet", "world"), "args", 10)
}

func Fatal added in v0.0.7

func Fatal(err error)

Fatal calls the default logger and exits with status code 1. Only intended for use in main functions or top-level handlers.

func Filter

func Filter(level slog.Level, handler slog.Handler) slog.Handler

Filter logs by level

Types

type Attr added in v0.0.7

type Attr = slog.Attr

func Err added in v0.0.7

func Err(err error) Attr

Err creates an error attribute for logging.

type ConsoleHandler

type ConsoleHandler struct {
	Color  color.Writer
	Source bool
	// contains filtered or unexported fields
}

ConsoleHandler handler for printing logs to the terminal

func Console

func Console(w io.Writer) *ConsoleHandler
Example
package main

import (
	"errors"
	"os"

	"github.com/livebud/color"
	"github.com/matthewmueller/logs"
)

func main() {
	console := logs.Console(os.Stdout)
	console.Color = color.Ignore()
	log := logs.New(console)
	log.WithGroup("grouped").Debug("debug line", "path", "console_test.go")
	log.Info("some info")
	log.Warn("some warning")
	log.Error("an error", "err", errors.New("oh no"))
}
Output:
debug: debug line grouped.path=console_test.go
info: some info
warn: some warning
error: an error err="oh no"

func (*ConsoleHandler) Enabled

Enabled is always set to true. Use log.Filter to filter out log levels

func (*ConsoleHandler) Handle

func (c *ConsoleHandler) Handle(ctx context.Context, record slog.Record) error

func (*ConsoleHandler) WithAttrs

func (c *ConsoleHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*ConsoleHandler) WithGroup

func (c *ConsoleHandler) WithGroup(group string) slog.Handler

type Handler

type Handler = slog.Handler

type Level added in v0.0.7

type Level = slog.Level

func MustParseLevel added in v0.0.7

func MustParseLevel(level string) Level

MustParseLevel parses a string into a log level and panics if it fails

func ParseLevel

func ParseLevel(level string) (Level, error)

ParseLevel parses a string into a log level

type Logger

type Logger = slog.Logger
Example
package main

import (
	"os"

	"github.com/livebud/color"
	"github.com/matthewmueller/logs"
)

func main() {
	console := logs.Console(os.Stdout)
	console.Color = color.Ignore()
	log := logs.New(console)
	log.WithGroup("hello").Debug("world", "args", 10)
	log.Info("hello", "planet", "world", "args", 10)
	log.Warn("hello", "planet", "world", "args", 10)
	log.Error("hello world", logs.String("planet", "world"), "args", 10)
}
Output:
debug: world hello.args=10
info: hello planet=world args=10
warn: hello planet=world args=10
error: hello world planet=world args=10

func Default

func Default() *Logger

Default logger writes to stderr at the info level by default. This can be adjusted by setting the LOG environment variable to one of the levels: debug, info, warn or error.

func New

func New(handler slog.Handler) *Logger

func Scope added in v0.0.5

func Scope(log *Logger) *Logger

Scope adds the funcion, filename and line number to the log

type MultiHandler

type MultiHandler []slog.Handler

func Multi

func Multi(handlers ...slog.Handler) MultiHandler
Example
package main

import (
	"log/slog"
	"os"

	"github.com/matthewmueller/logs"
)

func main() {
	console := logs.Console(os.Stderr)
	log := slog.New(logs.Multi(
		logs.Filter(slog.LevelInfo, console),
		slog.NewJSONHandler(os.Stderr, nil),
	))
	log.WithGroup("hello").Debug("world", "args", 10)
	log.Info("hello", "planet", "world", "args", 10)
	log.Warn("hello", "planet", "world", "args", 10)
	log.Error("hello world", "planet", "world", "args", 10)
}

func (MultiHandler) Enabled

func (handlers MultiHandler) Enabled(ctx context.Context, lvl slog.Level) bool

func (MultiHandler) Handle

func (handlers MultiHandler) Handle(ctx context.Context, record slog.Record) error

func (MultiHandler) WithAttrs

func (handlers MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (MultiHandler) WithGroup

func (handlers MultiHandler) WithGroup(group string) slog.Handler

type Record added in v0.0.7

type Record = slog.Record

Jump to

Keyboard shortcuts

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