File Logging Configuration
File logging allows you to persist logs to disk with automatic rotation. Configure file paths and rotation intervals to suit your needs.
Enable File Logging
Enable file persistence:
Go
import logs "github.com/bytehide/bytehide-logs-go"
logs.SetPersist(true) // Enable file logging (default: false)
logs.SetFilePath("logs/application.log") // File path (default: "logs.txt")import logs "github.com/bytehide/bytehide-logs-go"
logs.SetPersist(true) // Enable file logging (default: false)
logs.SetFilePath("logs/application.log") // File path (default: "logs.txt")File Path Configuration
Basic File Path
Go
logs.SetPersist(true)
logs.SetFilePath("logs/app.log")logs.SetPersist(true)
logs.SetFilePath("logs/app.log")Environment-Based Paths
Go
import (
"os"
"strings"
logs "github.com/bytehide/bytehide-logs-go"
)
environment := os.Getenv("APP_ENVIRONMENT")
if environment == "" {
environment = "production"
}
logs.SetPersist(true)
logs.SetFilePath("logs/" + strings.ToLower(environment) + "/app.log")import (
"os"
"strings"
logs "github.com/bytehide/bytehide-logs-go"
)
environment := os.Getenv("APP_ENVIRONMENT")
if environment == "" {
environment = "production"
}
logs.SetPersist(true)
logs.SetFilePath("logs/" + strings.ToLower(environment) + "/app.log")Absolute File Path
Go
import (
"os"
"path/filepath"
logs "github.com/bytehide/bytehide-logs-go"
)
homeDir, _ := os.UserHomeDir()
logPath := filepath.Join(homeDir, ".myapp", "logs", "app.log")
logs.SetPersist(true)
logs.SetFilePath(logPath)import (
"os"
"path/filepath"
logs "github.com/bytehide/bytehide-logs-go"
)
homeDir, _ := os.UserHomeDir()
logPath := filepath.Join(homeDir, ".myapp", "logs", "app.log")
logs.SetPersist(true)
logs.SetFilePath(logPath)File Rotation Configuration
Rolling Intervals
Configure when files are rotated:
Go
import logs "github.com/bytehide/bytehide-logs-go"
logs.SetPersist(true)
logs.SetFilePath("logs/app.log")
logs.SetRollingInterval(logs.RollingIntervalDay) // Default: RollingIntervalDayimport logs "github.com/bytehide/bytehide-logs-go"
logs.SetPersist(true)
logs.SetFilePath("logs/app.log")
logs.SetRollingInterval(logs.RollingIntervalDay) // Default: RollingIntervalDayAvailable Intervals:
| Interval | Description |
|---|---|
logs.RollingIntervalMinute | Every minute |
logs.RollingIntervalHour | Every hour |
logs.RollingIntervalDay | Daily (recommended) |
logs.RollingIntervalWeek | Weekly |
logs.RollingIntervalMonth | Monthly |
logs.RollingIntervalYear | Yearly |
Configuration Examples
Development Environment
Go
logs.SetPersist(true)
logs.SetFilePath("logs/debug.log")
logs.SetRollingInterval(logs.RollingIntervalHour)logs.SetPersist(true)
logs.SetFilePath("logs/debug.log")
logs.SetRollingInterval(logs.RollingIntervalHour)Production Environment
Go
logs.SetPersist(true)
logs.SetFilePath("logs/production.log")
logs.SetRollingInterval(logs.RollingIntervalDay)logs.SetPersist(true)
logs.SetFilePath("logs/production.log")
logs.SetRollingInterval(logs.RollingIntervalDay)High-Volume Applications
Go
logs.SetPersist(true)
logs.SetFilePath("logs/high-volume.log")
logs.SetRollingInterval(logs.RollingIntervalHour)logs.SetPersist(true)
logs.SetFilePath("logs/high-volume.log")
logs.SetRollingInterval(logs.RollingIntervalHour)Complete File Logging Configuration
Go
import (
"os"
logs "github.com/bytehide/bytehide-logs-go"
)
func initializeFileLogging() {
// Enable file persistence
logs.SetPersist(true)
// Set file path
logs.SetFilePath("logs/application.log")
// Configure rotation
logs.SetRollingInterval(logs.RollingIntervalDay)
// Optional: Set minimum level for file logs
logs.SetMinimumLevel(logs.LogLevelInfo)
}import (
"os"
logs "github.com/bytehide/bytehide-logs-go"
)
func initializeFileLogging() {
// Enable file persistence
logs.SetPersist(true)
// Set file path
logs.SetFilePath("logs/application.log")
// Configure rotation
logs.SetRollingInterval(logs.RollingIntervalDay)
// Optional: Set minimum level for file logs
logs.SetMinimumLevel(logs.LogLevelInfo)
}File Management
Automatic Directory Creation
ByteHide Logger automatically creates directories if they don't exist:
Go
// This will create the full directory structure
logs.SetFilePath("logs/application/2024/app.log")// This will create the full directory structure
logs.SetFilePath("logs/application/2024/app.log")Checking File Existence
Go
import (
"os"
logs "github.com/bytehide/bytehide-logs-go"
)
logPath := "logs/app.log"
logs.SetPersist(true)
logs.SetFilePath(logPath)
// Verify directory exists (created automatically, but you can check)
logDir := filepath.Dir(logPath)
if _, err := os.Stat(logDir); os.IsNotExist(err) {
// Directory will be created by logger
}import (
"os"
logs "github.com/bytehide/bytehide-logs-go"
)
logPath := "logs/app.log"
logs.SetPersist(true)
logs.SetFilePath(logPath)
// Verify directory exists (created automatically, but you can check)
logDir := filepath.Dir(logPath)
if _, err := os.Stat(logDir); os.IsNotExist(err) {
// Directory will be created by logger
}File Permissions (Linux/macOS)
Ensure your application has write permissions to the log directory:
Bash
# Set permissions
chmod 755 logs/
# Verify
ls -la logs/# Set permissions
chmod 755 logs/
# Verify
ls -la logs/Log Rotation Naming
Files are automatically named based on rotation interval:
CODE
app.log # Current log file
app.log.2024-01-15 # Daily rotation
app.log.2024-01-15-10 # Hourly rotation
app.log.2024-01 # Monthly rotationapp.log # Current log file
app.log.2024-01-15 # Daily rotation
app.log.2024-01-15-10 # Hourly rotation
app.log.2024-01 # Monthly rotationBest Practices
File Logging Best Practices
- Use daily rotation for most applications (
RollingIntervalDay) - Use environment-specific paths to separate dev/staging/prod logs
- Monitor disk space in production environments
- Consider log retention policies for long-running applications
- Use absolute paths for clarity in multi-environment setups
Troubleshooting
| Issue | Solution |
|---|---|
| Permission denied | Check write permissions on log directory |
| Directory not found | Logger creates directories automatically, verify permissions |
| Disk space full | Implement log retention or increase rotation frequency |
| Logs not written | Verify SetPersist(true) was called |