journal日志
目标
查看和解读系统日志中的条目,以对问题进行故障排除或查看系统状态。
名词定义
- 查找事件
systemd-journald服务将日志数据存储在带有索引的结构化二进制文件中,该文件称为日志。此数据包含与日志事件相关的额外信息。
- 重要
默认情况下/run/log目录用于存储系统日志。/run/log的内容在系统重启后将被清除。您可以更改此设置,具体操作方式将在后续部分讨论。
要从日志中检索日志消息,请使用journalctl命令。可以使用此命令来查看日志中的所有消息,或根据各种选项和标准来搜索特定事件。如果以root身份运行该命令,则对日志具有完全访问权限。普通用户也可以使用此命令,但可能会被限制查看某些消息。
实操演示
命令:journalctl
语法:journalctl 选项 参数
-p 按照日志类型进行筛选
_PID=按照进程的PID进行查询
_COMM=根据命令进行查询
–since--until根据设定时间段进行匹配
##按照类型查看日志内容
[root@jenkins ~]# journalctl -p
alert crit debug emerg err info notice warning
[root@jenkins ~]# journalctl -p err
-- Logs begin at 四 2021-12-02 17:32:31 CST, end at 四 2022-02-17 13:11:41 CS
12月 02 17:32:32 sinfotek kernel: sd 0:0:0:0: [sda] Assuming drive cache: wri
12月 02 17:32:48 sinfotek kernel: piix4_smbus 0000:00:07.3: SMBus Host Contro
12月 03 09:38:56 jenkins sshd[11153]: pam_systemd(sshd:session): Failed to re
12月 03 09:42:33 jenkins systemd[1]: Failed to start LSB: Jenkins Automation
12月 03 09:43:55 jenkins systemd[1]: Failed to start LSB: Jenkins Automation
12月 03 09:45:06 jenkins systemd[1]: Failed to start LSB: Jenkins Automation
12月 03 09:49:46 jenkins systemd[1]: Failed to start LSB: Jenkins Automation
12月 03 14:18:36 jenkins sshd[29009]: pam_systemd(sshd:session): Failed to re
12月 03 15:11:54 jenkins systemd[1]: Failed to start LSB: Jenkins Automation
12月 03 17:22:09 jenkins systemd[1]: Failed to start LSB: Jenkins Automation
12月 03 17:28:27 jenkins systemd[1]: Failed to start LSB: Jenkins Automation
12月 15 11:07:24 jenkins systemd[1]: Failed to start The NGINX HTTP and rever
1月 20 15:39:49 jenkins systemd[1]: Failed to start The NGINX HTTP and revers
1月 26 15:55:34 jenkins sshd[97005]: error: Received disconnect from 192.168.
1月 26 15:56:02 jenkins sshd[97470]: error: Received disconnect from 192.168.
1月 26 15:56:31 jenkins sshd[97986]: error: Received disconnect from 192.168.
2月 07 09:11:21 jenkins sshd[111366]: pam_systemd(sshd:session): Failed to re
2月 07 11:54:38 jenkins sudo[45366]: sinfotek : command not allowed ; TTY=pts
2月 07 11:58:28 jenkins sudo[49108]: sinfotek : command not allowed ; TTY=pts
2月 09 15:47:38 jenkins sshd[36014]: pam_systemd(sshd:session): Failed to rel
2月 09 17:03:11 jenkins abrtd[926]: Got signal 15, exiting
2月 09 17:03:15 jenkins postfix/postfix-script[31222]: fatal: the Postfix mai
...skipping...
##按照进程PID进行查找
[root@jenkins ~]# journalctl _PID=33027
-- Logs begin at 四 2021-12-02 17:32:31 CST, end at 四 2022-02-17 13:11:41 CS
2月 09 17:05:04 jenkins sshd[33027]: Server listening on 0.0.0.0 port 22.
2月 09 17:05:04 jenkins sshd[33027]: Server listening on :: port 22.
##按照命令进行查找
[root@jenkins ~]# journalctl _COMM=php-fpm
-- Logs begin at 四 2021-12-02 17:32:31 CST, end at 四 2022-02-17 13:11:41 CS
12月 08 13:15:40 jenkins php-fpm[77803]: Starting php-fpm [08-Dec-2021 13:15:
12月 08 13:25:20 jenkins php-fpm[78687]: Gracefully shutting down php-fpm . d
12月 08 13:25:20 jenkins php-fpm[78692]: Starting php-fpm [08-Dec-2021 13:25:
12月 08 13:25:20 jenkins php-fpm[78692]: [08-Dec-2021 13:25:20] NOTICE: PHP m
12月 08 13:25:20 jenkins php-fpm[78692]: [08-Dec-2021 13:25:20] NOTICE: PHP m
12月 08 13:25:20 jenkins php-fpm[78692]: [08-Dec-2021 13:25:20] NOTICE: PHP m
12月 08 13:25:20 jenkins php-fpm[78692]: [08-Dec-2021 13:25:20] NOTICE: PHP m
12月 08 13:25:20 jenkins php-fpm[78692]: [08-Dec-2021 13:25:20] NOTICE: PHP m
12月 08 13:25:20 jenkins php-fpm[78692]: [08-Dec-2021 13:25:20] NOTICE: PHP m
12月 08 13:25:20 jenkins php-fpm[78692]: [08-Dec-2021 13:25:20] NOTICE: PHP m
12月 08 13:25:20 jenkins php-fpm[78692]: [08-Dec-2021 13:25:20] NOTICE: PHP m
12月 08 13:25:20 jenkins php-fpm[78692]: [08-Dec-2021 13:25:20] NOTICE: PHP m
12月 08 13:25:20 jenkins php-fpm[78692]: done
12月 08 13:40:08 jenkins php-fpm[79939]: Gracefully shutting down php-fpm . d
12月 08 13:58:44 jenkins php-fpm[59933]: Starting php-fpm [08-Dec-2021 13:58:
12月 08 13:58:44 jenkins php-fpm[59933]: [08-Dec-2021 13:58:44] NOTICE: PHP m
12月 08 13:58:44 jenkins php-fpm[59933]: [08-Dec-2021 13:58:44] NOTICE: PHP m
12月 08 13:58:44 jenkins php-fpm[59933]: [08-Dec-2021 13:58:44] NOTICE: PHP m
12月 08 13:58:44 jenkins php-fpm[59933]: [08-Dec-2021 13:58:44] NOTICE: PHP m
12月 08 13:58:44 jenkins php-fpm[59933]: [08-Dec-2021 13:58:44] NOTICE: PHP m
12月 08 13:58:44 jenkins php-fpm[59933]: [08-Dec-2021 13:58:44] NOTICE: PHP m
12月 08 13:58:44 jenkins php-fpm[59933]: [08-Dec-2021 13:58:44] NOTICE: PHP m
12月 08 13:58:44 jenkins php-fpm[59933]: [08-Dec-2021 13:58:44] NOTICE: PHP m
12月 08 13:58:44 jenkins php-fpm[59933]: [08-Dec-2021 13:58:44] NOTICE: PHP m
12月 08 13:58:44 jenkins php-fpm[59933]: [08-Dec-2021 13:58:44] NOTICE: PHP m
12月 08 13:58:44 jenkins php-fpm[59933]: done
12月 08 17:38:40 jenkins php-fpm[84767]: Gracefully shutting down php-fpm . d
12月 08 17:38:40 jenkins php-fpm[84772]: Starting php-fpm [08-Dec-2021 17:38:
12月 08 17:38:40 jenkins php-fpm[84772]: [08-Dec-2021 17:38:40] NOTICE: PHP m
##指定开始时间段
[root@jenkins ~]# journalctl --since "2021-12-02 17:30:31" --until "2021-12-02 17:32:31"
-- Logs begin at 四 2021-12-02 17:32:31 CST, end at 四 2022-02-17 13:11:41 CST. --- 设置永久保存日志
需要修改配置来实现,配置文件路径:/etc/systemd/journald.conf
当配置生效后会默认在/var/log/日志下创建journal文件
[root@jenkins ~]# vim /etc/systemd/journald.conf
[Journal]
Storage=persistent
##重启后生效自动创建journald
[root@jenkins log]# systemctl restart systemd-journald.service
[root@jenkins log]# ls /var/log/journal -dl
drwxr-xr-x 3 root root 46 2月 17 13:47 /var/log/journal
文档更新时间: 2022-02-17 13:54 作者:xiubao yan