log

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 13 Imported by: 1

README

Log

Go Reference

Log utilities for the upcoming log/slog package in the Go v1.21.

log

Features

  • Built on top of the new log/slog package
  • Pretty console handler for terminals
  • Adds a level filter handler
  • Adds a multi-logger

Install

go get github.com/livebud/log

Note: This package depends on log/slog, which is only available for Go v1.21+.

Example

log := log.Multi(
  log.Filter(log.LevelInfo, &log.Console{Writer: 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)

Contributors

License

MIT

Documentation

Index

Examples

Constants

View Source
const (
	LevelDebug = slog.LevelDebug
	LevelInfo  = slog.LevelInfo
	LevelWarn  = slog.LevelWarn
	LevelError = slog.LevelError
)

Variables

This section is empty.

Functions

func Debug

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

func DebugContext

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

func Error

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

func ErrorContext

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

func Info

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

func InfoContext

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

func Provider

func Provider(in di.Injector)

func Warn

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

func WarnContext

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

Types

type Console

type Console struct {
	Color     color.Writer
	Writer    io.Writer
	AddSource bool
	// contains filtered or unexported fields
}

Console handler for printing logs to the terminal

Example
package main

import (
	"os"

	"log/slog"

	"github.com/livebud/color"
	"github.com/livebud/log"
)

func main() {
	log := slog.New(&log.Console{
		Color:     color.Ignore(),
		Writer:    os.Stdout,
		AddSource: false,
	})
	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", 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 (*Console) Enabled

func (c *Console) Enabled(context.Context, Level) bool

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

func (*Console) Handle

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

func (*Console) WithAttrs

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

func (*Console) WithGroup

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

type Handler

type Handler = slog.Handler

func Filter

func Filter(level Level, handler Handler) Handler

Filter logs by level

type Level

type Level = slog.Level

func ParseLevel

func ParseLevel(level string) (Level, error)

ParseLevel parses a string into a log level

type Logger

type Logger = slog.Logger

func Default

func Default() *Logger

func Multi

func Multi(handlers ...slog.Handler) *Logger
Example
package main

import (
	"log/slog"
	"os"

	"github.com/livebud/log"
)

func main() {
	log := log.Multi(
		log.Filter(log.LevelInfo, &log.Console{Writer: 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)
}

func New

func New(handler Handler) *Logger

New logger

Jump to

Keyboard shortcuts

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