logger

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Example
slog.SetDefault(slog.New(newColorHandler(os.Stderr, &LoggerOptions{
	Level:        slog.LevelDebug,
	ColorEnabled: true,
})))

slog.Info("Starting server", "addr", ":8080", "env", "production")
slog.Debug("Connected to DB", "db", "myapp", "host", "localhost:5432")
slog.Warn("Slow request", "method", "GET", "path", "/users", "duration", 497*time.Millisecond)
slog.Error("DB connection lost", Err(errors.New("connection reset")), "db", "myapp")

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	DefaultLogLevel  = slog.LevelDebug
	DefaultWriter    = os.Stdout
	DefaultAddSource = true

	NoRepeatInterval  = 3600 * time.Hour // arbitrarily long time to denote one-time sampling
	DefaultTimeFormat = "2006 Jan 02 15:04:05"
)

Functions

func Color

func Color(text string, color chalk.Color) string

func ColorEnabled

func ColorEnabled() bool

func Err

func Err(e error) slog.Attr

func New

func New(opts ...LoggerOption) *slog.Logger

func NewNop

func NewNop() *slog.Logger

func ParseLevel

func ParseLevel(lvl string) slog.Level

func TextStyle

func TextStyle(text string, textStyle chalk.TextStyle) string

Types

type LoggerOption

type LoggerOption func(*LoggerOptions)

func WithColor

func WithColor(color bool) LoggerOption

func WithDisableCaller

func WithDisableCaller() LoggerOption

func WithLogLevel

func WithLogLevel(l slog.Level) LoggerOption

func WithOmitLoggerName

func WithOmitLoggerName() LoggerOption

func WithTimeFormat

func WithTimeFormat(format string) LoggerOption

func WithWriter

func WithWriter(w io.Writer) LoggerOption

type LoggerOptions

type LoggerOptions struct {
	Level          slog.Level
	AddSource      bool
	ReplaceAttr    func(groups []string, a slog.Attr) slog.Attr
	Writer         io.Writer
	ColorEnabled   bool
	Sampling       *slogsampling.ThresholdSamplingOption
	TimeFormat     string
	OmitLoggerName bool
}

Jump to

Keyboard shortcuts

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