logger

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package logger provides structured logging with request tracing and service context. It uses zap for high-performance logging with support for different output formats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogDebug

func LogDebug(msg string, fields ...Field)

Debug logs a debug message using the global logger.

func LogError

func LogError(msg string, err error, fields ...Field)

LogError logs an error message using the global logger.

func LogFatal

func LogFatal(msg string, fields ...Field)

Fatal logs a fatal message using the global logger.

func LogInfo

func LogInfo(msg string, fields ...Field)

Info logs an info message using the global logger.

func LogWarn

func LogWarn(msg string, fields ...Field)

Warn logs a warning message using the global logger.

func SetGlobalLogger

func SetGlobalLogger(logger Logger)

SetGlobalLogger sets the global logger instance.

Types

type BatchLogger

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

BatchLogger provides batched logging capabilities.

func NewBatchLogger

func NewBatchLogger(logger Logger, batchSize int) *BatchLogger

NewBatchLogger creates a new batch logger.

func (*BatchLogger) Add

func (bl *BatchLogger) Add(entry LogEntry)

Add adds a log entry to the batch.

func (*BatchLogger) Flush

func (bl *BatchLogger) Flush()

Flush flushes all batched log entries.

type Field

type Field struct {
	Key   string
	Value interface{}
}

Field represents a key-value pair for structured logging.

func Any

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

Any creates a field with any value.

func Bool

func Bool(key string, value bool) Field

Bool creates a bool field.

func Component

func Component(value string) Field

Component creates a component field.

func Duration

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

Duration creates a duration field.

func Error

func Error(err error) Field

Error creates an error field.

func Float64

func Float64(key string, value float64) Field

Float64 creates a float64 field.

func Int

func Int(key string, value int) Field

Int creates an int field.

func Int64

func Int64(key string, value int64) Field

Int64 creates an int64 field.

func Namespace

func Namespace(value string) Field

Namespace creates a namespace field for grouping logs.

func Stack

func Stack() Field

Stack creates a stack trace field.

func String

func String(key, value string) Field

String creates a string field.

func Time

func Time(key string, value time.Time) Field

Time creates a time field.

type LogEntry

type LogEntry struct {
	Level     string                 `json:"level"`
	Timestamp time.Time              `json:"timestamp"`
	Message   string                 `json:"message"`
	Fields    map[string]interface{} `json:"fields"`
	Service   string                 `json:"service,omitempty"`
	RequestID string                 `json:"request_id,omitempty"`
	UserID    string                 `json:"user_id,omitempty"`
	TenantID  string                 `json:"tenant_id,omitempty"`
	Error     string                 `json:"error,omitempty"`
}

LogEntry represents a structured log entry for batch logging.

type Logger

type Logger interface {
	// Info logs an info level message with optional fields
	Info(msg string, fields ...Field)

	// Error logs an error level message with optional fields
	Error(msg string, err error, fields ...Field)

	// Warn logs a warning level message with optional fields
	Warn(msg string, fields ...Field)

	// Debug logs a debug level message with optional fields
	Debug(msg string, fields ...Field)

	// Fatal logs a fatal level message and exits the program
	Fatal(msg string, fields ...Field)

	// WithRequestID returns a logger with request ID context
	WithRequestID(requestID string) Logger

	// WithCorrelationID returns a logger with correlation ID context
	WithCorrelationID(correlationID string) Logger

	// WithService returns a logger with service name context
	WithService(service string) Logger

	// WithTenant returns a logger with tenant ID context
	WithTenant(tenantID string) Logger

	// WithUser returns a logger with user ID context
	WithUser(userID string) Logger

	// WithFields returns a logger with additional fields
	WithFields(fields ...Field) Logger

	// WithContext returns a logger with context values
	WithContext(ctx context.Context) Logger

	// SetLevel dynamically changes the log level
	SetLevel(level string) error

	// Sync flushes any buffered log entries
	Sync() error

	// Clone creates a copy of the logger
	Clone() Logger
}

Logger defines the interface for structured logging.

func GetGlobalLogger

func GetGlobalLogger() Logger

GetGlobalLogger returns the global logger instance.

func New

func New(cfg config.LoggingConfig) Logger

New creates a new logger instance with the given configuration.

func NewDevelopment

func NewDevelopment() Logger

NewDevelopment creates a development logger with console output.

func NewProduction

func NewProduction() Logger

NewProduction creates a production logger with JSON output.

func NewWithRotation

func NewWithRotation(cfg config.LoggingConfig) Logger

NewWithRotation creates a new logger with log rotation support.

func WithContext

func WithContext(ctx context.Context) Logger

WithContext returns a logger with context using the global logger.

func WithRequestID

func WithRequestID(requestID string) Logger

WithRequestID returns a logger with request ID using the global logger.

func WithService

func WithService(service string) Logger

WithService returns a logger with service name using the global logger.

type LoggerManager

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

LoggerManager manages multiple loggers and provides advanced logging features.

func NewManager

func NewManager() *LoggerManager

NewManager creates a new logger manager.

func (*LoggerManager) GetLogger

func (lm *LoggerManager) GetLogger(name string) Logger

GetLogger returns a named logger instance.

func (*LoggerManager) SetDefaultLogger

func (lm *LoggerManager) SetDefaultLogger(logger Logger)

SetDefaultLogger sets the default logger.

Jump to

Keyboard shortcuts

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