Caddy Log Rotation and Formatting Configuration

Nginx log configuration is flexible and varied, and Caddy log configuration is similar. The following example focuses on rotation and some formatting adjustments:

 1mephisto.cc {
 2
 3... Tactics omitted...
 4
 5log {
 6output file /var/log/caddy/mephisto.cc.log {
 7roll_size 10mb
 8roll_keep 20
 9roll_keep_for 7d
10}
11format json {
12time_local
13time_format wall_milli
14}
15}
16}

1. Example Explanation:

output <writer_module>

Here, <writer_module> can be: stderr | stdout | discard | file | net, respectively indicating output to standard error | standard output | discard (be frugal, don't log if you're unhappy!) | file | network address (such as syslog). The example outputs to a local file, /var/log/caddy/mephisto.cc.log

When the output destination is a file, the following options are available:

  • roll_disabled

Disabling rollover will result in disk full warnings. Do not configure this unless you have a fallback solution or don't care about this issue. Disables log rolling. This can lead to disk space depletion, so only use this if your log files are maintained in some other way.

  • roll_size

The file size at which to roll the log file. (Megabytes, fractional values are rounded up. Default: 100M)

This is the size at which the log file is rolled. The current implementation supports megabyte resolution; fractional values are rounded up to the next whole megabyte. For example, 1.1MiB is rounded up to 2MiB. Default: 100MiB

  • roll_uncompressed

Enables gzip compression.

Turns off gzip log compression. Default: gzip compression is enabled.

  • roll_local_time

Sets the time format in file names after rollover to the local time format. Default: UTC time.

Sets the rollover process to use local timestamps in file names. Default: UTC time.

  • roll_keep

How many log files to keep before deleting the oldest one. ones. Default: 10

  • roll_keep_for

How long to keep rolled files (d or h). See the following explanation (too lazy to translate). Default: 2160h (90 days).

This specifies how long to keep rolled files as a duration string. The current implementation supports day resolution; fractional values are rounded up to the next whole day. For example, 36h (1.5 days) is rounded up to 48h (2 days). Default: 2160h (90 days).

2. Example Explanation

format <encoder_module>

<encoder_module> can be: ** console | json | filter, etc.

json indicates that the log output will be in JSON format. Format, one per line:

1{"level":"info","ts":1654008681.039981,"logger":"http.log.access.log1","msg":"handled request","request":{"remote_ip":"2001:19f0:7001:2e1:5400:3ff:fe45:eb38","remote_port":"55598","proto":"HTTP/2 .0","method":"GET","host":"mephisto.cc","uri":"/icons/favicon-32x32.png","headers":{"User-Agent":["Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1"],"Accept-Language":["en-US,en;q=0.9"],"Referer":["https://mephisto.cc/"],"Cookie":[],"Accept":["*/*"],"Accept-Encoding":["gzip, deflate, br"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"mephisto.cc"}},"user_id":"","duration":0.000274438,"size":205 7,"status":200,"resp_headers":{"Content-Length":["2057"],"Server":["Caddy"],"Etag":["\"rcr3nq1l5\""],"Content-Type":["image/png"],"Last-Modified":["Tue, 31 May 2022 14:16:38 GMT"]}}
2{"level":"info","ts":1654008688.0219219,"logger":"http.log.access.log1","msg":"handled request","request":{"remote_ip":"2001:19f0:7001:2e1:5400:3ff:fe45:eb38","remote_port":"55596","proto":"HTTP/2.0","method":"GET","host":"mephisto.cc","uri ":"/images/sun.svg","headers":{"Cookie":[],"Accept":["image/webp,image/png,image/svg+xml,image/*;q=0.8,video/*;q=0.8,*/*;q=0.5"],"Accept-Encoding":["gzip, deflate, br"],"User-Agent":["Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1"],"Accept-Language":["en-US,en;q=0.9"],"Referer":["https://mephisto.cc/"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"mephisto. cc"}},"user_id":"","duration":0.000658347,"size":1268,"status":200,"resp_headers":{"Server" :["Caddy"],"Etag":["\"rcr3nq2x7\""],"Content-Type":["image/svg+xml"],"Last-Modified":["Tue, 31 May 2022 14:16:38 GMT"],"Content-Encoding":["gzip"],"Vary":["Accept-Encoding"]}}

Each type has multiple configuration options:

  • time_local The format of the ts field in the log follows the system's local time format. By default,

Logs use the local system time rather than the default of UTC time.

  • time_format
  • wall_milli

Time format: 2006/01/02 15:04:05.000. I chose this because it better aligns with local Chinese customs.

An example of the adjusted log is as follows (there are no line breaks, just formatted for easier reading):

 1{
 2"level": "info",
 3"ts": "2022/10/19 05:56:12.777",
 4"logger": "http.log.access.log1",
 5"msg": "handled request",
 6"request": {
 7"remote_ip": "2409:891f:1ae7:8859:c1b0:fe4f:15ed:bcfa",
 8"remote_port": "49577",
 9"proto": "HTTP/2.0",
10"method": "GET",
11"host": "mephisto.cc",
12"uri": "/images/geo_data_viewer_3d.webp",
13"headers": {
14"Referer": ["https://mephisto.cc/tech/gpx/"],
15"Cookie": [], 
16"Accept": [ 
17"image/webp,image/avif,video/*;q=0.8,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5" 
18], 
19"Accept-Encoding": ["gzip, deflate, br"], 
20"User-Agent": [ 
21"Mozilla/5.0 (iPhone; CPU iPhone OS 16_0_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1" 
22], 
23"Accept-Language": ["en-US,en;q=0.9"] 
24}, 
25"tls": { 
26"resumed": false, 
27"version": 772, 
28"cipher_suite": 4865, 
29"proto": "h2", 
30"server_name": "mephisto.cc" 
31} 
32}, 
33"user_id": "", 
34"duration": 0.002766397, 
35"size": 128076, 
36"status": 200, 
37"resp_headers": { 
38"Access-Control-Allow-Origin": ["*"], 
39"Etag": ["\"rjw2fi2qto\""],
40"Content-Type": ["image/webp"],
41"Accept-Ranges": ["bytes"],
42"Server": ["Caddy"],
43"Cache-Control": ["max-age=1705200"],
44"Last-Modified": ["Mon, 17 Oct 2022 08:33:18 GMT"],
45"Content-Length": ["128076"],
46"Alt-Svc": ["h3=\":443\"; ma=2592000"]
47}
48}

Of course, there are many other configurable options. If you're confused, you can test and choose the one that works for you. The official documentation link is: https://caddyserver.com/docs/caddyfile/directives/log This is my personal website; this configuration is sufficient.

In addition, the server time zone adjustment instructions are as follows:

1sudo timedatectl set-timezone your_time_zone

For example, to set the system time zone to Asia/Shanghai:

1sudo timedatectl set-timezone Asia/Shanghai

Get and filter the time zone list:

1sudo timedatectl list-timezones |grep -i shanghai

View the results using timedatectl:

1➜ timedatectl
2
3Local time: Wed 2022-10-19 16:14:21 CST
4Universal time: Wed 2022-10-19 08:14:21 UTC
5RTC time: Wed 2022-10-19 08:14:21
6Time zone: Asia/Shanghai (CST, +0800)
7System clock synchronized: yes 
8NTP service: active 
9RTC in local TZ: no
Lastmod: Wednesday, July 30, 2025

Translations: