linux命令logger使用


先從別的地方抄過來全部的解釋,如下:

**options (選項):**
   -d, --udp  
       使用數據報(UDP)而不是使用默認的流連接(TCP)
   -i, --id   
       逐行記錄每一次logger的進程ID
   -f, --file file_name
       記錄特定的文件
   -h, --help 
       顯示幫助文本並退出
   -n, --server 
       寫入指定的遠程syslog服務器,使用UDP代替內裝式syslog的例程
   -s, --stderr
       輸出標准錯誤到系統日志。
   -t, --tag tag
       指定標記記錄
   -u, --socket socket
       寫入指定的socket,而不是到內置系統日志例程。
   -V, --version
        顯示版本信息並退出
   -P, --port port_num
       使用指定的UDP端口。默認的端口號是514
   -p, --priority priority_level
       指定輸入消息日志級別,優先級可以是數字或者指定為 " facility.level" 的格式。  

下面開始逐一實驗每個參數的用法。

-d不清楚怎么使用

-i,記錄進程id,隨便寫個小列子,代碼如下:

#!/bin/bash
logger -i "this is a test --i"
logger "this is a test --"
sleep 100

在/var/log/messages文件中,會有如下記錄:

2018-03-27T10:54:17.838653+08:00|notice|root[9433]|this is a test --i
2018-03-27T10:54:17.839606+08:00|notice|root[-]|this is a test --

-f選項沒發現什么特別之處。

-n太復雜,暫時還沒有用到。

-s,輸出標准錯誤,並且將信息打印到系統日志中。

linux-xdYUnA:/home/test/shell # logger -i -s "abc"
root[5105]: abc

在message日志中記錄如下:

2018-03-27T11:06:47.705041+08:00|notice|root[5105]|abc

-t,指定標記記錄,在message日志中可以看到是屬於哪個模塊記錄的日志,-t后面為模塊名稱,加引號或不加都可以。如果不指定-t,都是已root為記錄。

linux-xdYUnA:/home/test/shell # logger -i -t xingmuxin -s "this is a test"
xingmuxin[23054]: this is a test
linux-xdYUnA:/home/test/shell # logger -i -t "xingmuxin" -s "this is a test"
xingmuxin[25200]: this is a test
linux-xdYUnA:/home/test/shell # logger -i -s "this a test"
root[16997]: this a test

-u,-P,這個暫時還不知道怎么使用。

-p,指定輸入消息日志級別,優先級可以是數字或者指定為 " facility.level" 的格式。比如:" -p local3.info " local3 這個設備的消息級別為 info。默認級別是 "user.notice"

facility:是用來定義由誰產生的日志信息:那個軟件、子系統運行過程中產生的日志信息。
             auth:             用戶授權
             authpriv:         授權和安全
             cron:             計划任務
             daemon:           系統守護進程
             kern:             與內核有關的信息
             lpr                與打印服務有關的信息
             mail               與電子郵件有關的信息
             news               來自新聞服務器的信息
             syslog             由syslog生成的信息
             user               用戶的程序生成的信息,默認
             ftp
             uucp               由uucp生成的信息
             local0~7           用來定義本地策略

level:是用來定義記錄什么類型的日志信息。alert需要立即采取動作。

debug(7)            調試
info(6)             正常消息
notice(5)           正常但是要注意
warning(4)
error(3)            錯誤狀態
crit(2)             臨界狀態
alert(1)
emerg(0)            系統不可用

如下列子:

linux-xdYUnA:/home/test/shell # logger -it xingmuxin -p kern.err "this a test"
linux-xdYUnA:/home/test/shell # logger -it xingmuxin -p syslog.info "this a test"
linux-xdYUnA:/home/test/shell # logger -it xingmuxin -p 4 "this a test"

在message中記錄的日志為:

2018-03-27T11:27:37.318344+08:00|err|xingmuxin[7822]|this a test
2018-03-27T11:27:50.052378+08:00|info|xingmuxin[10501]|this a test
2018-03-27T11:29:26.995571+08:00|warning|xingmuxin[31063]|this a test

 


             

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM