外观
TiDB 日志级别调整
日志级别的定义
日志级别是用于控制日志输出详细程度的机制,不同级别的日志包含不同程度的信息:
| 级别 | 描述 | 适用场景 |
|---|---|---|
| trace | 最详细的日志,包含大量调试信息 | 开发调试,问题定位 |
| debug | 调试信息,包含函数调用、参数等 | 开发调试,复杂问题定位 |
| info | 一般信息,记录系统运行状态 | 正常运行,监控系统状态 |
| warn | 警告信息,记录潜在问题 | 系统异常,需要关注 |
| error | 错误信息,记录错误事件 | 系统错误,需要立即处理 |
| fatal | 致命错误,记录导致系统崩溃的事件 | 系统崩溃,需要紧急处理 |
| panic | panic 信息,记录会导致程序崩溃的严重错误 | 严重错误,系统会崩溃 |
TiDB 组件的日志级别
TiDB 集群中的每个组件都有自己独立的日志级别配置:
| 组件 | 默认日志级别 | 日志文件位置 |
|---|---|---|
| TiDB Server | info | /tidb-deploy/tidb-port/logs/tidb.log |
| PD Server | info | /tidb-deploy/pd-port/logs/pd.log |
| TiKV Server | info | /tidb-deploy/tikv-port/logs/tikv.log |
| TiFlash Server | info | /tidb-deploy/tiflash-port/logs/tiflash.log |
| TiCDC Server | info | /tidb-deploy/cdc-port/logs/cdc.log |
| Prometheus | info | /tidb-deploy/prometheus-port/logs/prometheus.log |
| Grafana | info | /tidb-deploy/grafana-port/logs/grafana.log |
日志级别调整方法
静态配置(配置文件)
TiDB 配置文件调整
toml
# /tidb-deploy/tidb-<port>/conf/tidb.toml
[log]
# 设置日志级别
level = "info"
# 设置日志文件
file = "/tidb-deploy/tidb-4000/logs/tidb.log"
# 设置日志格式
format = "text" # 可选:text, json
# 设置日志轮转策略
[log.rotate]
# 日志文件大小,超过则轮转
size = "300MB"
# 日志保留天数
days = 7
# 最大保留日志文件数
max-days = 0PD 配置文件调整
toml
# /tidb-deploy/pd-<port>/conf/pd.toml
[log]
# 设置日志级别
level = "info"
# 设置日志文件
file = "/tidb-deploy/pd-2379/logs/pd.log"
# 设置日志格式
format = "text"TiKV 配置文件调整
toml
# /tidb-deploy/tikv-<port>/conf/tikv.toml
[log]
# 设置日志级别
level = "info"
# 设置日志文件
file = "/tidb-deploy/tikv-20160/logs/tikv.log"
# 设置日志格式
format = "json"TiFlash 配置文件调整
toml
# /tidb-deploy/tiflash-<port>/conf/tiflash.toml
[logger]
# 设置日志级别
level = "info"
# 设置日志文件
log = "/tidb-deploy/tiflash-9000/logs/tiflash.log"动态调整(运行时调整)
使用 HTTP API 调整
调整 TiDB 日志级别
bash# 查看当前日志级别 curl -s http://<tidb-host>:10080/debug/v1/log/level # 设置日志级别为 debug curl -X POST -d "debug" http://<tidb-host>:10080/debug/v1/log/level # 设置特定模块的日志级别 curl -X POST -d "debug" http://<tidb-host>:10080/debug/v1/log/level?module=executor调整 PD 日志级别
bash# 查看当前日志级别 curl -s http://<pd-host>:2379/pd/api/v1/log/level # 设置日志级别为 debug curl -X POST -d "{\"level\": \"debug\"}" http://<pd-host>:2379/pd/api/v1/log/level调整 TiKV 日志级别
bash# 查看当前日志级别 curl -s http://<tikv-host>:20180/metrics | grep tikv_log_level # 使用 tikv-ctl 调整日志级别 tiup tikv-ctl --host <tikv-host>:20160 log --set-level debug # 设置特定模块的日志级别 tiup tikv-ctl --host <tikv-host>:20160 log --set-level debug --module raft调整 TiFlash 日志级别
bash# 使用 HTTP API 调整 TiFlash 日志级别 curl -X POST -d "{\"level\": \"debug\"}" http://<tiflash-host>:3930/log/level # 设置特定模块的日志级别 curl -X POST -d "{\"level\": \"debug\", \"module\": \"raft\"}" http://<tiflash-host>:3930/log/level
使用命令行工具调整
- 使用 tiup cluster 调整
bash
# 调整整个集群的日志级别
tiup cluster edit-config <cluster-name>
# 在配置文件中修改日志级别后,执行滚动重启
tiup cluster reload <cluster-name>- 使用 tidb-ctl 调整
bash
# 查看 TiDB 日志级别
tiup tidb-ctl log --host <tidb-host> --port <tidb-port> --level
# 设置 TiDB 日志级别
tiup tidb-ctl log --host <tidb-host> --port <tidb-port> --set-level debug
# 设置特定模块的日志级别
tiup tidb-ctl log --host <tidb-host> --port <tidb-port> --set-level debug --module executor- 使用 pd-ctl 调整
bash
# 进入 pd-ctl 交互模式
tiup pd-ctl -u http://<pd-host>:2379
# 查看当前日志级别
pd-ctl> log level
# 设置日志级别
pd-ctl> log set level debug
# 设置特定模块的日志级别
pd-ctl> log set level debug raft不同组件的日志级别调整
TiDB Server 日志级别调整
动态调整
bash
# 方法 1: 使用 HTTP API
curl -X POST -d "debug" http://<tidb-host>:10080/debug/v1/log/level
# 方法 2: 使用 tidb-ctl
tiup tidb-ctl log --host <tidb-host> --port <tidb-port> --set-level debug
# 方法 3: 使用 SQL 语句
mysql -h <tidb-host> -P <tidb-port> -u root -e "SET GLOBAL tidb_log_level = 'debug';"静态调整
toml
# 修改 tidb.toml 配置文件
[log]
level = "info"PD Server 日志级别调整
动态调整
bash
# 方法 1: 使用 HTTP API
curl -X POST -d '{"level": "debug"}' http://<pd-host>:2379/pd/api/v1/log/level
# 方法 2: 使用 pd-ctl
pd-ctl> log set level debug静态调整
toml
# 修改 pd.toml 配置文件
[log]
level = "info"TiKV Server 日志级别调整
动态调整
bash
# 方法 1: 使用 tikv-ctl
tiup tikv-ctl --host <tikv-host>:20160 log --set-level debug
# 方法 2: 设置特定模块的日志级别
tiup tikv-ctl --host <tikv-host>:20160 log --set-level debug --module raft静态调整
toml
# 修改 tikv.toml 配置文件
[log]
level = "info"TiFlash Server 日志级别调整
动态调整
bash
# 使用 HTTP API
curl -X POST -d '{"level": "debug"}' http://<tiflash-host>:3930/log/level
# 设置特定模块的日志级别
curl -X POST -d '{"level": "debug", "module": "raft"}' http://<tiflash-host>:3930/log/level静态调整
toml
# 修改 tiflash.toml 配置文件
[logger]
level = "info"TiCDC Server 日志级别调整
动态调整
bash
# 使用 HTTP API
curl -X POST -d '{"level": "debug"}' http://<cdc-host>:8300/api/v1/log/level
# 使用 cdc cli
tiup cdc cli log level set --server=http://<cdc-host>:8300 --level=debug静态调整
toml
# 修改 cdc.toml 配置文件
[log]
level = "info"
file = "/tidb-deploy/cdc-8300/logs/cdc.log"日志级别调整最佳实践
不同场景下的日志级别选择
| 场景 | 推荐日志级别 | 原因 |
|---|---|---|
| 正常运行 | info | 平衡日志详细程度和性能开销 |
| 开发调试 | debug 或 trace | 获取详细的调试信息 |
| 性能优化 | warn | 减少日志开销,只关注警告和错误 |
| 问题定位 | debug | 获取足够的调试信息,帮助定位问题 |
| 生产环境 | info 或 warn | 避免过多日志影响性能 |
日志级别调整的注意事项
性能影响
- 低级别日志(trace, debug)会产生大量日志,影响系统性能
- 生产环境建议使用 info 或 warn 级别
- 调试完成后及时恢复到正常日志级别
磁盘空间
- 低级别日志会快速占用磁盘空间
- 确保日志轮转配置合理
- 监控磁盘空间使用情况
模块级别的日志调整
- 对于复杂问题,可以只调整相关模块的日志级别
- 避免全局调整为低级别日志
- 例如:只调整 executor 模块的日志级别为 debug
日志格式
- 开发环境可以使用 text 格式,便于阅读
- 生产环境建议使用 json 格式,便于日志分析工具处理
日志级别调整流程
- 问题识别:确定需要调整日志级别的原因
- 选择调整方式:根据情况选择静态或动态调整
- 执行调整:执行日志级别调整操作
- 验证调整:验证日志级别是否生效
- 问题定位:利用日志定位问题
- 恢复正常:问题解决后恢复到正常日志级别
- 记录操作:记录日志级别调整的时间、原因和结果
日志管理最佳实践
日志轮转配置
- TiDB 日志轮转配置toml
[log.rotate] # 日志文件大小,超过则轮转
size = "300MB"
日志保留天数
days = 7
最大保留日志文件数
max-days = 0
2. **PD 日志轮转配置**
```toml
[log]
# PD 自动处理日志轮转,无需额外配置- TiKV 日志轮转配置toml
[log] # TiKV 自动处理日志轮转,无需额外配置
日志收集与分析
使用 Filebeat 收集日志
yaml# filebeat.yml filebeat.inputs: - type: log enabled: true paths: - /tidb-deploy/*/logs/*.log fields: cluster: <cluster-name> fields_under_root: true output.elasticsearch: hosts: ["<elasticsearch-host>:9200"] index: "tidb-logs-%{+yyyy.MM.dd}"使用 Fluentd 收集日志
txt# fluentd.conf <source> @type tail path /tidb-deploy/*/logs/*.log pos_file /var/log/fluentd/tidb.pos tag tidb.logs <parse> @type regexp expression /^(?<log_time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{6})\s+(?<level>[A-Z]+)\s+(?<message>.+)$/ </parse> </source> <match tidb.logs> @type elasticsearch host <elasticsearch-host> port 9200 index tidb-logs-%Y.%m.%d type _doc </match>使用 Loki 收集日志
yaml# promtail-config.yml server: http_listen_port: 9080 grpc_listen_port: 0 positions: filename: /tmp/positions.yaml clients: - url: http://<loki-host>:3100/loki/api/v1/push scrape_configs: - job_name: tidb-logs static_configs: - targets: - localhost labels: job: tidb-logs cluster: <cluster-name> __path__: /tidb-deploy/*/logs/*.log
日志级别监控与告警
监控日志相关指标
TiDB 日志指标
tidb_log_messages_total:日志消息总数,按级别和模块分类tidb_log_dropped_total:丢弃的日志总数
PD 日志指标
pd_log_messages_total:PD 日志消息总数
TiKV 日志指标
tikv_log_messages_total:TiKV 日志消息总数
配置日志告警
配置 Prometheus 告警规则
yamlgroups: - name: log-alerts rules: - alert: TooManyErrorLogs expr: sum(rate(tidb_log_messages_total{level="error"}[5m])) by (instance) > 10 for: 1m labels: severity: warning annotations: summary: "Too many error logs on {{ $labels.instance }}" description: "{{ $labels.instance }} has generated {{ $value }} error logs in the last 5 minutes"监控磁盘空间
yaml- alert: LogDiskSpaceLow expr: (node_filesystem_size_bytes{mountpoint="/tidb-deploy"} - node_filesystem_free_bytes{mountpoint="/tidb-deploy"}) / node_filesystem_size_bytes{mountpoint="/tidb-deploy"} * 100 > 80 for: 5m labels: severity: warning annotations: summary: "Log disk space low on {{ $labels.instance }}" description: "Disk usage on {{ $labels.instance }} is {{ $value }}%"
常见问题(FAQ)
Q1: 动态调整日志级别不生效怎么办?
A1: 请检查以下内容:
- 确认组件是否支持动态调整日志级别
- 检查 HTTP API 地址和端口是否正确
- 检查组件是否正在运行
- 查看组件日志,确认是否有相关错误信息
- 尝试重启组件后再进行动态调整
Q2: 如何查看当前日志级别?
A2: 可以使用以下方法:
- 查看组件日志的第一行,通常会显示日志级别
- 使用 HTTP API 查看当前日志级别
- 使用命令行工具查看,如 tidb-ctl, pd-ctl, tikv-ctl
Q3: 日志级别调整会影响系统性能吗?
A3: 是的,日志级别会影响系统性能:
- 低级别日志(trace, debug)会产生大量日志,增加 CPU 和磁盘 I/O 开销
- 高级别日志(info, warn, error)性能影响较小
- 生产环境建议使用 info 或 warn 级别
Q4: 如何批量调整所有组件的日志级别?
A4: 可以使用以下方法:
- 使用 tiup cluster edit-config 命令修改配置文件,然后执行滚动重启
- 编写脚本批量调用 HTTP API 或命令行工具
- 使用配置管理工具(如 Ansible)批量修改配置文件
Q5: 如何调整特定模块的日志级别?
A5: 可以使用以下方法:
- TiDB:使用 HTTP API
http://tidb-host:10080/debug/v1/log/level?module=module - PD:使用 pd-ctl
log set level level module - TiKV:使用 tikv-ctl
log --set-level level --module module - TiFlash:使用 HTTP API
http://tiflash-host:3930/log/level?module=module
Q6: 日志文件太大,如何处理?
A6: 可以采取以下措施:
- 调整日志级别为更高的级别(如 info 或 warn)
- 调整日志轮转配置,减小单个日志文件大小
- 缩短日志保留天数
- 增加磁盘空间或清理旧日志
- 使用日志压缩功能
Q7: 如何将日志导出为 JSON 格式?
A7: 可以在配置文件中设置日志格式:
toml
[log]
format = "json"Q8: 如何监控日志级别变化?
A8: 可以使用以下方法:
- 监控日志级别相关的指标
- 配置日志级别变化的告警
- 定期检查日志级别配置
- 记录所有日志级别调整操作
