Logger SDK Configuration
Configuration overview
ByteHide Logger for Go is configured through package-level functions, providing a simple and flexible way to set up logging behavior, file management, security, and performance optimization.
Configuration Overview
The Go SDK uses package-level functions for configuration. Here's how to set up basic configuration:
import logs "github.com/bytehide/bytehide-logs-go"
// Set your project token
logs.SetProjectToken("your-project-token")
// Configure logging behavior
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelInfo)import logs "github.com/bytehide/bytehide-logs-go"
// Set your project token
logs.SetProjectToken("your-project-token")
// Configure logging behavior
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelInfo)Project Token Configuration
By simply adding your project token, all logs will be automatically sent to the ByteHide cloud platform without any additional configuration. If you don't set a project token, logs will only be stored in local files. For more information about obtaining and configuring your project token, see Project Token setup.
Configuration Functions
| Function | Description | Default |
|---|---|---|
SetProjectToken(string) | Set project token manually | "" |
SetProject() | Load token from environment variable | - |
SetConsoleEnabled(bool) | Enable/disable console output | false |
SetMinimumLevel(LogLevel) | Minimum log level to process | LogLevelWarn |
SetPersist(bool) | Enable/disable file persistence | false |
SetFilePath(string) | Log file path | "logs.txt" |
SetRollingInterval(RollingInterval) | File rotation interval | RollingIntervalDay |
AddSensitiveKey(string) | Add sensitive data key to mask | - |
SetDuplicateSuppressionWindow(int64) | Duplicate suppression window (ms) | 1000 |
SetUser(*AuthUser) | Set authenticated user | nil |
SetAnonymousID(string) | Set anonymous user ID | "" |
AddMetaContext(string, interface{}) | Add global context metadata | - |
Flush() | Flush pending logs | - |
Shutdown() | Shutdown logger | - |
Disable() | Disable all logging | - |
Enable() | Enable logging after disable | - |
File Persistence Settings
Control how logs are saved to files:
// Enable file logging
logs.SetPersist(true)
logs.SetFilePath("logs/application.log")
// File rotation settings
logs.SetRollingInterval(logs.RollingIntervalDay)// Enable file logging
logs.SetPersist(true)
logs.SetFilePath("logs/application.log")
// File rotation settings
logs.SetRollingInterval(logs.RollingIntervalDay)Available Rolling Intervals:
logs.RollingIntervalMinute: Every minutelogs.RollingIntervalHour: Every hourlogs.RollingIntervalDay: Daily (recommended)logs.RollingIntervalWeek: Weeklylogs.RollingIntervalMonth: Monthlylogs.RollingIntervalYear: Yearly
Console Output Settings
Configure console logging:
logs.SetConsoleEnabled(true) // Default: falselogs.SetConsoleEnabled(true) // Default: falseLog Level Configuration
Set the minimum log level to process:
logs.SetMinimumLevel(logs.LogLevelInfo) // Default: LogLevelWarnlogs.SetMinimumLevel(logs.LogLevelInfo) // Default: LogLevelWarnAvailable Log Levels (in order of severity):
logs.LogLevelTrace: Most detailed diagnostic informationlogs.LogLevelDebug: Detailed diagnostic informationlogs.LogLevelInfo: General information messageslogs.LogLevelWarn: Warning messages for potential issueslogs.LogLevelError: Error messages for failureslogs.LogLevelCritical: Critical errors that may cause application termination
Security Settings
Configure data masking for sensitive information:
// Add sensitive keys to mask in logs
logs.AddSensitiveKey("password")
logs.AddSensitiveKey("token")
logs.AddSensitiveKey("apiKey")
logs.AddSensitiveKey("creditCard")// Add sensitive keys to mask in logs
logs.AddSensitiveKey("password")
logs.AddSensitiveKey("token")
logs.AddSensitiveKey("apiKey")
logs.AddSensitiveKey("creditCard")Performance Settings
Configure duplicate suppression:
// Suppress duplicate messages within time window (milliseconds)
logs.SetDuplicateSuppressionWindow(5000) // 5 seconds// Suppress duplicate messages within time window (milliseconds)
logs.SetDuplicateSuppressionWindow(5000) // 5 secondsConfiguration Examples
Development Environment
logs.SetProjectToken("your-dev-token")
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelDebug)
// File logging
logs.SetPersist(true)
logs.SetFilePath("logs/debug.log")
logs.SetRollingInterval(logs.RollingIntervalHour)
// Basic security
logs.AddSensitiveKey("password")
logs.AddSensitiveKey("token")logs.SetProjectToken("your-dev-token")
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelDebug)
// File logging
logs.SetPersist(true)
logs.SetFilePath("logs/debug.log")
logs.SetRollingInterval(logs.RollingIntervalHour)
// Basic security
logs.AddSensitiveKey("password")
logs.AddSensitiveKey("token")Production Environment
logs.SetProjectToken(os.Getenv("BYTEHIDE_LOGS_TOKEN"))
logs.SetConsoleEnabled(false)
logs.SetMinimumLevel(logs.LogLevelWarn)
// Robust file logging
logs.SetPersist(true)
logs.SetFilePath("logs/production.log")
logs.SetRollingInterval(logs.RollingIntervalDay)
// Security
logs.AddSensitiveKey("password")
logs.AddSensitiveKey("token")
logs.AddSensitiveKey("apiKey")
logs.AddSensitiveKey("creditCard")
logs.AddSensitiveKey("secret")
// Performance
logs.SetDuplicateSuppressionWindow(10000)logs.SetProjectToken(os.Getenv("BYTEHIDE_LOGS_TOKEN"))
logs.SetConsoleEnabled(false)
logs.SetMinimumLevel(logs.LogLevelWarn)
// Robust file logging
logs.SetPersist(true)
logs.SetFilePath("logs/production.log")
logs.SetRollingInterval(logs.RollingIntervalDay)
// Security
logs.AddSensitiveKey("password")
logs.AddSensitiveKey("token")
logs.AddSensitiveKey("apiKey")
logs.AddSensitiveKey("creditCard")
logs.AddSensitiveKey("secret")
// Performance
logs.SetDuplicateSuppressionWindow(10000)Staging Environment
logs.SetProjectToken(os.Getenv("BYTEHIDE_LOGS_TOKEN"))
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelInfo)
// File logging
logs.SetPersist(true)
logs.SetFilePath("logs/staging.log")
logs.SetRollingInterval(logs.RollingIntervalDay)
// Moderate security
logs.AddSensitiveKey("password")
logs.AddSensitiveKey("token")logs.SetProjectToken(os.Getenv("BYTEHIDE_LOGS_TOKEN"))
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelInfo)
// File logging
logs.SetPersist(true)
logs.SetFilePath("logs/staging.log")
logs.SetRollingInterval(logs.RollingIntervalDay)
// Moderate security
logs.AddSensitiveKey("password")
logs.AddSensitiveKey("token")Environment-Based Configuration
Configure logging based on your application environment:
import (
"os"
logs "github.com/bytehide/bytehide-logs-go"
)
func initializeLogger() {
environment := os.Getenv("APP_ENVIRONMENT")
if environment == "" {
environment = "development"
}
// Always set project token from environment
logs.SetProject()
switch environment {
case "production":
logs.SetConsoleEnabled(false)
logs.SetMinimumLevel(logs.LogLevelWarn)
logs.SetDuplicateSuppressionWindow(10000)
case "staging":
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelInfo)
case "development":
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelDebug)
}
logs.SetPersist(true)
logs.SetFilePath("logs/" + environment + "/app.log")
logs.SetRollingInterval(logs.RollingIntervalDay)
}import (
"os"
logs "github.com/bytehide/bytehide-logs-go"
)
func initializeLogger() {
environment := os.Getenv("APP_ENVIRONMENT")
if environment == "" {
environment = "development"
}
// Always set project token from environment
logs.SetProject()
switch environment {
case "production":
logs.SetConsoleEnabled(false)
logs.SetMinimumLevel(logs.LogLevelWarn)
logs.SetDuplicateSuppressionWindow(10000)
case "staging":
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelInfo)
case "development":
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelDebug)
}
logs.SetPersist(true)
logs.SetFilePath("logs/" + environment + "/app.log")
logs.SetRollingInterval(logs.RollingIntervalDay)
}Global Metadata Context
Add metadata to all logs:
logs.AddMetaContext("service", "user-service")
logs.AddMetaContext("version", "1.0.0")
logs.AddMetaContext("environment", "production")
// All logs will include this metadata
logs.Info("User created")logs.AddMetaContext("service", "user-service")
logs.AddMetaContext("version", "1.0.0")
logs.AddMetaContext("environment", "production")
// All logs will include this metadata
logs.Info("User created")User Identification
Set authenticated user information:
user := &logs.AuthUser{
ID: "user-123",
Email: "john.doe@company.com",
Token: "token-xyz",
}
logs.SetUser(user)
// Or set anonymous ID
logs.SetAnonymousID("anon-456")user := &logs.AuthUser{
ID: "user-123",
Email: "john.doe@company.com",
Token: "token-xyz",
}
logs.SetUser(user)
// Or set anonymous ID
logs.SetAnonymousID("anon-456")