Skip to content

Commit f07710a

Browse files
committed
Change-over to go
1 parent 2d130d1 commit f07710a

File tree

8 files changed

+714
-161
lines changed

8 files changed

+714
-161
lines changed

Dockerfile

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
1-
FROM python:3.9-slim
1+
FROM golang:1.21-alpine AS builder
2+
3+
WORKDIR /build
4+
5+
# Copy go mod and sum files
6+
COPY go.mod go.sum ./
7+
8+
# Download dependencies
9+
RUN go mod download
10+
11+
# Copy source code
12+
COPY cmd/ ./cmd/
13+
14+
# Build the application
15+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o traefik-log-processor ./cmd/main.go
16+
17+
# Use a minimal alpine image for the final container
18+
FROM alpine:3.17
19+
20+
# Add ca-certificates for any HTTPS connections
21+
RUN apk --no-cache add ca-certificates tzdata
22+
223
WORKDIR /app
3-
COPY process_logs.py .
4-
CMD ["python", "process_logs.py"]
24+
25+
# Copy the binary from the builder stage
26+
COPY --from=builder /build/traefik-log-processor .
27+
28+
# Copy default config
29+
COPY config.yaml .
30+
31+
# Create directories for logs
32+
RUN mkdir -p /logs /output
33+
34+
# Set user to non-root
35+
RUN addgroup -g 1000 appuser && \
36+
adduser -u 1000 -G appuser -h /app -s /bin/sh -D appuser && \
37+
chown -R appuser:appuser /app /logs /output
38+
39+
USER appuser
40+
41+
# Command to run
42+
ENTRYPOINT ["/app/traefik-log-processor"]
43+
CMD ["--config", "/app/config.yaml"]

README.md

Lines changed: 73 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,93 @@
1-
21
# Traefik Log Processor
32

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.
284

29-
```yaml
30-
services:
31-
traefik:
32-
image: traefik:v3.3.4
33-
volumes:
34-
- traefik_logs:/logs
35-
# ... other configurations ...
36-
```
5+
## Features
376

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
3915

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
5317

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
5523

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+
```
6927

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
7329

74-
For example, to change the retention period to 7 days, update your `docker-compose.yml`:
30+
Create a `config.yaml` file:
7531

7632
```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}}"
8669
```
8770

88-
## Accessing Processed Logs
71+
## How It Works
8972

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
9178

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
10085
```
10186

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.
10390

10491
## License
10592

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

Comments
 (0)