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:
| Level | Severity | Use Case |
|---|---|---|
logs.LogLevelTrace | Low | Most detailed diagnostic information |
logs.LogLevelDebug | Low | Detailed diagnostic information |
logs.LogLevelInfo | Medium | General information messages |
logs.LogLevelWarn | High | Warning messages for potential issues |
logs.LogLevelError | Critical | Error messages for failures |
logs.LogLevelCritical | Critical | Critical 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: LogLevelWarnimport logs "github.com/bytehide/bytehide-logs-go"
logs.SetMinimumLevel(logs.LogLevelInfo) // Default: LogLevelWarnLevel 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// 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 minimumEnvironment-Specific Configuration
Development Environment
Go
logs.SetMinimumLevel(logs.LogLevelDebug) // Verbose logging for debugginglogs.SetMinimumLevel(logs.LogLevelDebug) // Verbose logging for debuggingProduction Environment
Go
logs.SetMinimumLevel(logs.LogLevelWarn) // Only warnings and errorslogs.SetMinimumLevel(logs.LogLevelWarn) // Only warnings and errorsDynamic 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)
}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)logs.Trace("Entering method processOrder with orderId: ", orderId)Debug Level
Go
logs.Debug("Database query executed in ", elapsed, "ms")logs.Debug("Database query executed in ", elapsed, "ms")Info Level
Go
logs.Info("User ", userId, " logged in successfully")logs.Info("User ", userId, " logged in successfully")Warn Level
Go
logs.Warn("API rate limit approaching: ", current, "/", max)logs.Warn("API rate limit approaching: ", current, "/", max)Error Level
Go
logs.Error("Failed to process payment for order "+orderId, err)logs.Error("Failed to process payment for order "+orderId, err)Critical Level
Go
logs.Critical("Database connection lost. Application shutting down.")logs.Critical("Database connection lost. Application shutting down.")Performance Impact
| Level | Performance | Use Case |
|---|---|---|
Trace/Debug | Highest overhead | Development only |
Info | Moderate overhead | Development/staging |
Warn | Low overhead | Production (recommended) |
Error/Critical | Minimal overhead | High-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()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
DEBUGorINFOfor detailed diagnostics - Staging: Use
INFOorWARNfor testing scenarios - Production: Use
WARNorERRORfor optimal performance - High-traffic: Use
ERRORorCRITICALfor minimal overhead