Unix 系統(tǒng)有一個非常靈活和強大的日志系統(tǒng),它可以讓您記錄幾乎任何你能想象和操作日志,以獲取所需要的信息。
許多版本 UNIX 提供了一個通用的日志工具,稱為:syslog。個別程序需要有記錄的信息發(fā)送到 syslog 信息。
Unix的系統(tǒng)日志主機配置,統(tǒng)一的系統(tǒng)日志記錄工具。該系統(tǒng)采用一個集中的系統(tǒng)運行程序 /etc/syslogd 或 /etc/syslog 日志記錄進程。.
系統(tǒng)日志的操作是相當簡單的。程序日志條目發(fā)送到syslogd,參考的配置文件在/etc/syslogd.conf 或 /etc/syslog,找到一個匹配時,所需的日志文件寫入日志消息。
有四個基本的 syslog 條款,你應(yīng)該明白:
Term | 描述 |
---|---|
Facility | The identifier used to describe the application or process that submitted the log message. Examples are mail, kernel, and ftp. |
Priority | An indicator of the importance of the message. Levels are defined within syslog as guidelines, from debugging information to critical events. |
Selector | A combination of one or more facilities and levels. When an incoming event matches a selector, an action is performed. |
Action | What happens to an incoming message that matches a selector. Actions can write the message to a log file, echo the message to a console or other device, write the message to a logged in user, or send the message along to another syslog server. |
這里是可用的設(shè)備選擇。并非所有的設(shè)施都存在于所有版本的UNIX。
設(shè)備 | 描述 |
---|---|
auth | Activity related to requesting name and password (getty, su, login) |
authpriv | Same as auth but logged to a file that can only be read by selected users |
console | Used to capture messages that would generally be directed to the system console |
cron | Messages from the cron system scheduler |
daemon | System daemon catch-all |
ftp | Messages relating to the ftp daemon |
kern | Kernel messages |
local0.local7 | Local facilities defined per site |
lpr | Messages from the line printing system |
Messages relating to the mail system | |
mark | Pseudo event used to generate timestamps in log files |
news | Messages relating to network news protocol (nntp) |
ntp | Messages relating to network time protocol |
user | Regular user processes |
uucp | UUCP subsystem |
該系統(tǒng)記錄的優(yōu)先級總結(jié)在下面的表中:
優(yōu)先級 | 描述 |
---|---|
emerg | Emergency condition, such as an imminent system crash, usually broadcast to all users |
alert | Condition that should be corrected immediately, such as a corrupted system database |
crit | Critical condition, such as a hardware error |
err | Ordinary error |
warning | Warning |
notice | Condition that is not an error, but possibly should be handled in a special way |
info | Informational message |
debug | Messages that are used when debugging programs |
none | Pseudo level used to specify not to log messages. |
設(shè)備和級別的組合,讓你辨識記錄和信息。
由于每個程序盡職地發(fā)送它的消息的系統(tǒng)記錄器,記錄器作出決定什么來跟蹤和丟棄的基礎(chǔ)上在選擇器中定義的級別。
當你指定一個級別,系統(tǒng)將跟蹤所有在這一水平較高。
/etc/syslog.conf 文件控制,記錄消息的位置。一個典型的 syslog.conf 文件可能看起來像這樣:
*.err;kern.debug;auth.notice /dev/console daemon,auth.notice /var/log/messages lpr.info /var/log/lpr.log mail.* /var/log/mail.log ftp.* /var/log/ftp.log auth.* @prep.ai.mit.edu auth.* root,amrood netinfo.err /var/log/netinfo.log install.* /var/log/install.log *.emerg * *.alert |program_name mark.* /dev/console
文件的每一行包含兩個部分:
消息選擇器,指定哪種要記錄的消息。例如,所有的錯誤消息或內(nèi)核的所有調(diào)試信息。
應(yīng)該做些什么消息,說一個動作域。例如,把它放在一個文件或消息發(fā)送到用戶的終端上。
以下是對上述配置的顯著點:
消息選擇器有兩個部分組成:設(shè)備和優(yōu)先級。例如,kern.debug的選擇由內(nèi)核(設(shè)施)產(chǎn)生的所有調(diào)試消息(優(yōu)先級)。
消息選擇kern.debug的選擇所有優(yōu)先級大于調(diào)試。
設(shè)施或優(yōu)先的地方中的星號表示“所有”。例如,*. 調(diào)試是指所有調(diào)試信息,而 kern.* 指由內(nèi)核生成的所有消息。
您還可以使用逗號指定多個設(shè)備。兩個或多個選擇可以組合在一起使用分號。
action字段指定的五個動作之一:
日志消息發(fā)送到一個文件或設(shè)備。例如,/var/log/lpr.log 或 /dev/console.。
發(fā)送一條信息給用戶。您可以指定多個用戶名(如根,amrood)用逗號將它們分隔開。
發(fā)送一條信息給所有用戶。在這種情況下,“動作”字段中包含一個星號(例如,*)。
管道消息的程序。在這種情況下,程序被指定后,UNIX管道符號(|)。
將消息發(fā)送到另一臺主機上的系統(tǒng)日志。在這種情況下,行動領(lǐng)域包括主機名,前面有一個at符號(例如,@ yiibai.com)
UNIX 提供了命令logger ,這是一個非常有用的命令處理系統(tǒng)日志。 logger 命令記錄消息發(fā)送到syslogd守護進程,從而引發(fā)系統(tǒng)日志。
這意味著我們可以在命令行檢查隨時syslogd 守護進程,它的配置。 logger 命令提供系統(tǒng)日志文件,在命令行添加一行條目的方法。
該命令的格式是:
logger [-i] [-f file] [-p priority] [-t tag] [message]...
下面是詳細的參數(shù):
選項 | 描述 |
---|---|
-f filename | Use the contents of file filename as the message to log. |
-i | Log the process ID of the logger process with each line. |
-p priority | Enter the message with the specified priority (specified selector entry); the message priority can be specified numerically, or as a facility.priority pair. The default priority is user.notice. |
-t tag | Mark each line added to the log with the specified tag. |
message | The string arguments whose contents are concatenated together in the specified order, separated by the space |
您可以使用聯(lián)機幫助幫助檢查完成此命令的語法。
日志文件的增長傾向非???,消耗大量的磁盤空間。要啟用日志切換,大多數(shù)發(fā)行版使用 newsyslog 或 logrotate 工具。
這些工具應(yīng)該被稱為使用cron守護程序在頻繁的時間間隔。檢查newsyslog 或 logrotate的更多詳細信息的手冊頁。
所有的系統(tǒng)應(yīng)用程序創(chuàng)建日志文件在 /var/log 和其子目錄。這里有幾個重要的應(yīng)用程序及其日志目錄:
應(yīng)用程序 | 目錄 |
---|---|
httpd | /var/log/httpd |
samba | /var/log/samba |
cron | /var/log/ |
/var/log/ | |
mysql | /var/log/ |