klog

package module
v0.1.1-0...-9eb9fca Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: MPL-2.0 Imports: 11 Imported by: 11

README

klog

logger with context

import via xorkevin.dev/klog

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func CtxWithAttrs

func CtxWithAttrs(ctx context.Context, attrs ...Attr) context.Context

CtxWithAttrs adds log attrs to context

func ExtendCtx

func ExtendCtx(dest, ctx context.Context, attrs ...Attr) context.Context

ExtendCtx adds log attrs to context

Types

type Attr

type Attr = slog.Attr

func AAny

func AAny(key string, value any) Attr

func ABool

func ABool(key string, value bool) Attr

func ADuration

func ADuration(key string, value time.Duration) Attr

func AFloat64

func AFloat64(key string, value float64) Attr

func AGroup

func AGroup(key string, attrs ...Attr) Attr

func AInt

func AInt(key string, value int) Attr

func AInt64

func AInt64(key string, value int64) Attr

func AString

func AString(key string, value string) Attr

func ATime

func ATime(key string, value time.Time) Attr

func AUint64

func AUint64(key string, value uint64) Attr

type Clock

type Clock interface {
	Time() time.Time
}

Clock returns the current and monotonic time

type Discard

type Discard struct{}

Discard is a Logger that discards logs

func (Discard) Enabled

func (d Discard) Enabled(ctx context.Context, level Level) bool

func (Discard) Handler

func (d Discard) Handler() Handler

func (Discard) Log

func (d Discard) Log(ctx context.Context, level Level, skip int, msg string, attrs ...Attr)

func (Discard) Sublogger

func (d Discard) Sublogger(modSegment string, attrs ...Attr) Logger

type DiscardHandler

type DiscardHandler struct{}

DiscardHandler is a Handler that discards logs

func (DiscardHandler) Enabled

func (d DiscardHandler) Enabled(ctx context.Context, level Level) bool

func (DiscardHandler) Handle

func (d DiscardHandler) Handle(ctx context.Context, rec Record) error

func (DiscardHandler) Subhandler

func (d DiscardHandler) Subhandler(modSegment string, attrs []Attr) Handler

type Handler

type Handler interface {
	Enabled(ctx context.Context, level Level) bool
	Handle(ctx context.Context, rec Record) error
	Subhandler(modSegment string, attrs []Attr) Handler
}

Handler is a log event handler

type KLogger

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

KLogger is a context logger that writes logs to a Handler

func New

func New(opts ...LoggerOpt) *KLogger

New creates a new Logger

func (*KLogger) Enabled

func (l *KLogger) Enabled(ctx context.Context, level Level) bool

Enabled implements Logger and returns if the logger is enabled for a level

func (*KLogger) Handler

func (l *KLogger) Handler() Handler

Handler implements Logger and returns the handler

func (*KLogger) Log

func (l *KLogger) Log(ctx context.Context, level Level, skip int, msg string, attrs ...Attr)

Log implements Logger and logs an event to its handler

func (*KLogger) Sublogger

func (l *KLogger) Sublogger(modSegment string, attrs ...Attr) Logger

Sublogger implements [SubLogger] and creates a new sublogger

type Level

type Level = slog.Level

type LevelLogger

type LevelLogger struct {
	Logger Logger
	Skip   int
}

LevelLogger provides convenience methods to log at particular levels

func NewLevelLogger

func NewLevelLogger(l Logger) *LevelLogger

NewLevelLogger creates a new *LevelLogger

func (*LevelLogger) Debug

func (l *LevelLogger) Debug(ctx context.Context, msg string, attrs ...Attr)

Debug logs at LevelDebug

func (*LevelLogger) Err

func (l *LevelLogger) Err(ctx context.Context, err error, attrs ...Attr)

Err logs an error LevelError

func (*LevelLogger) Error

func (l *LevelLogger) Error(ctx context.Context, msg string, attrs ...Attr)

Error logs at LevelError

func (*LevelLogger) Info

func (l *LevelLogger) Info(ctx context.Context, msg string, attrs ...Attr)

Info logs at LevelInfo

func (*LevelLogger) Warn

func (l *LevelLogger) Warn(ctx context.Context, msg string, attrs ...Attr)

Warn logs at LevelWarn

func (*LevelLogger) WarnErr

func (l *LevelLogger) WarnErr(ctx context.Context, err error, attrs ...Attr)

WarnErr logs at LevelWarn

type Logger

type Logger interface {
	Enabled(ctx context.Context, level Level) bool
	Log(ctx context.Context, level Level, skip int, msg string, attrs ...Attr)
	Handler() Handler
	Sublogger(modSegment string, attrs ...Attr) Logger
}

Logger writes logs with context

type LoggerOpt

type LoggerOpt = func(l *KLogger)

LoggerOpt is an options function for New

func OptClock

func OptClock(c Clock) LoggerOpt

OptClock returns a LoggerOpt that sets KLogger clock

func OptHandler

func OptHandler(h Handler) LoggerOpt

OptHandler returns a LoggerOpt that sets KLogger handler

func OptMinLevel

func OptMinLevel(level Level) LoggerOpt

OptMinLevel returns a LoggerOpt that sets KLogger minLevel

func OptMinLevelStr

func OptMinLevelStr(s string) LoggerOpt

OptMinLevelStr returns a LoggerOpt that sets KLogger minLevel from a string

func OptSubhandler

func OptSubhandler(modSegment string, attrs ...Attr) LoggerOpt

OptSubhandler returns a LoggerOpt that sets KLogger handler

type RealTime

type RealTime struct {
}

RealTime is a real time clock

func (RealTime) Time

func (c RealTime) Time() time.Time

type Record

type Record = slog.Record

func NewRecord

func NewRecord(t time.Time, level Level, msg string, pc uintptr) Record

type SlogHandler

type SlogHandler struct {
	FieldTimeInfo string
	FieldCaller   string
	FieldMod      string
	ModSeparator  string
	Mod           string
	// contains filtered or unexported fields
}

SlogHandler writes logs to an slog.Handler

func NewJSONSlogHandler

func NewJSONSlogHandler(w io.Writer) *SlogHandler

func NewSlogHandler

func NewSlogHandler(handler slog.Handler) *SlogHandler

NewSlogHandler creates a new *SlogHandler

func NewTextSlogHandler

func NewTextSlogHandler(w io.Writer) *SlogHandler

func (*SlogHandler) Enabled

func (h *SlogHandler) Enabled(ctx context.Context, level Level) bool

func (*SlogHandler) Handle

func (h *SlogHandler) Handle(ctx context.Context, r Record) error

func (*SlogHandler) Subhandler

func (h *SlogHandler) Subhandler(modSegment string, attrs []Attr) Handler

type SyncWriter

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

SyncWriter is a thread safe writer

func NewSyncWriter

func NewSyncWriter(w io.Writer) *SyncWriter

NewSyncWriter creates a new *SyncWriter

func (*SyncWriter) Write

func (w *SyncWriter) Write(p []byte) (int, error)

Write implements io.Writer

Jump to

Keyboard shortcuts

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