/

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:

Python
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:

Python
settings = LogSettings(
    persist=True,
    file_path="/var/log/myapp/app.log"
)

Relative Path

Relative to the current working directory:

Python
settings = LogSettings(
    persist=True,
    file_path="./logs/app.log"
)

Dynamic Path

Create paths based on environment or timestamp:

Python
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:

Python
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)

Python
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.log

Hourly Rotation

Python
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.log

Weekly Rotation

Python
settings = LogSettings(
    persist=True,
    file_path="./logs/app.log",
    rolling_interval=RollingInterval.WEEK
)

# New file every Monday

Monthly Rotation

Python
settings = LogSettings(
    persist=True,
    file_path="./logs/app.log",
    rolling_interval=RollingInterval.MONTH
)

# New file every month

Yearly Rotation

Python
settings = LogSettings(
    persist=True,
    file_path="./logs/app.log",
    rolling_interval=RollingInterval.YEAR
)

# New file every January 1st

Minute Rotation (Testing)

Python
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

Python
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:

Python
# 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

Combined Rotation

Rotate by both time and size:

Python
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 50MB

Production Configuration

Python
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

Python
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:

Python
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:

CODE
app.log              # Base name
app_2026-03-02.log   # Daily rotation
app_2026-03-02_14.log # Hourly rotation

Managing Log Files

View Recent Logs

Bash
# Tail last 50 lines
tail -50 /var/log/myapp/app.log

# Real-time monitoring
tail -f /var/log/myapp/app.log

Compress Old Logs

Bash
# Compress files older than 7 days
find /var/log/myapp -name "*.log" -mtime +7 -exec gzip {} \;

Clean Up Old Logs

Bash
# Remove logs older than 30 days
find /var/log/myapp -name "*.log*" -mtime +30 -delete

Log File Permissions

Set appropriate permissions for log files:

Bash
# 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

Troubleshooting

Log Files Not Created

Ensure the directory exists and is writable:

Python
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:

Python
# 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:

Bash
# 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

Combining Console and File

Python
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:

Python
Log.info("Application shutting down")
Log.flush()  # Ensure all logs are written

Use in shutdown handlers:

Python
import signal

def shutdown_handler(signum, frame):
    Log.info("Shutdown signal received")
    Log.flush()
    exit(0)

signal.signal(signal.SIGTERM, shutdown_handler)

Next Steps

Previous
Console Output