logs(structured logging)

A comprehensive logging system with multiple interfaces, level-based filtering, and flexible output configuration, designed for high-performance applications.

The logs module provides a comprehensive logging system with multiple interfaces, level-based filtering, and flexible output configuration, designed for high-performance applications.

Features

  • Multiple logging interfaces: Logger, FormatLogger, CtxLogger
  • Seven log levels: Trace, Debug, Info, Notice, Warn, Error, Fatal
  • Context-aware logging with CtxLogger interface
  • Configurable output destinations (stderr, files, custom writers)
  • Thread-safe operations for concurrent applications
  • Microsecond precision timestamps
  • Short file names in log output for better readability
  • Global and instance-based usage

Log Levels and Usage

 1package main
 2
 3import (
 4    "context"
 5    "os"
 6    
 7    "github.com/crazyfrankie/frx/logs"
 8)
 9
10func main() {
11    // Set log level (only logs at this level or higher will be output)
12    logs.SetLevel(logs.LevelInfo)
13    
14    // Different log levels (in order of severity)
15    logs.Trace("Detailed trace information")     // Lowest level
16    logs.Debug("Debug information for developers")
17    logs.Info("General information")             // Default level
18    logs.Notice("Notable events")
19    logs.Warn("Warning messages")
20    logs.Error("Error conditions")
21    logs.Fatal("Fatal errors - will call os.Exit(1)")  // Highest level
22}

Formatted Logging

 1func main() {
 2    // Formatted logging with printf-style formatting
 3    userID := 12345
 4    userName := "john_doe"
 5    
 6    logs.Infof("User %s (ID: %d) logged in successfully", userName, userID)
 7    logs.Errorf("Failed to process user %d: %v", userID, err)
 8    logs.Debugf("Processing request with %d items", len(items))
 9    
10    // All format levels available
11    logs.Tracef("Trace: %s", "detailed info")
12    logs.Debugf("Debug: %s", "debug info")
13    logs.Noticef("Notice: %s", "notable event")
14    logs.Warnf("Warning: %s", "warning message")
15    logs.Fatalf("Fatal: %s", "fatal error")  // Exits program
16}

Context-Aware Logging

 1func ProcessRequest(ctx context.Context, requestID string) {
 2    // Context-aware logging for request tracing
 3    logs.CtxInfof(ctx, "Processing request %s", requestID)
 4    
 5    // Simulate processing
 6    if err := doSomeWork(ctx); err != nil {
 7        logs.CtxErrorf(ctx, "Request %s failed: %v", requestID, err)
 8        return
 9    }
10    
11    logs.CtxInfof(ctx, "Request %s completed successfully", requestID)
12}
13
14func doSomeWork(ctx context.Context) error {
15    logs.CtxDebugf(ctx, "Starting work processing")
16    
17    // Simulate work
18    select {
19    case <-time.After(100 * time.Millisecond):
20        logs.CtxDebugf(ctx, "Work completed")
21        return nil
22    case <-ctx.Done():
23        logs.CtxWarnf(ctx, "Work cancelled: %v", ctx.Err())
24        return ctx.Err()
25    }
26}

Custom Logger Configuration

 1func main() {
 2    // Redirect logs to a file
 3    logFile, err := os.OpenFile("app.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
 4    if err != nil {
 5        panic(err)
 6    }
 7    defer logFile.Close()
 8    
 9    logs.SetOutput(logFile)
10    logs.SetLevel(logs.LevelDebug)
11    
12    // Use custom logger instance
13    customLogger := logs.DefaultLogger()
14    customLogger.SetLevel(logs.LevelWarn)
15    customLogger.Info("This won't be logged due to level filtering")
16    customLogger.Error("This will be logged")
17    
18    // Replace default logger
19    logs.SetLogger(customLogger)
20}

Production Logging Patterns

 1func HandleAPIRequest(ctx context.Context, req *APIRequest) (*APIResponse, error) {
 2    requestID := req.ID
 3    
 4    logs.CtxInfof(ctx, "API request started: %s", requestID)
 5    start := time.Now()
 6    
 7    defer func() {
 8        duration := time.Since(start)
 9        logs.CtxInfof(ctx, "API request completed: %s (took %v)", requestID, duration)
10    }()
11    
12    // Validate request
13    if err := validateRequest(req); err != nil {
14        logs.CtxWarnf(ctx, "Invalid request %s: %v", requestID, err)
15        return nil, err
16    }
17    
18    // Process request
19    resp, err := processRequest(ctx, req)
20    if err != nil {
21        logs.CtxErrorf(ctx, "Failed to process request %s: %v", requestID, err)
22        return nil, err
23    }
24    
25    logs.CtxDebugf(ctx, "Request %s processed successfully", requestID)
26    return resp, nil
27}

Basic Usage

 1package main
 2
 3import (
 4    "github.com/crazyfrankie/frx/logs"
 5)
 6
 7func main() {
 8    logs.SetlogLevel(logs.LevelInfo)
 9    
10    // call log
11    logs.Info("Application started")
12    logs.Error("Something went wrong", "error", err)
13    logs.Debug("Debug information", "data", debugData)
14}