logger

package
v0.19.2 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

README

Logger Package

A structured logging package built on top of uber-go/zap, providing both global and context-aware logging capabilities with sensible defaults.

Features

  • Built on top of the high-performance zap logger
  • Supports both structured and printf-style logging
  • Context-aware logging
  • Global and instance-based logging
  • Configurable log levels and encoding formats
  • Request ID tracking support
  • Easy integration with existing applications

Installation

go get -u github.com/zondax/golem/pkg/logger

Quick Start

// Initialize with default configuration
logger.InitLogger(logger.Config{
    Level:    "info",
    Encoding: "json",
})

// Basic logging
logger.Info("Server started")
logger.Error("Connection failed")

// Structured logging with fields
log := logger.NewLogger(
    logger.Field{Key: "service", Value: "api"},
    logger.Field{Key: "version", Value: "1.0.0"},
)
log.Info("Service initialized")

Configuration

Logger Config
type Config struct {
    Level    string `json:"level"`    // Logging level
    Encoding string `json:"encoding"` // Output format
}
Log Levels

Available log levels (in order of increasing severity):

  • debug: Detailed information for debugging
  • info: General operational information
  • warn: Warning messages for potentially harmful situations
  • error: Error conditions that should be addressed
  • dpanic: Critical errors in development that cause panic
  • panic: Critical errors that cause panic in production
  • fatal: Fatal errors that terminate the program
Encoding Formats
  1. JSON Format (Default)

    • Recommended for production
    • Machine-readable structured output
    {"level":"INFO","ts":"2024-03-20T10:00:00.000Z","msg":"Server started","service":"api"}
    
  2. Console Format

    • Recommended for development
    • Human-readable output
    2024-03-20T10:00:00.000Z INFO Server started service=api
    

Advanced Usage

Context-Aware Logging
// Create a context with logger
ctx := context.Background()
log := logger.NewLogger(logger.Field{
    Key: logger.RequestIDKey,
    Value: "req-123",
})
ctx = logger.ContextWithLogger(ctx, log)

// Get logger from context
contextLogger := logger.GetLoggerFromContext(ctx)
contextLogger.Info("Processing request")
Structured Logging with Fields
log := logger.NewLogger()
log.WithFields(
    zap.String("user_id", "12345"),
    zap.String("action", "login"),
    zap.Int("attempt", 1),
).Info("User login attempt")
Printf-Style Logging
logger.Infof("Processing item %d of %d", current, total)
logger.Errorf("Failed to connect to %s: %v", host, err)

Best Practices

  1. Use Structured Logging

    // Good
    log.WithFields(
        zap.String("user_id", "12345"),
        zap.String("action", "purchase"),
        zap.Float64("amount", 99.99),
    ).Info("Purchase completed")
    
    // Avoid
    log.Infof("User %s completed purchase of $%.2f", userID, amount)
    
  2. Include Request IDs

    log.WithFields(
        zap.String(logger.RequestIDKey, requestID),
    ).Info("Handling request")
    
  3. Proper Error Logging

    if err != nil {
        log.WithFields(
            zap.Error(err),
            zap.String("operation", "database_query"),
        ).Error("Query failed")
    }
    
  4. Resource Cleanup

    defer logger.Sync()
    

Performance Considerations

  • The logger is designed to be zero-allocation in most cases
  • JSON encoding is more CPU-intensive but provides structured data
  • Log level checks are performed atomically
  • Field allocation is optimized for minimal overhead

Thread Safety

The logger is completely thread-safe and can be used concurrently from multiple goroutines.

Documentation

Index

Constants

View Source
const (
	ConsoleEncode = "console"
)
View Source
const (
	RequestIDKey = "request_id"
)

Variables

This section is empty.

Functions

func ContextWithLogger

func ContextWithLogger(ctx context.Context, logger *Logger) context.Context

func DPanic added in v0.19.2

func DPanic(msg string)

func DPanicf added in v0.19.2

func DPanicf(template string, args ...interface{})

func Debug added in v0.19.2

func Debug(msg string)

func Debugf added in v0.19.2

func Debugf(template string, args ...interface{})

func Error added in v0.19.2

func Error(msg string)

func Errorf added in v0.19.2

func Errorf(template string, args ...interface{})

func Fatal added in v0.19.2

func Fatal(msg string)

func Fatalf added in v0.19.2

func Fatalf(template string, args ...interface{})

func Info added in v0.19.2

func Info(msg string)

func Infof added in v0.19.2

func Infof(template string, args ...interface{})

func InitLogger

func InitLogger(config Config)

func L added in v0.19.2

func L() *zap.Logger

func Panic added in v0.19.2

func Panic(msg string)

func Panicf added in v0.19.2

func Panicf(template string, args ...interface{})

func ReplaceGlobals added in v0.19.2

func ReplaceGlobals(logger *zap.Logger) func()

func S added in v0.19.2

func S() *zap.SugaredLogger

func SetGlobalConfig added in v0.19.2

func SetGlobalConfig(config Config) func()

func Sync

func Sync() error

func Warn added in v0.19.2

func Warn(msg string)

func Warnf added in v0.19.2

func Warnf(template string, args ...interface{})

Types

type Config

type Config struct {
	Level    string `json:"level"`
	Encoding string `json:"encoding"`
}

type Field

type Field struct {
	Key   string
	Value interface{}
}

type Logger

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

func GetLoggerFromContext

func GetLoggerFromContext(ctx context.Context) *Logger

func NewLogger

func NewLogger(opts ...interface{}) *Logger

func (*Logger) DPanic

func (l *Logger) DPanic(msg string)

func (*Logger) DPanicf

func (l *Logger) DPanicf(template string, args ...interface{})

func (*Logger) Debug

func (l *Logger) Debug(msg string)

func (*Logger) Debugf

func (l *Logger) Debugf(template string, args ...interface{})

func (*Logger) Error

func (l *Logger) Error(msg string)

func (*Logger) Errorf

func (l *Logger) Errorf(template string, args ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(msg string)

func (*Logger) Fatalf

func (l *Logger) Fatalf(template string, args ...interface{})

func (*Logger) Info

func (l *Logger) Info(msg string)

func (*Logger) Infof

func (l *Logger) Infof(template string, args ...interface{})

func (*Logger) IsDebugEnabled added in v0.15.1

func (l *Logger) IsDebugEnabled() bool

func (*Logger) Panic

func (l *Logger) Panic(msg string)

func (*Logger) Panicf

func (l *Logger) Panicf(template string, args ...interface{})

func (*Logger) Warn

func (l *Logger) Warn(msg string)

func (*Logger) Warnf

func (l *Logger) Warnf(template string, args ...interface{})

func (*Logger) WithFields

func (l *Logger) WithFields(fields ...zap.Field) *Logger

Jump to

Keyboard shortcuts

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