Watching server logs in real-time is crucial for debugging and monitoring web applications. The classic LAMP stack (Linux, Apache, MySQL, PHP) generates several types of logs that developers should monitor:
# Main Apache access log (typical path)
/var/log/apache2/access.log
# Apache error log
/var/log/apache2/error.log
# PHP error log (may vary based on php.ini)
/var/log/php_errors.log
The tail
command is your primary tool for real-time log monitoring. Here's how to use it effectively:
# Basic real-time monitoring
tail -f /var/log/apache2/access.log
# Monitoring with line numbers
tail -n 50 -f /var/log/apache2/error.log
# Watching multiple logs simultaneously
tail -f /var/log/apache2/access.log /var/log/apache2/error.log
Filter logs to focus on specific patterns or errors:
# Filter for 500 errors
tail -f /var/log/apache2/access.log | grep " 500 "
# Monitor PHP fatal errors
tail -f /var/log/apache2/error.log | grep "PHP Fatal error"
# Track specific IP addresses
tail -f /var/log/apache2/access.log | grep "192.168.1.100"
For more complex monitoring scenarios:
# Colorize output for better readability
tail -f /var/log/apache2/access.log | grep --color=auto -E "POST|GET|500|404"
# Count occurrences in real-time
tail -f /var/log/apache2/access.log | awk '{print $9}' | sort | uniq -c
# Monitor slow requests
tail -f /var/log/apache2/access.log | awk '$NF > 5 {print}' # Requests taking >5 seconds
For more detailed logs, modify your Apache configuration:
# In httpd.conf or apache2.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D" custom
CustomLog /var/log/apache2/custom.log custom
The %D
at the end adds response time in microseconds, valuable for performance monitoring.
For production environments, consider these tools:
# Install multitail for multiple log viewing
sudo apt-get install multitail
# Using multitail
multitail -cS apache /var/log/apache2/access.log -cS apache_error /var/log/apache2/error.log
# Install lnav for advanced log navigation
sudo apt-get install lnav
Every backend developer working with LAMP stacks needs to master real-time log monitoring. Unlike graphical interfaces, terminal-based monitoring gives you raw, unfiltered access to server activity with minimal latency - crucial when debugging live systems.
For Apache servers, these commands should be in your toolkit:
# Basic log tailing
tail -f /var/log/apache2/access.log
# Monitor with grep filtering
tail -f /var/log/apache2/error.log | grep -i "PHP Warning"
# Colorized output with multitail
multitail -cS apache /var/log/apache2/error.log
# Watch multiple logs simultaneously
tail -f /var/log/apache2/{access,error}.log
For PHP-specific monitoring, configure these settings in php.ini:
log_errors = On
error_log = /var/log/php_errors.log
error_reporting = E_ALL
Then monitor with:
tail -f /var/log/php_errors.log | awk '{print strftime("%Y-%m-%d %H:%M:%S"), $0}'
Create more readable output with these formatting commands:
# Add timestamps to each line
tail -f /path/to/log | while read line; do echo "$(date): ${line}"; done
# Colorize HTTP status codes
tail -f access.log | \
sed -u 's/200$/\\033[32m&\\033[0m/g; s/404$/\\033[31m&\\033[0m/g; s/500$/\\033[31;1m&\\033[0m/g'
When dealing with high-traffic servers, use these optimized commands:
# Minimal overhead monitoring
tail ---follow=name ---retry /var/log/apache2/access.log
# Sample every 10th request for high-volume logs
tail -f /var/log/apache2/access.log | awk 'NR % 10 == 0'
For 24/7 monitoring, consider these approaches:
# Screen session for background monitoring
screen -S logmonitor -dm bash -c "tail -f /var/log/apache2/*.log"
# Systemd service unit example
[Unit]
Description=Apache Log Monitor
After=network.target
[Service]
ExecStart=/usr/bin/tail -f /var/log/apache2/error.log
Restart=always
[Install]
WantedBy=multi-user.target