在 Linux 中配置 syslog 主要取决于你使用的发行版和日志服务。下面从常见场景出发,系统讲解如何配置 syslog。
常见日志服务有:
| 系统 | 默认日志服务 |
|---|---|
| CentOS 6 / RHEL 6 | rsyslog |
| CentOS 7+ / RHEL 7+ | rsyslog |
| Ubuntu / Debian | rsyslog(新版也可用 journald) |
| 老系统 | syslog-ng / syslogd |
查看当前服务:
ps -ef | grep syslog
或:
systemctl status rsyslog
/etc/rsyslog.conf
/etc/rsyslog.d/*.conf
facility.priority action
*.info;mail.none;authpriv.none /var/log/messages
authpriv.* /var/log/secure
| facility | 含义 |
|---|---|
| auth / authpriv | 认证 |
| cron | 定时任务 |
| daemon | 守护进程 |
| kern | 内核 |
| 邮件 | |
| user | 用户程序 |
| local0–local7 | 自定义 |
| priority | 含义 |
|---|---|
| debug | 调试 |
| info | 信息 |
| notice | 注意 |
| warning | 警告 |
| err | 错误 |
| crit | 严重 |
| alert | 警报 |
| emerg | 紧急 |
✅ 级别越高,越严重
*.* /var/log/all.log
*.err /var/log/error.log
local0.* /var/log/myapp.log
程序中使用:
logger -p local0.info "test message"
编辑 /etc/rsyslog.conf:
# 提供 UDP
module(load="imudp")
input(type="imudp" port="514")
# 提供 TCP
module(load="imtcp")
input(type="imtcp" port="514")
重启:
systemctl restart rsyslog
*.* @192.168.1.10 # UDP
*.* @@192.168.1.10 # TCP
配置文件:
/etc/logrotate.d/syslog
示例:
/var/log/messages {
daily
rotate 7
missingok
notifempty
compress
}
生效:
logrotate -f /etc/logrotate.conf
systemctl restart rsyslog
systemctl enable rsyslog
tail -f /var/log/messages
journalctl -u rsyslog
logger "test syslog"
查看日志:
journalctl
journalctl -u nginx
journalctl -f
你可以告诉我:
我可以给你完整可直接用的配置模板。