emkcloud
(Massimo Della Rovere)
June 27, 2025, 6:48pm
1
Hi everyone,
I’m currently migrating from Apache to Caddy on a test EC2 server before deploying it in production. The last piece I’m struggling with is separating HTTP error logs (specifically 4xx and 5xx responses) into a dedicated log file.
My goal is to log all 4xx and 5xx responses (like 404, 403, 500, etc.) into a separate file from normal access logs. Any help or working examples would be greatly appreciated!
timelordx
(timelordx)
June 27, 2025, 10:36pm
2
Could you share your Caddyfile
? It’ll be much easier to help with your specific situation rather than trying to guess all the possible scenarios.
emkcloud
(Massimo Della Rovere)
June 27, 2025, 11:01pm
3
(logging) {
log {args[0]} {
output file /var/log/website/developer-access-{args[0]}.log {
mode 664
}
}
log_skip /check.html
}
https://website-developer.server.it {
root /home/ubuntu/environment/e-website/public
@admin path /admin /admin/*
import headers
import caching
import certificate domain.it
import encoding
import logging frontend
import processing
log admin {
output file /var/log/website/developer-access-admin.log {
mode 664
}
no_hostname
}
handle @admin {
log_name admin
}
}
I have file log for admin request and another for frontend request I need file separate log for request with 4xx and 5xx
timelordx
(timelordx)
June 27, 2025, 11:39pm
4
I think you’re almost there.
This might be possible to do more efficiently, but here’s a quick example I just put together. Feel free to adjust it to your needs:
:80 {
log log4xx5xx {
output file /tmp/caddy-error4xx5xx.log {
mode 664
}
no_hostname
}
intercept {
@4xx-5xx status 4xx 5xx
handle_response @4xx-5xx {
log_name log4xx5xx
}
}
respond /4xx 403
respond /5xx 503
respond 200
}
If you want to have separate log files for 4xx and 5xx errors, you can repeat the same setup for each status group individually.
2 Likes
emkcloud
(Massimo Della Rovere)
June 28, 2025, 1:02am
5
Thank you @timelordx is perfect !
# Define global
{
log {
output file /var/log/website/developer-system.log {
mode 664
}
}
}
# Define snippet
(logging) {
log {args[0]} {
output file /var/log/website/developer-access-{args[0]}.log {
mode 664
}
{args[1:]}
}
log_skip /check.html
}
# Define subdomain
https://website-developer.server.it {
root /home/ubuntu/environment/e-website/public
import headers
import caching
import certificate mydomain.it
import encoding
import processing
import logging frontend
import logging admin no_hostname
import logging error no_hostname
intercept {
@status-error status 4xx 5xx
handle_response @status-error {
log_name error
}
}
handle @admin {
log_name admin
}
}
I have system log, access admin log, frontend log and response errors log
9697 Jun 28 02:52 developer-access-admin.log
1875 Jun 28 02:53 developer-access-error.log
2097 Jun 28 02:53 developer-access-frontend.log
9752 Jun 28 03:00 developer-system.log
1 Like