/

Log Levels Configuration

Log levels control which messages are processed and logged. Set the minimum level to filter out less important messages and optimize performance.

Available Log Levels

ByteHide Logger supports six log levels in order of severity:

LevelSeverityUse Case
logs.LogLevelTraceLowMost detailed diagnostic information
logs.LogLevelDebugLowDetailed diagnostic information
logs.LogLevelInfoMediumGeneral information messages
logs.LogLevelWarnHighWarning messages for potential issues
logs.LogLevelErrorCriticalError messages for failures
logs.LogLevelCriticalCriticalCritical errors that may cause termination

Setting Minimum Log Level

Configure the minimum level:

Go
import logs "github.com/bytehide/bytehide-logs-go"

logs.SetMinimumLevel(logs.LogLevelInfo)  // Default: LogLevelWarn

Level Filtering Behavior

When you set a minimum level, only messages at that level or higher are processed:

Go
// Set minimum level to Info
logs.SetMinimumLevel(logs.LogLevelInfo)

logs.Trace()           // ❌ Below minimum - ignored
logs.Debug()           // ❌ Below minimum - ignored
logs.Info("message")   // ✅ At minimum level
logs.Warn("message")   // ✅ Above minimum
logs.Error("msg", err) // ✅ Above minimum
logs.Critical()        // ✅ Above minimum

Environment-Specific Configuration

Development Environment

Go
logs.SetMinimumLevel(logs.LogLevelDebug)  // Verbose logging for debugging

Production Environment

Go
logs.SetMinimumLevel(logs.LogLevelWarn)   // Only warnings and errors

Dynamic Configuration

Go
import (
    "os"
    "strings"
    logs "github.com/bytehide/bytehide-logs-go"
)

environment := os.Getenv("APP_ENVIRONMENT")
isDevelopment := strings.EqualFold(environment, "development")

if isDevelopment {
    logs.SetMinimumLevel(logs.LogLevelDebug)
} else {
    logs.SetMinimumLevel(logs.LogLevelError)
}

Usage Examples

Trace Level

Go
logs.Trace("Entering method processOrder with orderId: ", orderId)

Debug Level

Go
logs.Debug("Database query executed in ", elapsed, "ms")

Info Level

Go
logs.Info("User ", userId, " logged in successfully")

Warn Level

Go
logs.Warn("API rate limit approaching: ", current, "/", max)

Error Level

Go
logs.Error("Failed to process payment for order "+orderId, err)

Critical Level

Go
logs.Critical("Database connection lost. Application shutting down.")

Performance Impact

LevelPerformanceUse Case
Trace/DebugHighest overheadDevelopment only
InfoModerate overheadDevelopment/staging
WarnLow overheadProduction (recommended)
Error/CriticalMinimal overheadHigh-performance production

Context-Aware Logging

Combine log levels with context for better diagnostics:

Go
logs.SetMinimumLevel(logs.LogLevelDebug)

logs.Debug("Starting database query").
    WithContext("table", "users").
    WithContext("limit", 100).Log()

logs.Info("Query completed").
    WithContext("rows", 42).
    WithContext("duration", "125ms").Log()

logs.Warn("Query took longer than expected").
    WithContext("duration", "5000ms").
    WithContext("threshold", "1000ms").Log()

Best Practices

Level Selection Guidelines

  • Development: Use DEBUG or INFO for detailed diagnostics
  • Staging: Use INFO or WARN for testing scenarios
  • Production: Use WARN or ERROR for optimal performance
  • High-traffic: Use ERROR or CRITICAL for minimal overhead

Next Steps

Previous
Project Token