Documentation
¶
Overview ¶
Package logging provides a flexible and extensible logging framework with support for multiple adapters and log levels. It offers a structured approach to logging with configurable output destinations and severity levels.
The package supports various logging adapters including standard library logger, zerolog, and no-op implementations. It provides a global logger registry for convenient access across applications and supports hierarchical log levels from trace to fatal.
Example usage:
import "github.com/valentin-kaiser/go-core/logging"
logger := logging.GetGlobalAdapter()
logger.Info().Str("key", "value").Msg("Application started")
Index ¶
- func Anonymous(a bool)
- func Debug(d bool)
- func DisablePackage(pkg string)
- func EnablePackage(pkg string)
- func ListPackages() []string
- func SetGlobalAdapter(adapter Adapter)
- func SetPackageAdapter(pkg string, adapter Adapter)
- func SetPackageLevel(pkg string, level Level)
- type Adapter
- func GetGlobalAdapter() Adapter
- func GetPackageLogger(pkg string) Adapter
- func NewDynamicAdapter(pkg string) Adapter
- func NewNoOpAdapter() Adapter
- func NewStandardAdapter() Adapter
- func NewStandardAdapterWithLogger(logger *log.Logger) Adapter
- func NewZerologAdapter() Adapter
- func NewZerologAdapterWithLogger(logger zerolog.Logger) Adapter
- type DynamicAdapter
- func (d *DynamicAdapter) Debug() Event
- func (d *DynamicAdapter) Enabled() bool
- func (d *DynamicAdapter) Error() Event
- func (d *DynamicAdapter) Fatal() Event
- func (d *DynamicAdapter) GetLevel() Level
- func (d *DynamicAdapter) Info() Event
- func (d *DynamicAdapter) Panic() Event
- func (d *DynamicAdapter) Printf(format string, v ...interface{})
- func (d *DynamicAdapter) SetLevel(level Level) Adapter
- func (d *DynamicAdapter) Trace() Event
- func (d *DynamicAdapter) Warn() Event
- func (d *DynamicAdapter) WithPackage(pkg string) Adapter
- type Event
- type Field
- type Level
- type NoOpAdapter
- func (n *NoOpAdapter) Debug() Event
- func (n *NoOpAdapter) Enabled() bool
- func (n *NoOpAdapter) Error() Event
- func (n *NoOpAdapter) Fatal() Event
- func (n *NoOpAdapter) GetLevel() Level
- func (n *NoOpAdapter) Info() Event
- func (n *NoOpAdapter) Panic() Event
- func (n *NoOpAdapter) Printf(_ string, _ ...interface{})
- func (n *NoOpAdapter) SetLevel(_ Level) Adapter
- func (n *NoOpAdapter) Trace() Event
- func (n *NoOpAdapter) Warn() Event
- func (n *NoOpAdapter) WithPackage(_ string) Adapter
- type NoOpEvent
- type StandardAdapter
- func (s *StandardAdapter) Debug() Event
- func (s *StandardAdapter) Enabled() bool
- func (s *StandardAdapter) Error() Event
- func (s *StandardAdapter) Fatal() Event
- func (s *StandardAdapter) GetLevel() Level
- func (s *StandardAdapter) Info() Event
- func (s *StandardAdapter) Panic() Event
- func (s *StandardAdapter) Printf(format string, v ...interface{})
- func (s *StandardAdapter) SetLevel(level Level) Adapter
- func (s *StandardAdapter) Trace() Event
- func (s *StandardAdapter) Warn() Event
- func (s *StandardAdapter) WithPackage(pkg string) Adapter
- type StandardEvent
- type ZerologAdapter
- func (z *ZerologAdapter) Debug() Event
- func (z *ZerologAdapter) Enabled() bool
- func (z *ZerologAdapter) Error() Event
- func (z *ZerologAdapter) Fatal() Event
- func (z *ZerologAdapter) GetLevel() Level
- func (z *ZerologAdapter) Info() Event
- func (z *ZerologAdapter) Panic() Event
- func (z *ZerologAdapter) Printf(format string, v ...interface{})
- func (z *ZerologAdapter) SetLevel(level Level) Adapter
- func (z *ZerologAdapter) Trace() Event
- func (z *ZerologAdapter) Warn() Event
- func (z *ZerologAdapter) WithPackage(pkg string) Adapter
- type ZerologEvent
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DisablePackage ¶
func DisablePackage(pkg string)
DisablePackage disables logging for a specific package
func EnablePackage ¶
func EnablePackage(pkg string)
EnablePackage removes package-specific adapter, falling back to global
func ListPackages ¶
func ListPackages() []string
ListPackages returns all packages that have specific adapters
func SetGlobalAdapter ¶
func SetGlobalAdapter(adapter Adapter)
SetGlobalAdapter sets the global logging adapter for all packages This will be used as the default for all packages unless they have a specific adapter
func SetPackageAdapter ¶
SetPackageAdapter sets a specific adapter for a package This overrides the global adapter for the specified package
func SetPackageLevel ¶
SetPackageLevel sets the log level for a specific package If the package doesn't have a specific adapter, this creates one based on the global adapter
Types ¶
type Adapter ¶
type Adapter interface {
// Level control
SetLevel(level Level) Adapter
GetLevel() Level
Trace() Event
Debug() Event
Info() Event
Warn() Event
Error() Event
Fatal() Event
Panic() Event
Printf(format string, v ...interface{})
// Package-specific logger
WithPackage(pkg string) Adapter
// Enabled returns true if logging is enabled for this adapter, false otherwise.
// This can be used to skip expensive log message construction when logging is disabled.
Enabled() bool
}
Adapter defines the interface for internal logging
func GetGlobalAdapter ¶
func GetGlobalAdapter() Adapter
GetGlobalAdapter returns the current global adapter
func GetPackageLogger ¶
GetPackageLogger returns a logger for a specific package Returns a dynamic adapter that will always use the current global/package-specific adapter
func NewDynamicAdapter ¶
NewDynamicAdapter creates a dynamic adapter for a package
func NewStandardAdapter ¶
func NewStandardAdapter() Adapter
NewStandardAdapter creates a new standard log adapter with the default logger
func NewStandardAdapterWithLogger ¶
NewStandardAdapterWithLogger creates a new standard log adapter with a custom logger
func NewZerologAdapter ¶
func NewZerologAdapter() Adapter
NewZerologAdapter creates a new zerolog adapter with the global zerolog logger
func NewZerologAdapterWithLogger ¶
NewZerologAdapterWithLogger creates a new zerolog adapter with a custom logger
type DynamicAdapter ¶
type DynamicAdapter struct {
// contains filtered or unexported fields
}
DynamicAdapter wraps the package lookup to always use the current adapter
func (*DynamicAdapter) Debug ¶
func (d *DynamicAdapter) Debug() Event
Debug returns a debug level event from the current active adapter.
func (*DynamicAdapter) Enabled ¶ added in v1.6.0
func (d *DynamicAdapter) Enabled() bool
Enabled returns whether logging is enabled for the current active adapter.
func (*DynamicAdapter) Error ¶
func (d *DynamicAdapter) Error() Event
Error returns an error level event from the current active adapter.
func (*DynamicAdapter) Fatal ¶
func (d *DynamicAdapter) Fatal() Event
Fatal returns a fatal level event from the current active adapter.
func (*DynamicAdapter) GetLevel ¶
func (d *DynamicAdapter) GetLevel() Level
GetLevel returns the current log level from the active adapter for this package.
func (*DynamicAdapter) Info ¶
func (d *DynamicAdapter) Info() Event
Info returns an info level event from the current active adapter.
func (*DynamicAdapter) Panic ¶
func (d *DynamicAdapter) Panic() Event
Panic returns a panic level event from the current active adapter.
func (*DynamicAdapter) Printf ¶
func (d *DynamicAdapter) Printf(format string, v ...interface{})
Printf logs a formatted message using the current active adapter.
func (*DynamicAdapter) SetLevel ¶
func (d *DynamicAdapter) SetLevel(level Level) Adapter
SetLevel sets the log level for this dynamic adapter by delegating to the current active adapter.
func (*DynamicAdapter) Trace ¶
func (d *DynamicAdapter) Trace() Event
Trace returns a trace level event from the current active adapter.
func (*DynamicAdapter) Warn ¶
func (d *DynamicAdapter) Warn() Event
Warn returns a warn level event from the current active adapter.
func (*DynamicAdapter) WithPackage ¶
func (d *DynamicAdapter) WithPackage(pkg string) Adapter
WithPackage returns a new adapter instance for the specified package from the current active adapter.
type Event ¶
type Event interface {
// Add fields to the event
Fields(fields ...Field) Event
Field(key string, value interface{}) Event
// Add an error to the event
Err(err error) Event
// Log the message
Msg(msg string)
// Log the formatted message
Msgf(format string, v ...interface{})
}
Event represents a log event with a fluent interface
type Field ¶
type Field struct {
Key string
Value interface{}
}
Field represents a structured log field
type Level ¶
type Level int
Level represents log levels
const ( TraceLevel Level = -1 // TraceLevel logs very detailed diagnostic information DebugLevel Level = 0 // DebugLevel logs debug information useful for development InfoLevel Level = 1 // InfoLevel logs general information about application execution WarnLevel Level = 2 // WarnLevel logs warnings about potentially harmful situations ErrorLevel Level = 3 // ErrorLevel logs error events that might still allow the application to continue FatalLevel Level = 4 // FatalLevel logs very severe error events that will presumably lead the application to abort PanicLevel Level = 5 // PanicLevel logs error events that will cause the application to panic DisabledLevel Level = 6 // DisabledLevel disables all logging )
Log level constants define the severity levels for logging operations. These levels are ordered from most verbose (TraceLevel) to least verbose (DisabledLevel).
func GetPackageLevel ¶
GetPackageLevel returns the log level for a specific package
type NoOpAdapter ¶
type NoOpAdapter struct{}
NoOpAdapter implements LogAdapter but does nothing This is the default implementation for minimal overhead when logging is disabled
func (*NoOpAdapter) Enabled ¶ added in v1.6.0
func (n *NoOpAdapter) Enabled() bool
Enabled returns false indicating logging is disabled
func (*NoOpAdapter) GetLevel ¶
func (n *NoOpAdapter) GetLevel() Level
GetLevel returns the current log level
func (*NoOpAdapter) Printf ¶
func (n *NoOpAdapter) Printf(_ string, _ ...interface{})
Printf does nothing
func (*NoOpAdapter) SetLevel ¶
func (n *NoOpAdapter) SetLevel(_ Level) Adapter
SetLevel sets the log level (no-op)
func (*NoOpAdapter) WithPackage ¶
func (n *NoOpAdapter) WithPackage(_ string) Adapter
WithPackage returns the same no-op adapter
type NoOpEvent ¶
type NoOpEvent struct{}
NoOpEvent implements Event interface but does nothing
type StandardAdapter ¶
type StandardAdapter struct {
// contains filtered or unexported fields
}
StandardAdapter implements LogAdapter using Go's standard log package
func (*StandardAdapter) Debug ¶
func (s *StandardAdapter) Debug() Event
Debug returns a debug level event
func (*StandardAdapter) Enabled ¶ added in v1.6.0
func (s *StandardAdapter) Enabled() bool
Enabled returns true if the adapter's log level is not DisabledLevel
func (*StandardAdapter) Error ¶
func (s *StandardAdapter) Error() Event
Error returns an error level event
func (*StandardAdapter) Fatal ¶
func (s *StandardAdapter) Fatal() Event
Fatal returns a fatal level event
func (*StandardAdapter) GetLevel ¶
func (s *StandardAdapter) GetLevel() Level
GetLevel returns the current log level
func (*StandardAdapter) Info ¶
func (s *StandardAdapter) Info() Event
Info returns an info level event
func (*StandardAdapter) Panic ¶
func (s *StandardAdapter) Panic() Event
Panic returns a panic level event
func (*StandardAdapter) Printf ¶
func (s *StandardAdapter) Printf(format string, v ...interface{})
Printf prints a formatted message
func (*StandardAdapter) SetLevel ¶
func (s *StandardAdapter) SetLevel(level Level) Adapter
SetLevel sets the log level
func (*StandardAdapter) Trace ¶
func (s *StandardAdapter) Trace() Event
Trace returns a trace level event
func (*StandardAdapter) Warn ¶
func (s *StandardAdapter) Warn() Event
Warn returns a warning level event
func (*StandardAdapter) WithPackage ¶
func (s *StandardAdapter) WithPackage(pkg string) Adapter
WithPackage returns a new adapter with package name field
type StandardEvent ¶
type StandardEvent struct {
// contains filtered or unexported fields
}
StandardEvent wraps standard log functionality to implement our Event interface
func (*StandardEvent) Err ¶
func (e *StandardEvent) Err(err error) Event
Err adds an error to the event
func (*StandardEvent) Field ¶
func (e *StandardEvent) Field(key string, value interface{}) Event
Field adds a single field to the event
func (*StandardEvent) Fields ¶
func (e *StandardEvent) Fields(fields ...Field) Event
Fields adds structured fields to the event
func (*StandardEvent) Msg ¶
func (e *StandardEvent) Msg(msg string)
Msg logs the message with all accumulated fields
func (*StandardEvent) Msgf ¶
func (e *StandardEvent) Msgf(format string, v ...interface{})
Msgf logs the formatted message with all accumulated fields
type ZerologAdapter ¶
type ZerologAdapter struct {
// contains filtered or unexported fields
}
ZerologAdapter implements LogAdapter using zerolog
func (*ZerologAdapter) Debug ¶
func (z *ZerologAdapter) Debug() Event
Debug returns a debug level event
func (*ZerologAdapter) Enabled ¶ added in v1.6.0
func (z *ZerologAdapter) Enabled() bool
Enabled returns whether logging is enabled
func (*ZerologAdapter) Error ¶
func (z *ZerologAdapter) Error() Event
Error returns an error level event
func (*ZerologAdapter) Fatal ¶
func (z *ZerologAdapter) Fatal() Event
Fatal returns a fatal level event
func (*ZerologAdapter) GetLevel ¶
func (z *ZerologAdapter) GetLevel() Level
GetLevel returns the current log level
func (*ZerologAdapter) Info ¶
func (z *ZerologAdapter) Info() Event
Info returns an info level event
func (*ZerologAdapter) Panic ¶
func (z *ZerologAdapter) Panic() Event
Panic returns a panic level event
func (*ZerologAdapter) Printf ¶
func (z *ZerologAdapter) Printf(format string, v ...interface{})
Printf logs a formatted message using the underlying zerolog logger.
func (*ZerologAdapter) SetLevel ¶
func (z *ZerologAdapter) SetLevel(level Level) Adapter
SetLevel sets the log level
func (*ZerologAdapter) Trace ¶
func (z *ZerologAdapter) Trace() Event
Trace returns a trace level event
func (*ZerologAdapter) Warn ¶
func (z *ZerologAdapter) Warn() Event
Warn returns a warning level event
func (*ZerologAdapter) WithPackage ¶
func (z *ZerologAdapter) WithPackage(pkg string) Adapter
WithPackage returns a new adapter with package name field
type ZerologEvent ¶
type ZerologEvent struct {
// contains filtered or unexported fields
}
ZerologEvent wraps zerolog.Event to implement our Event interface
func (*ZerologEvent) Err ¶
func (e *ZerologEvent) Err(err error) Event
Err adds an error to the event
func (*ZerologEvent) Field ¶
func (e *ZerologEvent) Field(key string, value interface{}) Event
Field adds a single structured field to the event
func (*ZerologEvent) Fields ¶
func (e *ZerologEvent) Fields(fields ...Field) Event
Fields adds structured fields to the event
func (*ZerologEvent) Msgf ¶
func (e *ZerologEvent) Msgf(format string, v ...interface{})
Msgf logs the formatted message