How to separate 4xx/5xx error logs into a different file?

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