Enhance log mirroring service that continuously tails web server logs from Enhance and mirrors them to individual site directories with automatic rotation and retention management.
- Handles source log files that are frequently deleted, recreated, or truncated
- Daily log rotation at midnight
- Automatic cleanup of logs older than 30 days, can be changed by updating the "RETENTION_DAYS"
- File locking due protect from concurrent writes
- Python: 3.8 or higher
- Dependencies:
aiofiles- Asynchronous file operationswatchdog- File system monitoring
Source: /var/local/enhance/webserver_logs/<uuid>.log
↓
[Enhance Log Mirror]
↓
Destination: /var/www/<uuid>/access-logs/YYYY-MM-DD.log
The service:
- Watches
/var/local/enhance/webserver_logs/for log files - Tails each
<uuid>.logfile in real-time - Mirrors content to
/var/www/<uuid>/access-logs/ - Creates daily log files (e.g.,
2025-11-05.log) - Maintains ownership matching the site's directory
- Continues operation through file deletion/recreation/truncation
sudo apt update
sudo apt install -y python3 python3-venv python3-pipsudo mkdir -p /opt/enhance-log-mirror
cd /opt/enhance-log-mirrorpython3 -m venv venv
source venv/bin/activatepip install aiofiles watchdogCopy the app.py script to /opt/enhance-log-mirror/app.py:
# Download or copy the script
sudo nano /opt/enhance-log-mirror/app.py
# Paste the script content and save
# Make executable
sudo chmod +x /opt/enhance-log-mirror/app.pyCreate the systemd unit file:
sudo nano /etc/systemd/system/enhance-log-mirror.servicePaste the following content:
[Unit]
Description=Enhance Log Mirror
After=network.target
[Service]
ExecStart=/opt/enhance-log-mirror/venv/bin/python /opt/enhance-log-mirror/app.py
Restart=always
RestartSec=5
Type=simple
User=root
Group=root
ProtectSystem=full
ProtectHome=yes
PrivateTmp=yes
NoNewPrivileges=yes
ReadWritePaths=/var/local/enhance/webserver_logs /var/www
LimitNOFILE=65535
[Install]
WantedBy=multi-user.targetSave and exit (Ctrl+X, Y, Enter).
# Reload systemd to recognize the new service
sudo systemctl daemon-reload
# Enable the service to start on boot
sudo systemctl enable enhance-log-mirror
# Start the service
sudo systemctl start enhance-log-mirror
# Check service status
sudo systemctl status enhance-log-mirrorStart the service:
sudo systemctl start enhance-log-mirrorStop the service:
sudo systemctl stop enhance-log-mirrorRestart the service:
sudo systemctl restart enhance-log-mirrorCheck service status:
sudo systemctl status enhance-log-mirrorEnable on boot:
sudo systemctl enable enhance-log-mirrorDisable on boot:
sudo systemctl disable enhance-log-mirrorView application logs:
sudo tail -f /var/log/enhance-mirror.logView systemd service logs:
sudo journalctl -u enhance-log-mirror -fView recent service logs:
sudo journalctl -u enhance-log-mirror -n 100View logs from today:
sudo journalctl -u enhance-log-mirror --since todayView logs with full details:
sudo journalctl -u enhance-log-mirror -xeCheck how many tailers are active:
sudo grep "active tailers" /var/log/enhance-mirror.log | tail -1View statistics (reported every 5 minutes):
sudo grep "Stats:" /var/log/enhance-mirror.log | tail -5Check for file recreations:
sudo grep "File recreated" /var/log/enhance-mirror.logCheck for truncations:
sudo grep "File truncated" /var/log/enhance-mirror.logEdit /opt/enhance-log-mirror/app.py to modify configuration:
# ---------------- CONFIG ----------------
RETENTION_DAYS = 30
LOG_FILE = "/var/log/enhance-mirror.log"
# ---------------------------------------After modifying the configuration:
sudo systemctl restart enhance-log-mirror/var/local/enhance/webserver_logs/
├── uuid-1.log
├── uuid-2.log
└── uuid-3.log
/var/www/
├── uuid-1/
│ └── access-logs/
│ ├── 2025-11-01.log
│ ├── 2025-11-02.log
│ └── 2025-11-05.log
├── uuid-2/
│ └── access-logs/
│ └── 2025-11-05.log
└── uuid-3/
└── access-logs/
└── 2025-11-05.log
At midnight (00:00):
- All tailers rotate to new date-based log files
- Previous day's logs are closed
- New files created with format
YYYY-MM-DD.log
At 00:30 daily:
- Scans all site directories for old logs
- Deletes logs older than
RETENTION_DAYS(default: 30) - Skips non-log directories for efficiency
Check configuration:
sudo /opt/enhance-log-mirror/venv/bin/python /opt/enhance-log-mirror/app.pyCheck for lock file:
sudo rm /var/run/enhance-mirror-logs.lock
sudo systemctl start enhance-log-mirrorVerify source files exist:
ls -la /var/local/enhance/webserver_logs/Check service is running:
sudo systemctl status enhance-log-mirrorCheck application logs:
sudo tail -100 /var/log/enhance-mirror.logVerify tailers started:
sudo grep "Starting tailer" /var/log/enhance-mirror.logAdjust retention period if needed (edit RETENTION_DAYS in config).
To completely remove the service:
# Stop and disable service
sudo systemctl stop enhance-log-mirror
sudo systemctl disable enhance-log-mirror
# Remove systemd unit
sudo rm /etc/systemd/system/enhance-log-mirror.service
sudo systemctl daemon-reload
# Remove application
sudo rm -rf /opt/enhance-log-mirror
# Remove lock file
sudo rm -f /var/run/enhance-mirror-logs.lock
# Optional: Remove logs
sudo rm -f /var/log/enhance-mirror.logRun the script manually for testing:
cd /opt/enhance-log-mirror
source venv/bin/activate
python app.pyPress Ctrl+C to stop.
Enable debug logging by editing app.py:
logging.basicConfig(
level=logging.DEBUG, # Change from INFO to DEBUG
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(LOG_FILE),
logging.StreamHandler()
]
)- Initial release