logger

package
v1.8.1-0...-100120b Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatStrings

func FormatStrings() []string

FormatStrings returns a slice of all logging formats as strings.

func LevelStrings

func LevelStrings() []string

LevelStrings returns a slice of all logging levels as strings.

func NewStdLog

func NewStdLog(log *Logger) *stdLog.Logger

NewStdLog returns a *log.Logger which writes to the supplied Logger at InfoLevel.

Types

type Attr

type Attr = slog.Attr

An Attr is a key-value pair.

func Any

func Any(key string, v any) Attr

Any returns an Attr for any value.

func Bool

func Bool(key string, v bool) Attr

Bool returns an Attr for a bool.

func Duration

func Duration(key string, v time.Duration) Attr

Duration returns an Attr for a time.Duration.

func Error

func Error(err error) Attr

Error returns an Attr for an error.

func Float64

func Float64(key string, v float64) Attr

Float64 returns an Attr for a floating-point number.

func Int

func Int(key string, value int) Attr

Int converts an int to an int64 and returns an Attr with that value.

func Int64

func Int64(key string, value int64) Attr

Int64 returns an Attr for an int64.

func String

func String(key, value string) Attr

String returns an Attr for a string value.

func Strings

func Strings(key string, value ...string) Attr

Strings returns an Attr for a slice of strings.

func Time

func Time(key string, v time.Time) Attr

Time returns an Attr for a time.Time. It discards the monotonic portion.

func Uint16

func Uint16(key string, v uint16) Attr

Uint16 returns an Attr for an uint16.

func Uint64

func Uint64(key string, v uint64) Attr

Uint64 returns an Attr for an uint64.

type Format

type Format uint8

A Format is a logging format.

const (
	ConsoleFormat Format = iota // useful for console output (for humans)
	JSONFormat                  // useful for logging aggregation systems (for robots)
)

func Formats

func Formats() []Format

Formats returns a slice of all logging formats.

func ParseFormat

func ParseFormat[T string | []byte](text T) (Format, error)

ParseFormat parses a format (case is ignored) based on the ASCII representation of the log format. If the provided ASCII representation is invalid an error is returned.

This is particularly useful when dealing with text input to configure log formats.

func (Format) String

func (f Format) String() string

String returns a lower-case ASCII representation of the log format.

type Level

type Level int8

A Level is a logging level.

const (
	DebugLevel Level = iota - 1
	InfoLevel        // default level (zero-value)
	WarnLevel
	ErrorLevel
)

func Levels

func Levels() []Level

Levels returns a slice of all logging levels.

func ParseLevel

func ParseLevel[T string | []byte](text T) (Level, error)

ParseLevel parses a level (case is ignored) based on the ASCII representation of the log level. If the provided ASCII representation is invalid an error is returned.

This is particularly useful when dealing with text input to configure log levels.

func (Level) String

func (l Level) String() string

String returns a lower-case ASCII representation of the log level.

type Logger

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

Logger is a simple logger that wraps slog.Logger. It provides a more convenient API for logging and formatting messages.

func New

func New(l Level, f Format, writer ...io.Writer) (*Logger, error)

New creates a new logger with the given level and format. Optionally, you can specify the writer to write logs to.

func NewNop

func NewNop() *Logger

NewNop returns a no-op Logger. It never writes out logs or internal errors. The common use case is to use it in tests.

func (*Logger) Debug

func (l *Logger) Debug(msg string, f ...Attr)

Debug logs a message at DebugLevel.

func (*Logger) Error

func (l *Logger) Error(msg string, f ...Attr)

Error logs a message at ErrorLevel.

func (*Logger) Info

func (l *Logger) Info(msg string, f ...Attr)

Info logs a message at InfoLevel.

func (*Logger) Level

func (l *Logger) Level() Level

Level returns the logger level.

func (*Logger) Named

func (l *Logger) Named(name string) *Logger

Named creates a new logger with the same properties as the original logger and the given name.

func (*Logger) Warn

func (l *Logger) Warn(msg string, f ...Attr)

Warn logs a message at WarnLevel.

Jump to

Keyboard shortcuts

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