logging

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2025 License: BSD-3-Clause Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Anonymous

func Anonymous(a bool)

Anonymous sets whether to use anonymous caller tracking

func Debug

func Debug(d bool)

Debug sets whether to use caller tracking

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

func SetPackageAdapter(pkg string, adapter Adapter)

SetPackageAdapter sets a specific adapter for a package This overrides the global adapter for the specified package

func SetPackageLevel

func SetPackageLevel(pkg string, level Level)

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
}

Adapter defines the interface for internal logging

func GetGlobalAdapter

func GetGlobalAdapter() Adapter

GetGlobalAdapter returns the current global adapter

func GetPackageLogger

func GetPackageLogger(pkg string) Adapter

GetPackageLogger returns a logger for a specific package Returns a dynamic adapter that will always use the current global/package-specific adapter

func NewDynamicAdapter

func NewDynamicAdapter(pkg string) Adapter

NewDynamicAdapter creates a dynamic adapter for a package

func NewNoOpAdapter

func NewNoOpAdapter() Adapter

NewNoOpAdapter creates a new no-op adapter

func NewStandardAdapter

func NewStandardAdapter() Adapter

NewStandardAdapter creates a new standard log adapter with the default logger

func NewStandardAdapterWithLogger

func NewStandardAdapterWithLogger(logger *log.Logger) Adapter

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

func NewZerologAdapterWithLogger(logger zerolog.Logger) Adapter

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) 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

func F

func F(key string, value interface{}) Field

F is a helper function to create fields

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

func GetPackageLevel(pkg string) Level

GetPackageLevel returns the log level for a specific package

func (Level) String

func (l Level) String() string

String returns the string representation of the log level

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) Debug

func (n *NoOpAdapter) Debug() Event

Debug returns a no-op event

func (*NoOpAdapter) Error

func (n *NoOpAdapter) Error() Event

Error returns a no-op event

func (*NoOpAdapter) Fatal

func (n *NoOpAdapter) Fatal() Event

Fatal returns a no-op event

func (*NoOpAdapter) GetLevel

func (n *NoOpAdapter) GetLevel() Level

GetLevel returns the current log level

func (*NoOpAdapter) Info

func (n *NoOpAdapter) Info() Event

Info returns a no-op event

func (*NoOpAdapter) Panic

func (n *NoOpAdapter) Panic() Event

Panic returns a no-op event

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) Trace

func (n *NoOpAdapter) Trace() Event

Trace returns a no-op event

func (*NoOpAdapter) Warn

func (n *NoOpAdapter) Warn() Event

Warn returns a no-op event

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

func (*NoOpEvent) Err

func (e *NoOpEvent) Err(_ error) Event

Err does nothing and returns itself for chaining

func (*NoOpEvent) Field

func (e *NoOpEvent) Field(_ string, _ interface{}) Event

Field does nothing and returns itself for chaining

func (*NoOpEvent) Fields

func (e *NoOpEvent) Fields(_ ...Field) Event

Fields does nothing and returns itself for chaining

func (*NoOpEvent) Msg

func (e *NoOpEvent) Msg(_ string)

Msg does nothing

func (*NoOpEvent) Msgf

func (e *NoOpEvent) Msgf(_ string, _ ...interface{})

Msgf 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) 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) 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) Msg

func (e *ZerologEvent) Msg(msg string)

Msg logs the message

func (*ZerologEvent) Msgf

func (e *ZerologEvent) Msgf(format string, v ...interface{})

Msgf logs the formatted message

Directories

Path Synopsis
Package log provides a convenient global logger interface that wraps the core logging functionality.
Package log provides a convenient global logger interface that wraps the core logging functionality.

Jump to

Keyboard shortcuts

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