logging

package
v0.0.0-...-6a04f06 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const TEST_LOG = "TEST_LOG"

TEST_LOG specifies an env variable that can be used to override the log level.

Variables

View Source
var TestLevel = LevelError

TestLevel is the default log level for tests, you can change it in your modules for tests.

Functions

This section is empty.

Types

type ColorTheme

type ColorTheme struct {
	Time terminal.Color
	// Logger         terminal.Color
	FieldKey       terminal.Color
	FieldEqualSign terminal.Color
	FieldValue     terminal.Color
	Levels         map[Level]terminal.Color
}

ColorTheme specifies the terminal colors.

func DefaultColorTheme

func DefaultColorTheme() ColorTheme

DefaultColorTheme returns the default terminal colors.

func (ColorTheme) Level

func (th ColorTheme) Level(level Level) terminal.Color

Level returns a color for the given level.

type Config

type Config struct {
	Console *ConsoleConfig `json:"console"`
	File    *FileConfig    `json:"file"`
}

Config specifies the logging configuration.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a default configuration.

func ReadConfig

func ReadConfig(path string) (*Config, error)

ReadConfig reads a configuration from a JSON or YAML file.

type ConsoleConfig

type ConsoleConfig struct {
	Enabled bool       `json:"enabled"`
	Level   Level      `json:"level"`
	Color   bool       `json:"color"`
	Theme   ColorTheme `json:"theme"`
}

type Field

type Field struct {
	Key   string
	Value any
}

func NewField

func NewField(key string, value any) Field

NewField returns a new field.

func NewFields

func NewFields(keyValues ...any) []Field

NewFields builds fields from key/value pairs.

type FileConfig

type FileConfig struct {
	Enabled    bool   `yaml:"enabled"`
	Path       string `yaml:"path"`
	Level      Level  `yaml:"level"`
	MaxSize    int    `yaml:"max_size"`    // Maximum log file size in megabytes
	MaxAge     int    `yaml:"max_age"`     // Maximum days to retain old log files
	MaxBackups int    `yaml:"max_backups"` // Maximum number of old log files to retain
}

type Formatter

type Formatter interface {
	Format(w io.Writer, rec *Record) error
}

type Level

type Level int
const (
	LevelUndefined Level = iota
	LevelTrace
	LevelDebug
	LevelInfo
	LevelNotice
	LevelWarn
	LevelError
	LevelFatal
)

func LevelFromString

func LevelFromString(s string) Level

func TestLevelEnv

func TestLevelEnv() Level

TestLevelEnv returns a log level from the env variable TEST_LOG or the default test level.

func (Level) MarshalJSON

func (l Level) MarshalJSON() ([]byte, error)

func (Level) MarshalText

func (l Level) MarshalText() ([]byte, error)

func (Level) String

func (l Level) String() string

func (*Level) UnmarshalJSON

func (lptr *Level) UnmarshalJSON(b []byte) error

func (*Level) UnmarshalText

func (l *Level) UnmarshalText(b []byte) error

type Logger

type Logger interface {
	// Name returns the logger name.
	Name() string

	// Logger returns a child logger.
	Logger(name string) Logger

	// WithFields returns a chained logger with the default fields.
	WithFields(keyValuePairs ...any) Logger

	// Enabled returns true if a level is enabled.
	Enabled(level Level) bool

	// Begin returns a record builder with the info level.
	Begin() RecordBuilder

	// Write sets the logger if abset, adds the default fields and writes the record.
	Write(rec *Record) error

	// Trace logs a trace message.
	Trace(msg string, keyValues ...any)

	// TraceStatus logs a trace message with a status and a stack trace.
	TraceStatus(msg string, st status.Status, keyValues ...any)

	// TraceOn returns true if trace level is enabled.
	TraceOn() bool

	// Debug logs a debug message.
	Debug(msg string, keyValues ...any)

	// DebugStatus logs a debug message with a status and a stack trace.
	DebugStatus(msg string, st status.Status, keyValues ...any)

	// DebugOn returns true if debug level is enabled.
	DebugOn() bool

	// Info logs an info message.
	Info(msg string, keyValues ...any)

	// InfoStatus logs an info message with a status and a stack trace.
	InfoStatus(msg string, st status.Status, keyValues ...any)

	// InfoOn returns true if info level is enabled.
	InfoOn() bool

	// Notice logs a notice message.
	Notice(msg string, keyValues ...any)

	// NoticeStatus logs a notice message with a status and a stack trace.
	NoticeStatus(msg string, st status.Status, keyValues ...any)

	// NoticeOn return true if notice level is enabled.
	NoticeOn() bool

	// Warn logs a warning message.
	Warn(msg string, keyValues ...any)

	// WarnStatus logs a warning message with a status and a stack trace.
	WarnStatus(msg string, st status.Status, keyValues ...any)

	// WarnOn returns true if warn level is enabled.
	WarnOn() bool

	// Error logs an error message.
	Error(msg string, keyValues ...any)

	// ErrorStatus logs an error message with a status and a stack trace.
	ErrorStatus(msg string, st status.Status, keyValues ...any)

	// ErrorOn returns true if error level is enabled.
	ErrorOn() bool

	// Fatal logs a fatal mesage.
	Fatal(msg string, keyValues ...any)

	// FatalStatus logs a fatal message with a status and a stack trace.
	FatalStatus(msg string, st status.Status, keyValues ...any)

	// FatalOn returns true if fatal level is enabled.
	FatalOn() bool
}
var (
	Null   Logger = newLogger("null", true, newNullWriter())
	Stdout Logger = newLogger("main", true, newConsoleWriter(LevelDebug, true, os.Stdout))
	Stderr Logger = newLogger("main", true, newConsoleWriter(LevelDebug, true, os.Stderr))
)

func TestLogger

func TestLogger(t tests.T) Logger

TestLogger returns a new test logger.

func TestLoggerDebug

func TestLoggerDebug(t tests.T) Logger

TestLoggerDebug returns a new test logger with the debug level.

func TestLoggerInfo

func TestLoggerInfo(t tests.T) Logger

TestLoggerInfo returns a new test logger with the info level.

func TestLoggerLevel

func TestLoggerLevel(t tests.T, level Level) Logger

TestLoggerLevel returns a new test logger with the specified level.

type Logging

type Logging interface {
	// Main returns the main logger.
	Main() Logger

	// Logger returns a logger with the given name or creates a new one.
	Logger(name string) Logger

	// Enabled returns true if logging is enabled for the given level.
	Enabled(level Level) bool

	// Write writes a record.
	Write(rec *Record) error
}

Logging is a logging service.

func Default

func Default() Logging

Default returns a new logging service with the default config.

func Init

func Init(config *Config) (Logging, error)

Init initializes and returns a new logging service, uses the default config when config is nil.

func Load

func Load(path string) (Logging, error)

Load loads a logging config from a JSON/YAML file if it exists and returns a new logging service.

func Test

func Test(t tests.T) Logging

Test returns a new test logging service.

type PrefixLogger

type PrefixLogger interface {
	Logger

	// SetPrefix sets the logger prefix.
	SetPrefix(s string)

	// ClearPrefix clears the logger prefix.
	ClearPrefix()
}

PrefixLogger is a logger that adds a prefix to messages, including messages from child loggers.

func NewPrefixLogger

func NewPrefixLogger(logger Logger) PrefixLogger

NewPrefixLogger returns a new prefix logger.

type Record

type Record struct {
	Time    time.Time `json:"time"`
	Level   Level     `json:"level"`
	Logger  string    `json:"logger"`
	Message string    `json:"message"`
	Fields  []Field   `json:"fields"`
	Stack   []byte    `json:"stack"`
}

func NewRecord

func NewRecord(logger string, level Level) *Record

NewRecord returns a new record with the current time.

func (*Record) WithField

func (r *Record) WithField(key string, value any) *Record

func (*Record) WithFieldf

func (r *Record) WithFieldf(key string, format string, a ...any) *Record

func (*Record) WithFields

func (r *Record) WithFields(keysValues ...any) *Record

func (*Record) WithLevel

func (r *Record) WithLevel(lv Level) *Record

func (*Record) WithLogger

func (r *Record) WithLogger(logger string) *Record

func (*Record) WithMessage

func (r *Record) WithMessage(msg string) *Record

func (*Record) WithMessagef

func (r *Record) WithMessagef(msg string, args ...any) *Record

func (*Record) WithStack

func (r *Record) WithStack(stack []byte) *Record

func (*Record) WithStatus

func (r *Record) WithStatus(st status.Status) *Record

func (*Record) WithTime

func (r *Record) WithTime(t time.Time) *Record

type RecordBuilder

type RecordBuilder interface {
	// Build builds and returns the record, but does not send it.
	Build() *Record

	// Send sends the record.
	Send()

	// Level sets the record level.
	Level(lv Level) RecordBuilder

	// Message sets the record message.
	Message(msg string) RecordBuilder

	// Messagef formats and sets the record message.
	Messagef(msg string, args ...any) RecordBuilder

	// Stack adds a stack trace.
	Stack(stack []byte) RecordBuilder

	// Status adds a status with an optional stack trace.
	Status(st status.Status) RecordBuilder

	// Field adds a field to the record.
	Field(key string, value any) RecordBuilder

	// Fieldf formats a field value and adds a field to the record.
	Fieldf(key string, format string, a ...any) RecordBuilder

	// Fields adds fields to the record.
	Fields(keyValuePairs ...any) RecordBuilder

	// Trace sets the level to trace and adds the message.
	Trace(msg string) RecordBuilder

	// Tracef sets the level to trace and formats the message.
	Tracef(format string, args ...any) RecordBuilder

	// Debug sets the level to debug and adds the message.
	Debug(msg string) RecordBuilder

	// Debugf sets the level to debug and formats the message.
	Debugf(format string, args ...any) RecordBuilder

	// Info sets the level to info and adds the message.
	Info(msg string) RecordBuilder

	// Infof sets the level to info and formats the message.
	Infof(format string, args ...any) RecordBuilder

	// Notice sets the level to notice and adds the message.
	Notice(msg string) RecordBuilder

	// Noticef sets the level to notice and formats the message.
	Noticef(format string, args ...any) RecordBuilder

	// Warn sets the level to warn and adds the message.
	Warn(msg string) RecordBuilder

	// Warnf sets the level to warn and formats the message.
	Warnf(format string, args ...any) RecordBuilder

	// Error sets the level to error and adds the message.
	Error(msg string) RecordBuilder

	// Errorf sets the level to error and formats the message.
	Errorf(format string, args ...any) RecordBuilder

	// Fatal sets the level to fatal and adds the message.
	Fatal(msg string) RecordBuilder

	// Fatalf sets the level to fatal and formats the message.
	Fatalf(format string, args ...any) RecordBuilder
}

type Writer

type Writer interface {
	// Enabled returns true if the writer is enabled for the given level.
	Enabled(level Level) bool

	// Write writes a record.
	Write(rec *Record) error
}

Writer writes log records.

Jump to

Keyboard shortcuts

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