core

package
v0.7.4 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2025 License: MIT Imports: 2 Imported by: 21

Documentation

Overview

Package core provides the fundamental interfaces and types for mtlog.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddTyped

func AddTyped[T any](pb *PropertyBag, name string, value T)

AddTyped adds a typed property to the bag

func GetTyped

func GetTyped[T any](pb *PropertyBag, name string) (T, bool)

GetTyped retrieves a typed property from the bag

Types

type Capturer added in v0.6.0

type Capturer interface {
	// TryCapture attempts to capture a value into a log event property.
	// Returns the property and true if successful, nil and false otherwise.
	TryCapture(value any, propertyFactory LogEventPropertyFactory) (*LogEventProperty, bool)
}

Capturer converts complex types to log-appropriate representations.

type LogEvent

type LogEvent struct {
	// Timestamp is when the event occurred.
	Timestamp time.Time

	// Level is the severity of the event.
	Level LogEventLevel

	// MessageTemplate is the original message template with placeholders.
	MessageTemplate string

	// Properties contains the event's properties extracted from the template.
	Properties map[string]any

	// Exception associated with the event, if any.
	Exception error
}

LogEvent represents a single log event with all its properties.

func (*LogEvent) AddProperty

func (e *LogEvent) AddProperty(name string, value any)

AddProperty adds or overwrites a property in the event.

func (*LogEvent) AddPropertyIfAbsent

func (e *LogEvent) AddPropertyIfAbsent(property *LogEventProperty)

AddPropertyIfAbsent adds a property to the event if it doesn't already exist.

type LogEventEnricher

type LogEventEnricher interface {
	// Enrich adds properties to the provided log event.
	Enrich(event *LogEvent, propertyFactory LogEventPropertyFactory)
}

LogEventEnricher adds contextual properties to log events.

type LogEventFilter

type LogEventFilter interface {
	// IsEnabled returns true if the event should be logged.
	IsEnabled(event *LogEvent) bool
}

LogEventFilter determines which events proceed through the pipeline.

type LogEventLevel

type LogEventLevel int

LogEventLevel specifies the severity of a log event.

const (
	// VerboseLevel is the most detailed logging level.
	VerboseLevel LogEventLevel = iota

	// DebugLevel is for debugging information.
	DebugLevel

	// InformationLevel is for informational messages.
	InformationLevel

	// WarningLevel is for warnings.
	WarningLevel

	// ErrorLevel is for errors.
	ErrorLevel

	// FatalLevel is for fatal errors.
	FatalLevel
)

type LogEventProperty

type LogEventProperty struct {
	// Name is the property name.
	Name string

	// Value is the property value.
	Value any
}

LogEventProperty represents a single property of a log event.

type LogEventPropertyFactory

type LogEventPropertyFactory interface {
	// CreateProperty creates a new log event property.
	CreateProperty(name string, value any) *LogEventProperty
}

LogEventPropertyFactory creates log event properties.

type LogEventSink

type LogEventSink interface {
	// Emit writes the log event to the sink's destination.
	Emit(event *LogEvent)

	// Close releases any resources held by the sink.
	Close() error
}

LogEventSink outputs log events to a destination.

type LogValue

type LogValue interface {
	// LogValue returns the value to be logged. This can be a simple type
	// (string, number, bool) or a complex type (struct, map, slice).
	// The returned value may itself be captured if it's complex.
	LogValue() any
}

LogValue is an optional interface that types can implement to provide custom log representations. When a type implements this interface, the capturer will use the returned value instead of using reflection.

type Logger

type Logger interface {
	// Verbose writes a verbose-level log event.
	Verbose(messageTemplate string, args ...any)

	// Debug writes a debug-level log event.
	Debug(messageTemplate string, args ...any)

	// Information writes an information-level log event.
	Information(messageTemplate string, args ...any)

	// Warning writes a warning-level log event.
	Warning(messageTemplate string, args ...any)

	// Error writes an error-level log event.
	Error(messageTemplate string, args ...any)

	// Fatal writes a fatal-level log event.
	Fatal(messageTemplate string, args ...any)

	// Write writes a log event at the specified level.
	Write(level LogEventLevel, messageTemplate string, args ...any)

	// ForContext creates a logger that enriches events with the specified property.
	ForContext(propertyName string, value any) Logger

	// WithContext creates a logger that enriches events with context values.
	WithContext(ctx context.Context) Logger

	// IsEnabled returns true if events at the specified level would be processed.
	IsEnabled(level LogEventLevel) bool

	// Info writes an information-level log event (alias for Information).
	Info(messageTemplate string, args ...any)

	// Warn writes a warning-level log event (alias for Warning).
	Warn(messageTemplate string, args ...any)
}

Logger is the main logging interface providing structured logging methods.

type PropertyBag

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

PropertyBag is a type-safe property collection

func NewPropertyBag

func NewPropertyBag() *PropertyBag

NewPropertyBag creates a new property bag

func (*PropertyBag) Add

func (pb *PropertyBag) Add(name string, value any)

Add adds a typed property to the bag

func (*PropertyBag) Get

func (pb *PropertyBag) Get(name string) (any, bool)

Get retrieves a property from the bag

func (*PropertyBag) Properties

func (pb *PropertyBag) Properties() map[string]any

Properties returns all properties as a map

type PropertyValue

type PropertyValue[T any] struct {
	// contains filtered or unexported fields
}

PropertyValue represents a typed property value using generics

func NewPropertyValue

func NewPropertyValue[T any](value T) PropertyValue[T]

NewPropertyValue creates a new typed property value

func (PropertyValue[T]) ToLogEventProperty

func (p PropertyValue[T]) ToLogEventProperty(name string, factory LogEventPropertyFactory) *LogEventProperty

ToLogEventProperty converts to a LogEventProperty

func (PropertyValue[T]) Value

func (p PropertyValue[T]) Value() T

Value returns the underlying value

type SimpleSink

type SimpleSink interface {
	LogEventSink

	// EmitSimple writes a simple log message without allocations.
	EmitSimple(timestamp time.Time, level LogEventLevel, message string)
}

SimpleSink is an optional interface for sinks that support zero-allocation simple logging.

Jump to

Keyboard shortcuts

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