Skip to content

🐳 Universal Log Monitor - Real-time Docker containers, system services, and file monitoring with intelligent service detection and auto-discovery

License

Notifications You must be signed in to change notification settings

patrickrizzardi/docker-log-watcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐳 Docker Log Watcher

License: MIT

Universal Real-time Log Monitoring for Docker Containers & System Services

A comprehensive Docker Compose setup that monitors both Docker containers and system services with real-time log streaming, intelligent service detection, and automatic log rotation handling.

πŸš€ Features

Docker Container Monitoring

  • Live streaming of all Docker container logs using docker logs -f
  • Auto-detection of new containers (configurable interval, default: 0.1s)
  • Smart service classification based on container names
  • Prevents infinite loops by excluding itself from monitoring
  • Clean container lifecycle management with automatic cleanup

System Service Monitoring

  • Pre-configured services: nginx, apache2, httpd, mysql, postgresql, redis, mongodb, ssh, system logs
  • Auto-detection of running services and their log files
  • Real-time monitoring with configurable intervals
  • Service-specific log paths for each application

File System Monitoring

  • Auto-detection of log directories from mounted volumes
  • Real-time updates using inotify with polling fallback
  • Log rotation handling with automatic restart
  • Recursive scanning of subdirectories
  • Only monitors .log files for performance

Intelligent Service Detection

  • Docker containers are classified by name patterns:

    • web - nginx, web, http containers
    • api - api, backend, service containers
    • database - db, mysql, postgres, redis, mongo containers
    • worker - worker, queue, job containers
    • cache - cache, memcache, redis containers
    • proxy - proxy, gateway, router containers
    • monitor - monitor, metrics, stats containers
    • frontend - frontend, ui, client containers
    • container - fallback for unknown containers
  • System services are detected by log file paths:

    • nginx - nginx logs
    • apache - apache2/httpd logs
    • mysql - mysql logs
    • postgresql - postgresql logs
    • redis - redis logs
    • auth - authentication logs
    • syslog - system logs
    • kernel - kernel logs
    • And many more...

πŸ“‹ Output Format

  • Docker logs: [DOCKER:service-type] log message
  • System logs: [SYSTEM:service-name] log message
  • File logs: [FILE:service-name] log message

πŸ› οΈ Configuration

Environment Variables

All configuration is done through environment variables with sensible defaults:

Variable Default Description
DOCKER_CHECK_INTERVAL 0.1 Seconds between Docker container checks
SYSTEM_CHECK_INTERVAL 0.1 Seconds between system service checks
LOG_TAIL_LINES 50 Number of historical log lines to show
ENABLE_DOCKER_MONITORING true Enable Docker container monitoring
ENABLE_SYSTEM_MONITORING true Enable system service monitoring
ENABLE_FILE_MONITORING true Enable file system monitoring

Usage

Basic Usage (with defaults)

docker compose up --build

Custom Configuration

# Using environment variables
ENABLE_DOCKER_MONITORING=false docker compose up

# Using .env file
echo "ENABLE_DOCKER_MONITORING=false" > .env
echo "DOCKER_CHECK_INTERVAL=1.0" >> .env
docker compose up

Background Mode

docker compose up -d

πŸ“ File Structure

docker-log-watcher/
β”œβ”€β”€ docker-compose.yml          # Main compose file
β”œβ”€β”€ Dockerfile                  # Custom Alpine image
β”œβ”€β”€ env.example                 # Configuration template
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ main.sh                # Main orchestrator script
β”‚   β”œβ”€β”€ common.sh              # Shared utilities and functions
β”‚   └── unified-monitor.sh     # System + file monitoring
└── README.md                  # This file

πŸ”§ Advanced Configuration

Custom Log Directories

Mount additional volumes to monitor custom log directories:

# docker-compose.yml
volumes:
  - /var/log:/var/log:ro
  - /your/custom/logs:/your/custom/logs:ro
  - /app/logs:/app/logs:ro

Adding New System Services

Edit the SYSTEM_SERVICES array in scripts/unified-monitor.sh:

declare -A SYSTEM_SERVICES=(
    ["your-service"]="/path/to/your/logfile.log"
    ["another-service"]="/path/to/another/*.log"
)

Custom Docker Service Detection

Modify the detect_docker_service() function in scripts/main.sh to add new patterns:

case "$name_lower" in
    *your-pattern*)
        echo "your-service-type"
        ;;
    # ... existing patterns
esac

🎯 Use Cases

Development Environment

# Fast monitoring with all features enabled
DOCKER_CHECK_INTERVAL=0.1
SYSTEM_CHECK_INTERVAL=0.1
ENABLE_DOCKER_MONITORING=true
ENABLE_SYSTEM_MONITORING=true
ENABLE_FILE_MONITORING=true

Production Environment

# Slower monitoring, Docker only
DOCKER_CHECK_INTERVAL=1.0
SYSTEM_CHECK_INTERVAL=5.0
ENABLE_DOCKER_MONITORING=true
ENABLE_SYSTEM_MONITORING=false
ENABLE_FILE_MONITORING=false

System Monitoring Only

# No Docker, just system services
ENABLE_DOCKER_MONITORING=false
ENABLE_SYSTEM_MONITORING=true
ENABLE_FILE_MONITORING=true

πŸ” Monitoring Behavior

Docker Container Monitoring

  • Real-time streaming using docker logs -f --follow
  • Container lifecycle - automatically starts/stops monitoring
  • Service classification - intelligent naming based on container names
  • Self-exclusion - never monitors itself

System Service Monitoring

  • Process detection - checks if services are actually running
  • Log file validation - only monitors existing, readable files
  • Service-specific paths - each service has predefined log locations
  • Auto-restart - handles log rotation automatically

File System Monitoring

  • Volume-based detection - scans mounted volumes automatically
  • Real-time updates - uses inotify for instant detection
  • Polling fallback - 0.1s intervals if inotify unavailable
  • Log rotation - automatically handles rotated files
  • Recursive scanning - monitors subdirectories

🚨 Troubleshooting

No Docker Access

[ERROR] Cannot access Docker daemon. Make sure Docker socket is mounted.

Solution: Ensure /var/run/docker.sock is properly mounted

Missing Log Files

[WARN] Log file not found for service-name: /path/to/logfile.log

Solution: This is normal - the watcher will continue monitoring available files

Performance Issues

  • Increase DOCKER_CHECK_INTERVAL and SYSTEM_CHECK_INTERVAL
  • Disable unused monitoring types
  • Use specific log directories instead of broad monitoring

πŸ”’ Security Notes

  • Read-only mounts - All system directories mounted read-only
  • Docker socket - Mounted read-only for safety
  • Process isolation - Runs in containerized environment
  • Minimal permissions - Only reads logs, never modifies

πŸ“Š Example Output

╔══════════════════════════════════════════════════════════════════════════════╗
β•‘ 🐳 DOCKER LOG WATCHER 2024-01-15 10:30:15 β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

πŸ“Š CONTAINER STATUS
NAMES                    STATUS              PORTS
example-api-1       Up 10 minutes       0.0.0.0:5000->5000/tcp
example-db-1        Up 10 minutes       0.0.0.0:3306->3306/tcp

πŸ“Š CONFIGURATION
Docker monitoring: true (interval: 0.1s)
System monitoring: true (interval: 0.1s)
File monitoring: true
Log tail lines: 50

πŸ“Š LIVE LOGS (Press Ctrl+C to stop)

[DOCKER:api] Processing request...
[DOCKER:database] Connection established
[SYSTEM:nginx] 192.168.1.100 - GET /api/data
[SYSTEM:mysql] [Note] Server ready for connections
[FILE:redis] Redis server started

πŸŽ‰ Getting Started

  1. Clone or download this repository
  2. Run with defaults: docker compose up --build
  3. Customize by creating a .env file (see env.example)
  4. Monitor your logs in real-time!

The log watcher will automatically detect and start monitoring all your Docker containers and system services. No configuration needed to get started!

About

🐳 Universal Log Monitor - Real-time Docker containers, system services, and file monitoring with intelligent service detection and auto-discovery

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published