logging

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package logging provides a logging system for the application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...any)

Debug emits a "DEBUG" level log message using the default logger. If more than one argument is provided, the first argument is used as a string format template and the remaining arguments are used as string formatting parameters.

func Error

func Error(args ...any)

Error emits an "ERROR" level log message using the default logger. If more than one argument is provided, the first argument is used as a string format template and the remaining arguments are used as string formatting parameters.

func Fatal

func Fatal(args ...any)

Fatal emits a "FATAL" level log message using the default logger. If more than one argument is provided, the first argument is used as a string format template and the remaining arguments are used as string formatting parameters.

func Info

func Info(args ...any)

Info emits an "INFO" level log message using the default logger. If more than one argument is provided, the first argument is used as a string format template and the remaining arguments are used as string formatting parameters.

func NewContext

func NewContext(ctx context.Context, logger Logger) context.Context

NewContext returns a new Context carrying logger.

func Panic

func Panic(args ...any)

Panic emits a "PANIC" level log message using the default and then panics. If more than one argument is provided, the first argument is used as a string format template and the remaining arguments are used as string formatting parameters.

func SetDefaultConfig

func SetDefaultConfig(config Config)

SetDefaultConfig sets the package's default logging configuration.

Parameters:

  • config: The configuration to set as the default.

func Warn

func Warn(args ...any)

Warn emits a "WARN" level log message using the default logger. If more than one argument is provided, the first argument is used as a string format template and the remaining arguments are used as string formatting parameters.

Types

type Config

type Config struct {
	// Level is the lowest level of log message that should be emitted. Any log
	// messages logged at the specified level or any level more severe will be
	// emitted. The default level is DEBUG.
	Level Level

	// Output is the destination for log messages. By default, it is os.Stderr.
	Output io.Writer
}

Config is a logging configuration.

type Fields

type Fields map[string]any

Fields is a map of strings to any type. It is used to pass to Logger.WithFields.

type Level

type Level uint8

A Level is a level of severity for a log message.

const (
	// DebugLevel causes a logger to emit messages logged at "DEBUG" level or more
	// severe. It is typically only enabled when debugging or during development,
	// and usually results in very verbose logging output.
	DebugLevel Level = iota

	// InfoLevel causes a logger to emit messages logged at "INFO" level or more
	// severe. It is typically used for general operational entries about what's
	// going on inside an application.
	InfoLevel

	// WarnLevel causes a logger to emit messages logged at "WARN" level or more
	// severe. It is typically used for non-critical entries that deserve attention.
	WarnLevel

	// ErrorLevel causes a logger to emit messages logged at "ERROR" level or more
	// severe. It is typically used for errors that should definitely be noted, and
	// is commonly used for hooks to send errors to an error tracking service.
	ErrorLevel

	// FatalLevel causes a logger to emit messages logged at "FATAL" level or more
	// severe. Messages logged at this level cause a logger to log the message and
	// then call os.Exit(1). It will exit even if the logging level is set to PanicLevel.
	FatalLevel

	// PanicLevel causes a logger to only emit messages logged at "PANIC" level.
	// It is the highest level of severity.
	PanicLevel
)

func (Level) String

func (l Level) String() string

String implements fmt.Stringer for Level.

type Logger

type Logger interface {
	// Debug emits a "DEBUG" level log message. If more than one argument is provided,
	// the first argument is used as a string format template and the remaining arguments
	// are used as string formatting parameters.
	Debug(args ...any)

	// Info emits an "INFO" level log message. If more than one argument is provided,
	// the first argument is used as a string format template and the remaining arguments
	// are used as string formatting parameters.
	Info(args ...any)

	// Warn emits a "WARN" level log message. If more than one argument is provided,
	// the first argument is used as a string format template and the remaining arguments
	// are used as string formatting parameters.
	Warn(args ...any)

	// Error emits an "ERROR" level log message. If more than one argument is provided,
	// the first argument is used as a string format template and the remaining arguments
	// are used as string formatting parameters.
	Error(args ...any)

	// Fatal emits a "FATAL" level log message. If more than one argument is provided,
	// the first argument is used as a string format template and the remaining arguments
	// are used as string formatting parameters.
	Fatal(args ...any)

	// Panic emits a "PANIC" level log message and then panics. If more than one argument
	// is provided, the first argument is used as a string format template and the remaining
	// arguments are used as string formatting parameters.
	Panic(args ...any)

	// WithField adds a field to the logger and returns a new Logger.
	WithField(key string, value any) Logger

	// WithFields adds multiple fields to the logger and returns a new Logger.
	WithFields(fields Fields) Logger

	// WithError adds a field called "error" to the logger and returns a new Logger.
	WithError(err error) Logger

	// Copy returns a copy of the logger. The copy will have the same fields as the
	// original logger.
	Copy() Logger
}

Logger provides methods for logging messages.

func FromContext

func FromContext(ctx context.Context) (logger Logger, created bool)

FromContext extracts the Logger from ctx, if present. If no Logger is present within the context, a new default Logger is created.

func NewDefaultLogger

func NewDefaultLogger() Logger

NewDefaultLogger returns a new logger that uses the package's default configuration.

Returns:

  • Logger: A new Logger configured with the default configuration.

func NewLogger

func NewLogger(config Config) Logger

NewLogger returns a new Logger.

Parameters:

  • config: The configuration for the logger.

Returns:

  • Logger: A new Logger configured with the given configuration.

func WithError

func WithError(err error) Logger

WithError adds a field called "error" to the default logger and returns a new Logger.

func WithField

func WithField(key string, value any) Logger

WithField adds a field to the default logger and returns a new Logger.

func WithFields

func WithFields(fields Fields) Logger

WithFields adds multiple fields to the default logger and returns a new Logger.

Jump to

Keyboard shortcuts

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