To keep logging consistent across my Caddy web server, I use this configuration.
(log) {
log {
output stdout
format json
}
}
This setup applies the same JSON logging format across all sites.
mysite.com {
import log
}
The JSON format makes it easy to pipe log output to jq
for quick querying and formatting. For example, to monitor essential request data in real time.
sudo journalctl -xefu caddy | sed 's/^[^{]*//' | jq '{
timestamp: .ts,
remote_ip: .request.remote_ip,
method: .request.method,
host: .request.host,
uri: .request.uri,
user_agent: .request.headers["User-Agent"][0],
status: .status,
response_size: .size,
duration: .duration
}'
This command captures key fields, providing a streamlined view of web traffic.