linux】利用logger,logrotate處理腳本日誌

更多的時候我處理我們的程序(腳本)的日誌是通過重定向的方法去實現,而日誌的大小和切割有的時候就會忽略掉(哦,也許你加條find命令檢測一下大小或者超過一定時間執行刪除或者切割操作),何必自己再去寫這樣的程序呢,我們的系統就自帶這樣的程序;
logger
logger 是一個shell 命令接口,可以通過該接口使用Syslog的系統日誌模塊,還可以從命令行直接向系統日誌文件(或者自定義的文件)寫入一行信息。
下面就是logger的man信息
用法

logger [-isd] [-f file] [-p pri] [-t tag] [-u socket] [message …]

描述

Logger 用於往系統中寫入日誌. 它提供了一個shell命令接口到syslog系統模塊

選項:

-i 逐行記錄每一次logger的進程ID.

-s 記錄消息到標準錯誤, 包括系統日誌.

-f file 記錄特定的文件.

-p pri 輸入消息的特定優先級. 優先級可以是自定義的數值或者諸如 “facility.level” 的格式. 舉例: “-p local3.info” local3 facility這個設備的消息級別爲info. 默認是 “user.notice.”

-t tag 打上特定的標誌.

-u sock 以特定的socket代替內嵌系統常規工作

-d 使用一個數據進程代替一個流連接到這個socket.

— 結束參數列表. 這個允許消息以一個“-”開始

message 寫入log文件的內容消息,可以與-f配合使用

logger 以0退出表示成功, 大於0表示失敗.

可用的facility名有: auth, authpriv (for security information of a
sensitive nature), cron, daemon, ftp, kern, lpr, mail, news, security
(deprecated synonym for auth), syslog, user, uucp, and local0 to local7,
inclusive.

可用的level名用: alert, crit, debug, emerg, err, error (deprecated
synonym for err), info, notice, panic (deprecated synonym for emerg),
warning, warn (deprecated synonym for warning).

舉例:

logger System rebooted

logger -p local0.notice -t HOSTIDM -f /dev/idmc

舉個例子好了,如何在我們的腳本中利用logger命令:
[root@qing ~]# cat my_test.sh
#!/bin/bash
#some other scripts goes here …
logger -i -t my_test.sh -p local3.notice ” my_test.sh find some error in …”
echo “Hello world”
[root@qing ~]# vim /etc/rsyslog.conf #在Centos6之前的版本這個文件對應的應該是/etc/syslog.conf,對應的進程爲syslogd
修改這一行:增加local3.none,表示local3上的任何消息都不記錄到/var/log/messages 中
*.info;mail.none;authpriv.none;cron.none;local3.none /var/log/messages
增加這一行:意思是來自local3的所有消息都記錄到/var/log/my_test.log ,前面加個“-”,說明要啓用寫入緩衝
local3.* -/var/log/my_test.log

[root@qing ~]# service rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
[root@qing ~]# cat my_test.sh
#/bin/bash
#some other scripts goes here …
logger -i -t my_test.sh -p local3.notice ” my_test.sh find some error in …”
echo “Hello world”
[root@qing ~]# bash my_test.sh
Hello world
[root@qing ~]# cat /var/log/my_test.log
Aug 14 23:15:19 localhost my_test.sh[31996]: my_test.sh find some error in …
Aug 14 23:32:41 localhost my_test.sh[32096]: my_test.sh find some error in …

哈哈,看看怎麼樣,有了吧。。
那要是我日誌變的越來越大,怎麼辦,我們的系統日誌是怎麼變成messages.0,messages.1….的呢,這就是我們接下來所說的logrotate
這個東東可是管理着我們系統的大大小小的日誌的切割與輪替哦。。。
[root@qing ~]# cat /etc/logrotate.conf
# 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

# no packages own wtmp and btmp — we’ll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}

/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}

具體的意思大家可以網上搜下,我們在/etc/logrotate.d/下創建我們自己的一個配置文件:
[root@qing ~]# vim /etc//logrotate.d/my_test
/var/log/my_test.log #日誌處理的目標
{
nocompress #不採用壓縮
daily #每天輪替
copytruncate
ifempty #文件爲空則不切割
olddir /root/chenqing/log #切割後的文件所保存的目錄
rotate 4 #保存最近切割的4份文件
}
最好重啓下syslog。。。。

這樣就可以像我們的系統日誌一樣定時的進行切割了,再也不用關心我們所輸出的日誌了,這裏面還有其它的更詳細的設置,大家不妨自己好好研究下了。。。


轉自 : 鄉村運維

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章