概述
Logger指令常用于把資訊送到系統日志檔案或遠端日志審計系統,本期文章結合實際場景向各位小夥伴總結分享它的使用方法。
Tips:
測試作業系統:CentOS 8;
Logger軟體版本:
[root@localhost ~]# logger -V
logger from util-linux 2.32.1
[root@localhost ~]#
Logger指令使用場景及方法
場景1:把test2023送到系統日志
[root@localhost ~]# logger test2023
預設情況下,使用logger指令把資訊輸入到/var/log/messages日志檔案中,如下所示;
[root@localhost ~]# more /var/log/messages | grep test2023
Jun 20 15:25:24 localhost root[1164078]: test2023
[root@localhost ~]#
場景2:把test2023送到遠端日志審計系統
[root@localhost ~]# logger -n 192.168.1.10 test2023
檢視日志審計系統中收到的日志資訊,如下圖所示:
釋義1:logger指令預設連接配接日志審計系統的UDP 514端口
釋義2:預設采用的日志協定是RFC 5424(The RFC 5424 protocol has been the default for logger since version 2.26)
釋義3:資訊預設級别,即對應的level或Priority是:notice;
釋義4:資訊預設實體,即對應的Facility是:user
場景3:以RFC 3164協定方式,把test2023送到遠端日志審計系統
[root@localhost ~]# logger -n 192.168.1.10 --rfc3164 test2023
檢視日志審計系統中收到的日志資訊,如下圖所示:
場景4:以RFC 3164協定并指定資訊的級别,把test2023送到遠端日志審計系統中
[root@localhost ~]# logger -n 192.168.1.10 --rfc3164 -p 0 test2023
[root@localhost ~]# logger -n 192.168.1.10 --rfc3164 -p 1 test2023
[root@localhost ~]# logger -n 192.168.1.10 --rfc3164 -p 2 test2023
[root@localhost ~]# logger -n 192.168.1.10 --rfc3164 -p 3 test2023
[root@localhost ~]# logger -n 192.168.1.10 --rfc3164 -p 4 test2023
[root@localhost ~]# logger -n 192.168.1.10 --rfc3164 -p 5 test2023
[root@localhost ~]# logger -n 192.168.1.10 --rfc3164 -p 6 test2023
[root@localhost ~]# logger -n 192.168.1.10 --rfc3164 -p 7 test2023
[root@localhost ~]#
檢視日志審計系統中收到的日志資訊,如下圖所示:
場景5:以RFC 3164協定方式并設定資訊的實體、級别和标簽,把Test2023_Facility_level_tag送到遠端日志審計系統
[root@localhost ~]# logger -n 192.168.31.95 --rfc316 -t Test_TCP -p local5.2 Test2023_Facility_level_tag
場景6:以RFC 3164協定方式,把file_log檔案中的内容送到遠端日志審計系統
[root@localhost ~]# cat file_log
[root@localhost ~]# logger -f file_log -n 192.168.1.10 --rfc3164
#通過“- e”過濾掉檔案中的空行
[root@localhost ~]# logger -e -f file_log -n 192.168.1.10 --rfc3164
總結
結合具體的使用場景詳細說明了logger指令的使用方法,其它用法可通過指令man logger檢視。以上分享,希望各位小夥伴有所收獲,歡迎各位點贊收藏和指正。