File Logging
File Logging
Persist application logs to files with automatic rotation by time and size.
Enable File Logging
Enable persistence to write logs to disk:
from bytehide_logs import Log, LogSettings
settings = LogSettings(
persist=True,
file_path="./logs/app.log"
)
Log.initialize(settings)
Log.info("This message is written to file")from bytehide_logs import Log, LogSettings
settings = LogSettings(
persist=True,
file_path="./logs/app.log"
)
Log.initialize(settings)
Log.info("This message is written to file")File Path Configuration
Absolute Path
Use absolute paths for reliability:
settings = LogSettings(
persist=True,
file_path="/var/log/myapp/app.log"
)settings = LogSettings(
persist=True,
file_path="/var/log/myapp/app.log"
)Relative Path
Relative to the current working directory:
settings = LogSettings(
persist=True,
file_path="./logs/app.log"
)settings = LogSettings(
persist=True,
file_path="./logs/app.log"
)Dynamic Path
Create paths based on environment or timestamp:
import os
from datetime import datetime
app_name = "myapp"
env = os.getenv("ENV", "dev")
log_file = f"./logs/{app_name}_{env}_{datetime.now().strftime('%Y%m%d')}.log"
settings = LogSettings(
persist=True,
file_path=log_file
)import os
from datetime import datetime
app_name = "myapp"
env = os.getenv("ENV", "dev")
log_file = f"./logs/{app_name}_{env}_{datetime.now().strftime('%Y%m%d')}.log"
settings = LogSettings(
persist=True,
file_path=log_file
)Directory Creation
Ensure the log directory exists:
import os
log_dir = "./logs"
os.makedirs(log_dir, exist_ok=True)
settings = LogSettings(
persist=True,
file_path=os.path.join(log_dir, "app.log")
)import os
log_dir = "./logs"
os.makedirs(log_dir, exist_ok=True)
settings = LogSettings(
persist=True,
file_path=os.path.join(log_dir, "app.log")
)Rolling Intervals
Automatically rotate log files at regular time intervals.
Daily Rotation (Default)
from bytehide_logs import Log, LogSettings, RollingInterval
settings = LogSettings(
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.DAY
)
Log.initialize(settings)
# New file created: app_2026-03-02.log
# Next day: app_2026-03-03.logfrom bytehide_logs import Log, LogSettings, RollingInterval
settings = LogSettings(
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.DAY
)
Log.initialize(settings)
# New file created: app_2026-03-02.log
# Next day: app_2026-03-03.logHourly Rotation
settings = LogSettings(
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.HOUR
)
# File names: app_2026-03-02_14.log, app_2026-03-02_15.logsettings = LogSettings(
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.HOUR
)
# File names: app_2026-03-02_14.log, app_2026-03-02_15.logWeekly Rotation
settings = LogSettings(
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.WEEK
)
# New file every Mondaysettings = LogSettings(
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.WEEK
)
# New file every MondayMonthly Rotation
settings = LogSettings(
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.MONTH
)
# New file every monthsettings = LogSettings(
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.MONTH
)
# New file every monthYearly Rotation
settings = LogSettings(
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.YEAR
)
# New file every January 1stsettings = LogSettings(
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.YEAR
)
# New file every January 1stMinute Rotation (Testing)
settings = LogSettings(
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.MINUTE
)
# Creates new file every minute (useful for testing)settings = LogSettings(
persist=True,
file_path="./logs/app.log",
rolling_interval=RollingInterval.MINUTE
)
# Creates new file every minute (useful for testing)Size-Based Rotation
Rotate files when they exceed a size limit.
Enable Size-Based Rotation
settings = LogSettings(
persist=True,
file_path="./logs/app.log",
roll_on_file_size_limit=True,
file_size_limit_bytes=10485760 # 10MB
)
Log.initialize(settings)settings = LogSettings(
persist=True,
file_path="./logs/app.log",
roll_on_file_size_limit=True,
file_size_limit_bytes=10485760 # 10MB
)
Log.initialize(settings)Size Limits
Common file size limits:
# 1 MB
file_size_limit_bytes = 1048576
# 10 MB
file_size_limit_bytes = 10485760
# 50 MB
file_size_limit_bytes = 52428800
# 100 MB (default)
file_size_limit_bytes = 104857600
# 500 MB
file_size_limit_bytes = 524288000# 1 MB
file_size_limit_bytes = 1048576
# 10 MB
file_size_limit_bytes = 10485760
# 50 MB
file_size_limit_bytes = 52428800
# 100 MB (default)
file_size_limit_bytes = 104857600
# 500 MB
file_size_limit_bytes = 524288000Combined Rotation
Rotate by both time and size:
from bytehide_logs import Log, LogSettings, RollingInterval
settings = LogSettings(
persist=True,
file_path="./logs/app.log",
# Time-based rotation
rolling_interval=RollingInterval.DAY,
# Size-based rotation
roll_on_file_size_limit=True,
file_size_limit_bytes=52428800 # 50MB
)
Log.initialize(settings)
# Rotates at midnight OR when file exceeds 50MBfrom bytehide_logs import Log, LogSettings, RollingInterval
settings = LogSettings(
persist=True,
file_path="./logs/app.log",
# Time-based rotation
rolling_interval=RollingInterval.DAY,
# Size-based rotation
roll_on_file_size_limit=True,
file_size_limit_bytes=52428800 # 50MB
)
Log.initialize(settings)
# Rotates at midnight OR when file exceeds 50MBProduction Configuration
import os
from bytehide_logs import Log, LogSettings, LogLevel, RollingInterval
settings = LogSettings(
# No console in production
console_enabled=False,
# Write to persistent storage
persist=True,
file_path="/var/log/myapp/app.log",
# Only important messages
minimum_level=LogLevel.WARN,
# Daily rotation
rolling_interval=RollingInterval.DAY,
# Also rotate on size
roll_on_file_size_limit=True,
file_size_limit_bytes=52428800, # 50MB
# Include source info for debugging
include_caller_info=True,
# Mask sensitive data
mask_sensitive_data=["password", "api_key", "token"]
)
Log.set_project_token(os.getenv("PROJECT_TOKEN"))
Log.initialize(settings)import os
from bytehide_logs import Log, LogSettings, LogLevel, RollingInterval
settings = LogSettings(
# No console in production
console_enabled=False,
# Write to persistent storage
persist=True,
file_path="/var/log/myapp/app.log",
# Only important messages
minimum_level=LogLevel.WARN,
# Daily rotation
rolling_interval=RollingInterval.DAY,
# Also rotate on size
roll_on_file_size_limit=True,
file_size_limit_bytes=52428800, # 50MB
# Include source info for debugging
include_caller_info=True,
# Mask sensitive data
mask_sensitive_data=["password", "api_key", "token"]
)
Log.set_project_token(os.getenv("PROJECT_TOKEN"))
Log.initialize(settings)Development Configuration
from bytehide_logs import Log, LogSettings, LogLevel, RollingInterval
settings = LogSettings(
# See output immediately
console_enabled=True,
# Also save to file
persist=True,
file_path="./logs/dev.log",
# All messages
minimum_level=LogLevel.DEBUG,
# Simple rotation
rolling_interval=RollingInterval.DAY,
# Include debugging info
include_caller_info=True
)
Log.initialize(settings)from bytehide_logs import Log, LogSettings, LogLevel, RollingInterval
settings = LogSettings(
# See output immediately
console_enabled=True,
# Also save to file
persist=True,
file_path="./logs/dev.log",
# All messages
minimum_level=LogLevel.DEBUG,
# Simple rotation
rolling_interval=RollingInterval.DAY,
# Include debugging info
include_caller_info=True
)
Log.initialize(settings)High-Volume Logging
Configure for applications with high log volume:
from bytehide_logs import Log, LogSettings, LogLevel, RollingInterval
settings = LogSettings(
persist=True,
file_path="./logs/app.log",
# Hourly rotation for frequent rolling
rolling_interval=RollingInterval.HOUR,
# Rotate at 100MB to prevent huge files
roll_on_file_size_limit=True,
file_size_limit_bytes=104857600,
# Reduce console overhead
console_enabled=False,
# Only important messages
minimum_level=LogLevel.WARN
)
Log.initialize(settings)from bytehide_logs import Log, LogSettings, LogLevel, RollingInterval
settings = LogSettings(
persist=True,
file_path="./logs/app.log",
# Hourly rotation for frequent rolling
rolling_interval=RollingInterval.HOUR,
# Rotate at 100MB to prevent huge files
roll_on_file_size_limit=True,
file_size_limit_bytes=104857600,
# Reduce console overhead
console_enabled=False,
# Only important messages
minimum_level=LogLevel.WARN
)
Log.initialize(settings)Log File Naming
ByteHide automatically generates file names with timestamps:
app.log # Base name
app_2026-03-02.log # Daily rotation
app_2026-03-02_14.log # Hourly rotationapp.log # Base name
app_2026-03-02.log # Daily rotation
app_2026-03-02_14.log # Hourly rotationManaging Log Files
View Recent Logs
# Tail last 50 lines
tail -50 /var/log/myapp/app.log
# Real-time monitoring
tail -f /var/log/myapp/app.log# Tail last 50 lines
tail -50 /var/log/myapp/app.log
# Real-time monitoring
tail -f /var/log/myapp/app.logCompress Old Logs
# Compress files older than 7 days
find /var/log/myapp -name "*.log" -mtime +7 -exec gzip {} \;# Compress files older than 7 days
find /var/log/myapp -name "*.log" -mtime +7 -exec gzip {} \;Clean Up Old Logs
# Remove logs older than 30 days
find /var/log/myapp -name "*.log*" -mtime +30 -delete# Remove logs older than 30 days
find /var/log/myapp -name "*.log*" -mtime +30 -deleteLog File Permissions
Set appropriate permissions for log files:
# Create log directory with restricted permissions
mkdir -p /var/log/myapp
chmod 755 /var/log/myapp
# Restrict log file access
chmod 644 /var/log/myapp/app.log# Create log directory with restricted permissions
mkdir -p /var/log/myapp
chmod 755 /var/log/myapp
# Restrict log file access
chmod 644 /var/log/myapp/app.logTroubleshooting
Log Files Not Created
Ensure the directory exists and is writable:
import os
log_dir = "./logs"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
print(f"Created directory: {log_dir}")
settings = LogSettings(
persist=True,
file_path=os.path.join(log_dir, "app.log")
)
Log.initialize(settings)
Log.info("Testing file logging")import os
log_dir = "./logs"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
print(f"Created directory: {log_dir}")
settings = LogSettings(
persist=True,
file_path=os.path.join(log_dir, "app.log")
)
Log.initialize(settings)
Log.info("Testing file logging")Permission Denied
Change the file path to a writable location:
# Instead of /var/log (requires root)
# Use current directory
settings = LogSettings(
persist=True,
file_path="./logs/app.log" # Writable by current user
)# Instead of /var/log (requires root)
# Use current directory
settings = LogSettings(
persist=True,
file_path="./logs/app.log" # Writable by current user
)Disk Space Issues
Monitor and manage disk space for log files:
# Check disk usage
du -sh /var/log/myapp
# Check available space
df -h /var/log
# List files by size
ls -lhS /var/log/myapp/*.log# Check disk usage
du -sh /var/log/myapp
# Check available space
df -h /var/log
# List files by size
ls -lhS /var/log/myapp/*.logCombining Console and File
from bytehide_logs import Log, LogSettings, LogLevel
settings = LogSettings(
# Show in console
console_enabled=True,
# Also save to file
persist=True,
file_path="./logs/app.log",
# File includes everything
rolling_interval=RollingInterval.DAY,
roll_on_file_size_limit=True,
file_size_limit_bytes=52428800
)
Log.initialize(settings)
Log.info("This appears in console and file")from bytehide_logs import Log, LogSettings, LogLevel
settings = LogSettings(
# Show in console
console_enabled=True,
# Also save to file
persist=True,
file_path="./logs/app.log",
# File includes everything
rolling_interval=RollingInterval.DAY,
roll_on_file_size_limit=True,
file_size_limit_bytes=52428800
)
Log.initialize(settings)
Log.info("This appears in console and file")Flush Logs
Ensure all logs are written to disk:
Log.info("Application shutting down")
Log.flush() # Ensure all logs are writtenLog.info("Application shutting down")
Log.flush() # Ensure all logs are writtenUse in shutdown handlers:
import signal
def shutdown_handler(signum, frame):
Log.info("Shutdown signal received")
Log.flush()
exit(0)
signal.signal(signal.SIGTERM, shutdown_handler)import signal
def shutdown_handler(signum, frame):
Log.info("Shutdown signal received")
Log.flush()
exit(0)
signal.signal(signal.SIGTERM, shutdown_handler)Next Steps
- Quick Start - Initialize logging
- Log Levels - Control verbosity
- Console Output - Configure console display
- Environment Variables - Configure via environment