Centos 7/8 日志管理

LogSystem

CentOS 7/8 使用systemd-journald来做日志中心库,使用rsyslog来做日志持久化,使用logrotate来做日志文件轮转。

 

systemd-Journald

systemd-journald 守护进程提供一种改进的日志管理服务,可以收集来自内核,启动过程的早期阶段,标准输出,系统日志,以及守护进程启动和运行期间的错误的消息。

它将这些消息写到一个结构化的事件日志中,默认情况下不在重新启动之间保留。

 

持久化systemd日志

默认情况下,systemd 的日志保存在 /run/log/journal 中,系统重启就会清除,这是RHEL7的新特性。通过新建 /var/log/journal 目录,日志会自动记录到这个目录中,并永久存储。

# 1. 修改journal存储模式
[root@study ~]# sed -i "/^#Storage/cStorage=persistent" /etc/systemd/journald.conf

# 2. 先处理所需要的目录与相关权限设置 ( 可选,服务重启后会自动创建该目录 )
[root@study ~]# mkdir /var/log/journal
[root@study ~]# chown root:systemd-journal /var/log/journal
[root@study ~]# chmod 2775 /var/log/journal


# 3. 重新启动 systemd-journald 并且观察备份的日志数据!
[root@study ~]# systemctl restart systemd-journald.service
[root@study ~]# ll /var/log/journal/
drwxr-sr-x. 2 root systemd-journal 27 Aug 20 02:37 309eb890d09f440681f596543d95ec

 

journalctl常用命令

# 以flow形式查看日志
$ journalctl -f

# 查看内核日志
$ journalctl -k

# 查看指定服务日志
$ journalctl -u docker.serivce

# 查看指定日期日志
$ journalctl --since="2018-09-21 10:21:00" -u docker
$ journalctl --since="2018-09-21 10:21:00" --until="2018-09-21 10:22:00" -u docker

# 查看指定级别日志
$ journalctl -p 3 -u docker.service
操作系统提供了从0 (emerg) 到 7 (debug) 一共7个级别的日志,7个级别的含义为:
    0: emerg
    1: alert
    2: crit
    3: err
    4: warning
    5: notice
    6: info
    7: debug
    
# 查看日志占用的磁盘空间
$ journalctl --disk-usage

# 设置日志占用的空间
$ journalctl --vacuum-size=500M

# 设置日志保存的时间
$ journalctl --vacuum-time=1month

# 检查日志文件一致性
$ journalctl –-verify

 

rsyslog

rsyslog 服务随后根据优先级排列日志信息,将它们写入到 /var/log目录中永久保存

rsyslog 配置文件 /etc/rsyslog.conf

*.info;mail.none;authpriv.none;cron.none                /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
mail.*                                                  /var/log/maillog

# Log cron stuff
cron.*                                                  /var/log/cron

# Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log

 

logrotate

logrotate ‐ rotates, compresses, and mails system logs. logrotate是Linux自带的日志切割工具,logrotate是基于cron运行的。

logrotate的配置文件是/etc/logrotate.conf,其定义了logrotate的玩法:每周,最多4周,文件名加时间后缀,默认不压缩。

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d/

# system-specific logs may be also be configured here.

/etc/logrotate.d/中定义了需要切割Service的文件,syslog也在其中。

# cat /etc/logrotate.d/syslog 
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler

logrotate是基于cron运行的,其脚本路径为 /etc/cron.daily/logrotate

$ cat /etc/cron.daily/logrotate 
#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit $EXITVALUE

logrotate基于CRON运行,所以执行时间由CRON控制

  • ubuntu查看/etc/crontab
  • centos查看/etc/anacrontab

没错,run-parts是运行一个目录中的所有脚本或程序

没错,logrotate脚本是/etc/cron.daily/目录下面的脚本,CRON每天05:01运行/etc/cron.daily/目录下面的所有脚本

 

最后看一下Nginx的logrotate

# cat /etc/logrotate.d/nginx 
/var/log/nginx/*log {
    create 0664 nginx root
    daily
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

 

posted @ 2020-03-08 11:22  Vincen_shen  阅读(3420)  评论(0编辑  收藏  举报