輕鬆搞定LinuxTlog

來源:互聯網
上載者:User

[導讀
]現在很多人把Linux 配置成了一個開發工具,而用 Windows 來娛樂。你是Linux的使用者嗎?本文為你講解LinuxTlog方法,希望你能掌握LinuxTlog。

  現在很多人把Linux 配置成了一個開發工具,而用 Windows
來娛樂。你是Linux的使用者嗎?本文為你講解LinuxTlog方法,希望你能掌握LinuxTlog。每個使用UNIX/Linux的人都知道日誌
的用處,那你是否清楚Linux這些日誌資訊處理的來龍去脈呢?

  我們可以看到Linux系統資訊日誌的途徑基本有以下2種:

  (1)dmesg查看----這個命令比較常見

  (2)/var/log/下的檔案

  那下面我們就從這個2個途徑著手,一步步的走下去.

  (一)首先,我們來看dmesg這個常見的命令背後隱藏的是什麼!!

  (1)先讓我們來MAN一下這個傢伙

  -------------man dmesg--------------------------

 

 
  1. NAME   
  2. dmesg - print or control the kernel ring buffer   
  3. SYNOPSIS   
  4. dmesg [ -c ] [ -n level ] [ -s bufsize ]   
  5. DESCRIPTION   
  6. dmesg is used to examine or control the kernel ring buffer.   
  7. The program helps users to print out their bootup mes- sages. Instead of copying the messages by hand, the user need only:   
  8. dmesg > boot.messages   
  9. and mail the boot.messages file to whoever can debug their   
  10. problem.   
  11. OPTIONS   
  12. -c Clear the ring buffer contents after printing.   
  13. -sbufsize   
  14. Use a buffer of size bufsize to query the kernel ring buffer. This is 16392 by default. (The default kernel syslog buffer size was 4096 at first, 8192 since 1.3.54, 16384 since 2.1.113.) If you have set the kernel buffer to be larger than the default then this option can be used to view the entire buffer.   
  15. -nlevel   
  16. Set the level at which logging of messages is done to the console. For example, -n 1 prevents all messages, expect panic messages, from appearing on the console. All levels of messages are still written to /proc/kmsg, so syslogd(8) can still be used to control exactly where kernel messages appear.   
  17. When the -n option is used, dmesg will not print or clear the kernel ring buffer.   
  18. When both options are used, only the last option on the command line will have an effect.  

  從Linux提供的手冊,我們可以得知一條最重要的資訊dmesg是從kernel 的ring buffer(環緩衝區)中讀取資訊的.

  (2)那什麼是ring buffer呢?

  在Linux中,所有的系統資訊(包核心資訊)都會傳送到ring
buffer中.而核心產生的資訊由printk()列印出來。系統啟動時所看到的資訊都是由該函數列印到螢幕中。 printk()打出的資訊往往以
<0><2>...這的數字表明訊息的重要層級。高於一定的優先順序別會列印到螢幕上, 否則只會保留在系統的緩衝區中(ring
buffer)。

  至於dmesg具體是如何從ring buffer中讀取的,大家可以看dmesg.c原始碼.很短,比較輕易讀懂.

  (二)dmesg怎麼搞的大家應該很明白了吧.至於/var/log/下的檔案更是大家熟悉得不能再熟悉了!

  (1)/var/log/..下為什麼有這麼多檔案呢?

  一句話解釋: 是syslogd這個守護進程根據/etc/syslog.conf,將不同的服務產生的Log記錄到不同的檔案中.

  這裡的/etc/syslog.conf我就不細說了,很多這方面的資訊(去查吧).

  (2)既然知道了,/var/log/..是由syslogd這個守護進程產生的.那就再順著這條線走下去.

  Linux系統啟動後,由/etc/init.d/sysklogd先後啟動klogd,syslogd兩個守護進程。

  其中klogd會通過syslog()系統調用或者讀取proc檔案系統來從系統緩衝區(ring buffer)中得到由核心printk()

  發出的資訊.而syslogd是通過klogd來讀取系統核心資訊.

  我想至此,大家心理應該對log產生,讀取等一系列的動作有所感覺.

  總結:

  (1)所有系統資訊是輸出到ring buffer中去的.dmesg所顯示的內容也是從ring buffer中讀取的.

  (2)Linux系統中/etc/init.d/sysklogd會啟動2個守護進程:Klogd&&Syslogd

  (3)klogd是負責讀取核心資訊的,有2種方式:

  syslog()系統調用(這個函數用法比較全,大家去MAN一下看看)

  直接的對/proc/kmsg進行讀取(再這提一下,/proc/kmsg是專門輸出核心資訊的地方)

  (4)Klogd的輸出結果會傳送給syslogd進行處理,syslogd會根據/etc/syslog.conf的配置把log

  資訊輸出到/var/log/下的不同檔案中。

  這樣你就能很好的完成LinuxTlog了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.