Caddy is a powerful web server allowing you to serve your sites to the Internet. When exposing websites to the Internet, you want to know what kind of requests you’re receiving. Caddy supports request logging (also known as access logs).
This tutorial shows you how to enable request logging to write an access log for your site.
Caddy Series Overview
- Reverse Proxy a Node.js App
- Configure SSL for HTTPS
- Configure Logging and Access Logs
- Remove the "Server" Response Header
- Redirect (Sub)Domains
Log Requests to a File
Caddy has built-in log support. Use the log
directive to enable request logging. The log
directive is a block containing three options:
output
: the log destination (stdout, stderr, file, net, etc.)format
: the log format sent to the destination (console, json)level
: the log level (info, error)
This tutorial focuses on how to configure the output
. We’re skipping the format and level because they have thoughtful default settings.
Write Caddy Logs to a File
You can write the request logs to a file. Configure the log
directive to use an output file <path>
. A common log destination on Linux is /var/log
. We suggest using a dedicated folder for your caddy logs, like /var/logs/caddy
.
Here’s a sample configuration logging all incoming requests to a file:
superchargejs.com {
log {
output file /var/log/caddy/superchargejs.com-access.log
}
}
By default, Caddy rotates log files to prevent disk space exhaustion. Be careful when disabling log rotation. Disabled log rotation for a destination file may continuously grow in size and could cause disk space or writing issues on your system.
Customize Caddy’s Log Rotation Settings
Caddy’s request logging with a file destination has a default configuration of rolling a file at 100MB. By default, it keeps 10 log files for at most 90 days.
You can adjust the default settings to your own needs. For example, you may configure a roll_size
of 10MB and keep 20 log files for at most 30 days (720 hours).
Here’s a sample configuration telling Caddy to apply our custom log rotation settings:
superchargejs.com {
log {
output file /var/log/caddy/superchargejs.com-access.log {
roll_size 10mb
roll_keep 20
roll_keep_for 720h
}
}
}
Enjoy request logging with Caddy!