|
1 | | - |
2 | 1 | # Traefik Log Processor |
3 | 2 |
|
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: |
| 3 | +A lightweight, resource-efficient tool that splits Traefik logs by service name while maintaining the original JSON format. |
28 | 4 |
|
29 | | - ```yaml |
30 | | - services: |
31 | | - traefik: |
32 | | - image: traefik:v3.3.4 |
33 | | - volumes: |
34 | | - - traefik_logs:/logs |
35 | | - # ... other configurations ... |
36 | | - ``` |
| 5 | +## Features |
37 | 6 |
|
38 | | -2. **Create a Docker Compose file for the log processor:** |
| 7 | +- Splits Traefik JSON logs based on `ServiceName` field |
| 8 | +- Preserves original log format and structure |
| 9 | +- Supports multiple input methods (file, directory monitoring, stdin) |
| 10 | +- Configurable log rotation (size-based and time-based) |
| 11 | +- Configurable log retention policies (age-based and count-based) |
| 12 | +- Minimal resource footprint (written in Go) |
| 13 | +- Runs in a lightweight container |
| 14 | +- Simple configuration via YAML file |
39 | 15 |
|
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 | | - ``` |
| 16 | +## Quick Start |
53 | 17 |
|
54 | | - This assumes that `traefik_logs` is the volume where Traefik writes its log file. |
| 18 | +```bash |
| 19 | +# Using Docker |
| 20 | +docker run -v /path/to/traefik/logs:/logs -v /path/to/output:/output \ |
| 21 | + -v /path/to/config.yaml:/app/config.yaml \ |
| 22 | + ghcr.io/hhftechnology/traefik-log-processor:latest |
55 | 23 |
|
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: |
| 24 | +# Using Docker Compose |
| 25 | +docker compose up -d |
| 26 | +``` |
69 | 27 |
|
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) |
| 28 | +## Configuration |
73 | 29 |
|
74 | | -For example, to change the retention period to 7 days, update your `docker-compose.yml`: |
| 30 | +Create a `config.yaml` file: |
75 | 31 |
|
76 | 32 | ```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 |
| 33 | +input: |
| 34 | + # Watch a single file |
| 35 | + file: "/logs/traefik.log" |
| 36 | + |
| 37 | + # Or watch a directory for log files |
| 38 | + # directory: "/logs" |
| 39 | + # pattern: "*.log" |
| 40 | + |
| 41 | + # Or read from stdin |
| 42 | + # stdin: true |
| 43 | + |
| 44 | +output: |
| 45 | + # Base directory for service-specific logs |
| 46 | + directory: "/output" |
| 47 | + |
| 48 | + # Format for service directories (supports templating) |
| 49 | + format: "{{.ServiceName}}" |
| 50 | + |
| 51 | +rotation: |
| 52 | + # Maximum size of each log file in MB |
| 53 | + max_size: 100 |
| 54 | + |
| 55 | + # Maximum age of each log file in days |
| 56 | + max_age: 7 |
| 57 | + |
| 58 | + # Maximum number of old log files to retain |
| 59 | + max_backups: 5 |
| 60 | + |
| 61 | + # Whether to compress old log files |
| 62 | + compress: true |
| 63 | + |
| 64 | +# Optional field mapping (useful for CLF format or adding fields) |
| 65 | +field_mapping: |
| 66 | + # You can rename fields or add new computed fields |
| 67 | + # Example for adding a RouterName field: |
| 68 | + # router_name: "{{extractRouterName .ServiceName}}" |
86 | 69 | ``` |
87 | 70 |
|
88 | | -## Accessing Processed Logs |
| 71 | +## How It Works |
89 | 72 |
|
90 | | -To access the processed logs from the host, you can mount the `processed_logs` volume to a host directory. For example: |
| 73 | +1. The application watches Traefik log files or reads from stdin |
| 74 | +2. Each log line is parsed as JSON |
| 75 | +3. The `ServiceName` field is extracted (e.g., `5-service@http`) |
| 76 | +4. The log entry is written to a service-specific directory |
| 77 | +5. Log rotation and retention policies are applied to manage storage |
91 | 78 |
|
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 |
| 79 | +## Building from Source |
| 80 | + |
| 81 | +```bash |
| 82 | +git clone https://github.com/hhftechnology/traefik-log-processor.git |
| 83 | +cd traefik-log-processor |
| 84 | +go build -o traefik-log-processor cmd/main.go |
100 | 85 | ``` |
101 | 86 |
|
102 | | -Then, the logs will be available at `/host/path/to/processed/logs/<service_name>/<YYYY-MM-DD>.log`. |
| 87 | +## Contributing |
| 88 | + |
| 89 | +Contributions are welcome! Please feel free to submit a Pull Request. |
103 | 90 |
|
104 | 91 | ## License |
105 | 92 |
|
106 | | -This project is licensed under the MIT License. |
| 93 | +This project is licensed under the MIT License - see the LICENSE file for details. |
0 commit comments