logging

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, fields ...Fields)

Debug logs a debug message using the default logger

func DebugOp

func DebugOp(op, msg string, fields ...Fields)

DebugOp logs a debug message with operation context using the default logger

func Error

func Error(msg string, err error, fields ...Fields)

Error logs an error message using the default logger

func ErrorOp

func ErrorOp(op, msg string, err error, fields ...Fields)

ErrorOp logs an error message with operation context using the default logger

func Info

func Info(msg string, fields ...Fields)

Info logs an info message using the default logger

func InfoOp

func InfoOp(op, msg string, fields ...Fields)

InfoOp logs an info message with operation context using the default logger

func SetDefaultLogger

func SetDefaultLogger(logger *Logger)

SetDefaultLogger sets the global default logger

func Warn

func Warn(msg string, fields ...Fields)

Warn logs a warning message using the default logger

func WarnOp

func WarnOp(op, msg string, fields ...Fields)

WarnOp logs a warning message with operation context using the default logger

Types

type Config

type Config struct {
	Level        Level
	Output       io.Writer
	Component    string
	IncludeTrace bool
	IncludeFile  bool
}

Config holds logger configuration

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a default logger configuration

type Entry

type Entry struct {
	Timestamp  time.Time              `json:"timestamp"`
	Level      string                 `json:"level"`
	Message    string                 `json:"message"`
	Fields     map[string]interface{} `json:"fields,omitempty"`
	Component  string                 `json:"component,omitempty"`
	Operation  string                 `json:"operation,omitempty"`
	Error      string                 `json:"error,omitempty"`
	StackTrace string                 `json:"stack_trace,omitempty"`
	File       string                 `json:"file,omitempty"`
	Line       int                    `json:"line,omitempty"`
}

Entry represents a single log entry

type Fields

type Fields map[string]interface{}

Fields represents structured log fields

type Level

type Level int

Level represents the severity level of a log message

const (
	// LevelDebug is for detailed debugging information
	LevelDebug Level = iota
	// LevelInfo is for informational messages
	LevelInfo
	// LevelWarn is for warning messages
	LevelWarn
	// LevelError is for error messages
	LevelError
	// LevelFatal is for fatal errors
	LevelFatal
)

func ParseLevel

func ParseLevel(s string) (Level, error)

ParseLevel parses a string into a log level

func (Level) String

func (l Level) String() string

String returns the string representation of the log level

type Logger

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

Logger is the main logging interface

Example
logger := New(&Config{
	Level:     LevelInfo,
	Component: "example",
})

logger.Info("Application started")
logger.InfoOp("database.connect", "Connected to database", Fields{
	"host": "localhost",
	"port": 5432,
})
logger.Error("Failed to process request", errors.New("connection timeout"))

func Default

func Default() *Logger

Default returns the default logger

func New

func New(config *Config) *Logger

New creates a new logger with the given configuration

func (*Logger) Debug

func (l *Logger) Debug(msg string, fields ...Fields)

Debug logs a debug message

func (*Logger) DebugOp

func (l *Logger) DebugOp(op, msg string, fields ...Fields)

DebugOp logs a debug message with operation context

func (*Logger) Error

func (l *Logger) Error(msg string, err error, fields ...Fields)

Error logs an error message

func (*Logger) ErrorOp

func (l *Logger) ErrorOp(op, msg string, err error, fields ...Fields)

ErrorOp logs an error message with operation context

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, err error, fields ...Fields)

Fatal logs a fatal message and exits

func (*Logger) GetLevel

func (l *Logger) GetLevel() Level

GetLevel returns the current log level

func (*Logger) Info

func (l *Logger) Info(msg string, fields ...Fields)

Info logs an info message

func (*Logger) InfoOp

func (l *Logger) InfoOp(op, msg string, fields ...Fields)

InfoOp logs an info message with operation context

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel sets the minimum log level

func (*Logger) Warn

func (l *Logger) Warn(msg string, fields ...Fields)

Warn logs a warning message

func (*Logger) WarnOp

func (l *Logger) WarnOp(op, msg string, fields ...Fields)

WarnOp logs a warning message with operation context

func (*Logger) WithComponent

func (l *Logger) WithComponent(component string) *Logger

WithComponent returns a new logger with the specified component

Example
logger := New(DefaultConfig())

dbLogger := logger.WithComponent("database")
dbLogger.Info("Database initialized")

apiLogger := logger.WithComponent("api")
apiLogger.Info("API server started")

func (*Logger) WithFields

func (l *Logger) WithFields(fields Fields) *Logger

WithFields returns a new logger with the specified default fields

Example
logger := New(DefaultConfig())

requestLogger := logger.WithFields(Fields{
	"request_id": "abc123",
	"user_id":    "user456",
})

requestLogger.Info("Processing request")
requestLogger.Info("Request completed")

Jump to

Keyboard shortcuts

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