logging

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: BSD-3-Clause Imports: 12 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 GetGlobalAdapter

func GetGlobalAdapter[T Adapter]() (T, bool)

GetGlobalAdapter returns the current global adapter

func Interactive added in v1.6.1

func Interactive() bool

Interactive checks if the application is running in interactive mode by checking if stdout is available.

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 SetModeDetector added in v1.6.1

func SetModeDetector(d ModeDetector)

SetModeDetector allows the user to provide a custom function to detect interactive mode

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

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

	Logger() *log.Logger
}

Adapter defines the interface for internal logging

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 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) 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) Logger added in v1.6.1

func (d *DynamicAdapter) Logger() *log.Logger

Logger returns the underlying logger 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 (
	VerboseLevel  Level = -2 // VerboseLevel logs extremely detailed information, more than TraceLevel
	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 (VerboseLevel) 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 ModeDetector added in v1.6.1

type ModeDetector func() bool

ModeDetector is a function type for detecting interactive mode

type MultiWriter added in v1.6.1

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

MultiWriter is like io.MultiWriter but continues writing to other writers even if one fails

func (*MultiWriter) Write added in v1.6.1

func (mw *MultiWriter) Write(p []byte) (n int, err error)

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) Enabled added in v1.6.0

func (n *NoOpAdapter) Enabled() bool

Enabled returns false indicating logging is disabled

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) Logger added in v1.6.1

func (n *NoOpAdapter) Logger() *log.Logger

Logger returns a nil logger

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) 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) Logger added in v1.6.1

func (s *StandardAdapter) Logger() *log.Logger

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 StreamWriter added in v1.6.1

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

StreamWriter is an io.Writer that streams log entries to listeners

func NewStreamWriter added in v1.6.1

func NewStreamWriter(bufferMax int) *StreamWriter

NewStreamWriter creates a new StreamWriter with optional buffer size

func (*StreamWriter) AddListener added in v1.6.1

func (sw *StreamWriter) AddListener(ch chan string)

AddListener adds a new listener channel and sends buffered entries

func (*StreamWriter) GetListenerCount added in v1.6.1

func (sw *StreamWriter) GetListenerCount() int

GetListenerCount returns the number of active listeners

func (*StreamWriter) HasListeners added in v1.6.1

func (sw *StreamWriter) HasListeners() bool

HasListeners returns true if there are active listeners

func (*StreamWriter) RemoveListener added in v1.6.1

func (sw *StreamWriter) RemoveListener(ch chan string)

RemoveListener removes a specific listener channel

func (*StreamWriter) Write added in v1.6.1

func (sw *StreamWriter) Write(p []byte) (n int, err error)

Write implements io.Writer interface

type ZerologAdapter

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

ZerologAdapter implements LogAdapter using zerolog

func NewZerologAdapter

func NewZerologAdapter() *ZerologAdapter

NewZerologAdapter creates a new zerolog adapter with the global zerolog logger

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) Flush added in v1.6.1

func (z *ZerologAdapter) Flush()

Flush flushes all outputs

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) Logger added in v1.6.1

func (z *ZerologAdapter) Logger() *l.Logger

func (*ZerologAdapter) Panic

func (z *ZerologAdapter) Panic() Event

Panic returns a panic level event

func (*ZerologAdapter) Path added in v1.6.1

func (z *ZerologAdapter) Path() string

GetPath returns the log file path if logging to a file

func (*ZerologAdapter) Printf

func (z *ZerologAdapter) Printf(format string, v ...interface{})

Printf logs a formatted message using the underlying zerolog logger.

func (*ZerologAdapter) Rotate added in v1.6.1

func (z *ZerologAdapter) Rotate() error

Rotate rotates the log file manually. It creates a new log file and closes the old one.

func (*ZerologAdapter) SetLevel

func (z *ZerologAdapter) SetLevel(level Level) Adapter

SetLevel sets the log level

func (*ZerologAdapter) Stop added in v1.6.1

func (z *ZerologAdapter) Stop()

Stop flushes and closes the log file if configured

func (*ZerologAdapter) Stream added in v1.6.1

func (z *ZerologAdapter) Stream() *StreamWriter

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) With added in v1.6.1

func (z *ZerologAdapter) With(writers ...io.Writer) *ZerologAdapter

With adds additional output writers to the logger

func (*ZerologAdapter) WithConsole added in v1.6.1

func (z *ZerologAdapter) WithConsole() *ZerologAdapter

WithConsole adds a console writer to the logger if in interactive mode

func (*ZerologAdapter) WithFileRotation added in v1.6.1

func (z *ZerologAdapter) WithFileRotation(name string, size, age, backups int, compress bool) *ZerologAdapter

WithFileRotation adds a file writer with rotation to the logger

func (*ZerologAdapter) WithPackage

func (z *ZerologAdapter) WithPackage(pkg string) Adapter

WithPackage returns a new adapter with package name field

func (*ZerologAdapter) WithStream added in v1.6.1

func (z *ZerologAdapter) WithStream(max int) *ZerologAdapter

WithStream adds a stream writer to the logger with the specified max buffer size

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