惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Cloudflare Blog
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
博客园 - 司徒正美
V
Visual Studio Blog
G
Google Developers Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
aimingoo的专栏
aimingoo的专栏
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
D
Docker
Vercel News
Vercel News
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
爱范儿
爱范儿
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏

mephisto.cc

修复rime-ice无法弹出候选框的问题 开机滚动字体显示优化 个人网站安全防护 树莓派上部署Vaultwarden 使用goaccess实时分析Caddy日志 个人网站监控 Hugo全站AVIF记 Linux下尝试使用Godot开发小游戏 Arch linux dae 透明代理 Airflow接管galler-dl下载任务 如何使用gallery-dl批量下载图像 Arch核显下如何愉快玩Dota2和CS2 Arch/labwc 环境网络相关设置 Rime添加dota2词库 Arch 重装记录 Supertuxkart iOS版终于发布了 打造自己的流媒体音乐服务 剪切板管理工具clipcat推荐 MangoHud性能监控 微信小程序开发记 Rofi试用 解决Arch下VSCode无法输入中文的问题 独立窗口管理器下无法录屏问题处理 上海市二手房成交数据监控 OpenLDAP监控 Arch linux如何顺畅连接蓝牙设备 使用Git和Ansible管理配置文件 Arch Linux SSL VPN 客户端配置 简单获取celery任务实时结果 Arch linux下iNode客户端的安装和使用方法
Caddy日志配置轮转和格式化
2022-10-19 · via mephisto.cc

Nginx 日志配置方式灵活多变,caddy 日志配置也类似,示例如下,重点为轮转和部分格式调整:

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

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 格式,一行一个:

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":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"]}}
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"]}}

每种类型有多个配置项:

  • 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,我选的这个,比较符合中国大陆习惯。

调整后日志示例为(实际不会换行,这里只是格式化方便阅读):

 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}

当然其它可配选项还有很多,看不明白的可以测试选择适合自己的。官方文档链接为:https://caddyserver.com/docs/caddyfile/directives/log , 个人小站,这样配置已经够用了。

另外,服务器调整时区相关如下

1sudo timedatectl set-timezone your_time_zone

例如,想要将系统时区设置为 Asia/Shanghai:

1sudo timedatectl set-timezone Asia/Shanghai

时区列表获取过滤:

1sudo timedatectl list-timezones |grep -i shanghai

查看结果 timedatectl:

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