Logging
Log Locations
Section titled “Log Locations”| Service | Log Path |
|---|---|
| Nginx | /var/log/nginx/access.log, error.log |
| Supervisor | /var/log/supervisor/ |
| Docker | docker logs <container> |
| Systemd | journalctl -u <service> |
| PHP-FPM | /var/log/php-fpm/ |
Basic Commands
Section titled “Basic Commands”# Tail logs in real-timetail -f /var/log/nginx/access.log
# Last 100 linestail -n 100 /var/log/nginx/error.log
# Search logsgrep "error" /var/log/nginx/error.loggrep -i "500" /var/log/nginx/access.log
# Journalctl (systemd)journalctl -u snapcode -fjournalctl -u snapcode --since "1 hour ago"Log Rotation
Section titled “Log Rotation”/etc/logrotate.d/snapcode:
/var/log/snapcode/*.log { daily rotate 14 compress delaycompress missingok notifempty create 0640 www-data www-data postrotate systemctl reload snapcode > /dev/null 2>&1 || true endscript}Docker Logging
Section titled “Docker Logging”# View logsdocker logs snapcodedocker logs -f snapcode # Followdocker logs --tail 100 snapcode # Last 100
# Docker Composedocker-compose logs -f snapcodeStructured Logging (JSON)
Section titled “Structured Logging (JSON)”import jsonimport logging
class JSONFormatter(logging.Formatter): def format(self, record): return json.dumps({ 'timestamp': self.formatTime(record), 'level': record.levelname, 'message': record.getMessage(), 'module': record.module })Log Levels
Section titled “Log Levels”| Level | Use Case |
|---|---|
| DEBUG | Development details |
| INFO | General information |
| WARNING | Potential issues |
| ERROR | Errors that need attention |
| CRITICAL | System failures |
Centralized Logging (ELK)
Section titled “Centralized Logging (ELK)”App → Filebeat → Logstash → Elasticsearch → KibanaFor small projects, start with file-based logging and tail -f.