|
1 | | -# traefik-log-processor |
2 | | -Processing Traefik logs by splitting them into separate folders based on the "ServiceName" field (e.g., "5-service@http") and implementing log rotation and retention. |
| 1 | + |
| 2 | +# Traefik Log Processor |
| 3 | + |
| 4 | +This Docker Compose setup uses the `hhftechnology/traefik-log-processor` image to process Traefik logs, splitting them into separate folders based on the "ServiceName" field and implementing log rotation and retention. |
| 5 | + |
| 6 | +## Introduction |
| 7 | + |
| 8 | +The `hhftechnology/traefik-log-processor` image contains a Python script that reads Traefik's JSON-formatted logs from standard input, parses each log entry, and writes it to a file in a directory structure based on the "ServiceName" and the date. It also performs periodic cleanup to remove old log files based on the retention policy. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +- Docker |
| 13 | +- Docker Compose |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +To use this log processor with your existing Traefik setup: |
| 18 | + |
| 19 | +1. **Configure Traefik to write logs in JSON format to a file.** For example, in your Traefik configuration: |
| 20 | + |
| 21 | + ```yaml |
| 22 | + [log] |
| 23 | + filePath = "/logs/traefik.log" |
| 24 | + format = "json" |
| 25 | + ``` |
| 26 | + |
| 27 | + And in your Traefik Docker Compose service: |
| 28 | + |
| 29 | + ```yaml |
| 30 | + services: |
| 31 | + traefik: |
| 32 | + image: traefik:v3.3.4 |
| 33 | + volumes: |
| 34 | + - traefik_logs:/logs |
| 35 | + # ... other configurations ... |
| 36 | + ``` |
| 37 | + |
| 38 | +2. **Create a Docker Compose file for the log processor:** |
| 39 | + |
| 40 | + ```yaml |
| 41 | + services: |
| 42 | + log_processor: |
| 43 | + image: hhftechnology/traefik-log-processor |
| 44 | + volumes: |
| 45 | + - traefik_logs:/input_logs |
| 46 | + - processed_logs:/logs |
| 47 | + command: tail -F /input_logs/traefik.log | python /app/process_logs.py |
| 48 | + volumes: |
| 49 | + traefik_logs: |
| 50 | + external: true |
| 51 | + processed_logs: |
| 52 | + ``` |
| 53 | +
|
| 54 | + This assumes that `traefik_logs` is the volume where Traefik writes its log file. |
| 55 | + |
| 56 | +3. **Start the log processor:** |
| 57 | + |
| 58 | + Run the following command in the directory containing your `docker-compose.yml` file: |
| 59 | + |
| 60 | + ```bash |
| 61 | + docker-compose up -d |
| 62 | + ``` |
| 63 | + |
| 64 | +The processed logs will be written to the `processed_logs` volume, organized by service name and date, e.g., `/logs/<service_name>/<YYYY-MM-DD>.log`. |
| 65 | + |
| 66 | +## Customization |
| 67 | + |
| 68 | +You can customize the log processing behavior using environment variables: |
| 69 | + |
| 70 | +- `LOG_DIR`: Directory where processed logs are written (default: `/logs`) |
| 71 | +- `RETENTION_DAYS`: Number of days to retain logs (default: 30) |
| 72 | +- `CLEANUP_INTERVAL_HOURS`: Interval in hours between cleanup operations (default: 1) |
| 73 | + |
| 74 | +For example, to change the retention period to 7 days, update your `docker-compose.yml`: |
| 75 | + |
| 76 | +```yaml |
| 77 | +services: |
| 78 | + log_processor: |
| 79 | + image: hhftechnology/traefik-log-processor |
| 80 | + environment: |
| 81 | + - RETENTION_DAYS=7 |
| 82 | + volumes: |
| 83 | + - traefik_logs:/input_logs |
| 84 | + - processed_logs:/logs |
| 85 | + command: tail -F /input_logs/traefik.log | python /app/process_logs.py |
| 86 | +``` |
| 87 | + |
| 88 | +## Accessing Processed Logs |
| 89 | + |
| 90 | +To access the processed logs from the host, you can mount the `processed_logs` volume to a host directory. For example: |
| 91 | + |
| 92 | +```yaml |
| 93 | +volumes: |
| 94 | + processed_logs: |
| 95 | + driver: local |
| 96 | + driver_opts: |
| 97 | + type: none |
| 98 | + o: bind |
| 99 | + device: /host/path/to/processed/logs |
| 100 | +``` |
| 101 | + |
| 102 | +Then, the logs will be available at `/host/path/to/processed/logs/<service_name>/<YYYY-MM-DD>.log`. |
| 103 | + |
| 104 | +## License |
| 105 | + |
| 106 | +This project is licensed under the MIT License. |
0 commit comments