Caddy日誌配置輪轉和格式化
Nginx 日誌配置方式靈活多變,caddy 日誌配置也類似,示例如下,重點爲輪轉和部分格式調整:
mephisto.cc {
... 戰術省略 ...
log {
output file /var/log/caddy/mephisto.cc.log {
roll_size 10mb
roll_keep 20
roll_keep_for 7d
}
format json {
time_local
time_format wall_milli
}
}
}
1. 示例解釋:
output <writer_module>
此處 <writer_module> 可以是:stderr | stdout | discard | file | net, 分別表示:輸出到 標準錯誤 | 標準輸出 | 放棄(勤儉持家,不高興不要日誌啦!) | 文件 | 網絡地址(比如 syslog 等), 示例爲輸出到本地文件 /var/log/caddy/mephisto.cc.log
輸出目標爲 file 的時候,可配選項爲:
-
roll_disabled
關閉輪轉,會導致磁盤寫滿告警,除非你有其它兜底方式解決 或者 不在乎這個問題,否則不要配置。 disables log rolling. This can lead to disk space depletion, so only use this if your log files are maintained some other way.
-
roll_size
文件大小多大開始輪轉,單位 megabyte,小數值向上取整,默認 100M is the size at which to roll the log file. 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
是否開啓 gzip 壓縮 turns off gzip log compression. Default: gzip compression is enabled.
-
roll_local_time
輪轉後的文件名所帶時間的格式是否設置爲本地格式,默認 utc time sets the rolling to use local timestamps in filenames. Default: uses UTC time.
-
roll_keep
保留多少個輪轉文件 is how many log files to keep before deleting the oldest ones. Default: 10
-
roll_keep_for
輪轉後的文件保留多長時間,d 或者 h,看下面引文解釋,懶得翻譯了 默認:2160h(90 天) is 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. 示例解釋
format <encoder_module>
此處 <encoder_module> 可以是:** console | json | filter 等
json 表示日誌會輸出爲 json 格式,一行一個:
{"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":2057,"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"]}}
{"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"]}}
每種類型有多個配置項:
-
time_local 日誌裏面 ts 這個字段的格式跟隨系統的本地時間格式,默認 Logs with the local system time rather than the default of UTC time.
-
time_format
- wall_milli 時間格式:2006/01/02 15:04:05.000,我選的這個,比較符合中國大陸習慣。
調整後日志示例爲:
{"level":"info","ts":"2022/10/19 05:56:12.777","logger":"http.log.access.log1","msg":"handled request","request":{"remote_ip":"2409:891f:1ae7:8859:c1b0:fe4f:15ed:bcfa","remote_port":"49577","proto":"HTTP/2.0","method":"GET","host":"mephisto.cc","uri":"/images/geo_data_viewer_3d.webp","headers":{"Referer":["https://mephisto.cc/tech/gpx/"],"Cookie":[],"Accept":["image/webp,image/avif,video/*;q=0.8,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5"],"Accept-Encoding":["gzip, deflate, br"],"User-Agent":["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"],"Accept-Language":["en-US,en;q=0.9"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"mephisto.cc"}},"user_id":"","duration":0.002766397,"size":128076,"status":200,"resp_headers":{"Access-Control-Allow-Origin":["*"],"Etag":["\"rjw2fi2qto\""],"Content-Type":["image/webp"],"Accept-Ranges":["bytes"],"Server":["Caddy"],"Cache-Control":["max-age=1705200"],"Last-Modified":["Mon, 17 Oct 2022 08:33:18 GMT"],"Content-Length":["128076"],"Alt-Svc":["h3=\":443\"; ma=2592000"]}}
當然其它可配選項還有很多,看不明白的可以測試選擇適合自己的。官方文檔鏈接爲:https://caddyserver.com/docs/caddyfile/directives/log , 個人小站,這樣配置已經夠用了。
另外,服務器調整時區相關如下
sudo timedatectl set-timezone your_time_zone
例如,想要將系統時區設置爲 Asia/Shanghai:
sudo timedatectl set-timezone Asia/Shanghai
時區列表獲取過濾:
sudo timedatectl list-timezones |grep -i shanghai
查看結果 timedatectl:
➜ timedatectl
Local time: Wed 2022-10-19 16:14:21 CST
Universal time: Wed 2022-10-19 08:14:21 UTC
RTC time: Wed 2022-10-19 08:14:21
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
版權申明:
- 未標註來源的內容皆為原創,未經授權請勿轉載(因轉載後排版往往錯亂、內容不可控、無法持續更新等);
- 非營利為目的,演繹本博客任何內容,請以'原文出處'或者'參考鏈接'等方式給出本站相關網頁地址(方便讀者)。