logs

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2025 License: MIT Imports: 14 Imported by: 2

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

This section is empty.

Functions

func Debug added in v0.0.6

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

Debug calls [Logger.Debug] on the default logger.

func DebugContext added in v0.0.6

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

DebugContext calls [Logger.DebugContext] on the default logger.

func Error added in v0.0.6

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

Error calls [Logger.Error] on the default logger.

func ErrorContext added in v0.0.6

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

ErrorContext calls [Logger.ErrorContext] on the default logger.

func Info added in v0.0.6

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

Info calls [Logger.Info] on the default logger.

func InfoContext added in v0.0.6

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

InfoContext calls [Logger.InfoContext] on the default logger.

func ParseLevel

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

ParseLevel parses a string into a log level

func Warn added in v0.0.6

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

Warn calls [Logger.Warn] on the default logger.

func WarnContext added in v0.0.6

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

WarnContext calls [Logger.WarnContext] on the default logger.

Types

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 FilterHandler

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

func Filter

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

Filter logs by level

func (*FilterHandler) Enabled

func (f *FilterHandler) Enabled(ctx context.Context, l slog.Level) bool

func (*FilterHandler) Handle

func (f *FilterHandler) Handle(ctx context.Context, record slog.Record) error

func (*FilterHandler) WithAttrs

func (f *FilterHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*FilterHandler) WithGroup

func (f *FilterHandler) WithGroup(group string) slog.Handler

type Handler

type Handler = slog.Handler

type Logger

type Logger = slog.Logger
Example
package main

import (
	"log/slog"
	"os"

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

func main() {
	console := logs.Console(os.Stdout)
	console.Color = color.Ignore()
	log := logs.New(console)
	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)
}
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
Example
package main

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

func main() {
	logs.Info("hello", "planet", "world", "args", 10)
	// No output because default logger logs to stderr
	
Output:

func Discard added in v0.0.4

func Discard() *Logger
Example
package main

import (
	"log/slog"

	"github.com/matthewmueller/logs"
)

func main() {
	log := logs.Discard()
	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)
}
Output:

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)
}
Output:

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

Jump to

Keyboard shortcuts

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