温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何理解LINUX日志

发布时间:2021-10-22 11:03:43 来源:亿速云 阅读:147 作者:柒染 栏目:云计算

本篇文章给大家分享的是有关如何理解LINUX日志,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

每个使用UNIX/LINUX的人都知道日志的用处,那你是否清楚LINUX这些日志信息处理的来龙去脉呢~

本人通过近一个月来对源代码的死缠烂打,终于有所体会!

我们可以看到LINUX系统信息日志的途径基本有以下2种:

1>dmesg查看----这个命令比较常见

2>/var/log/下的文件

那下面我们就从这个2个途径着手,一步步的走下去.

<一>

首先,我们来看dmesg这个常见的命令背后隐藏的是什么!!

1> 先让我们来MAN一下这个家伙

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

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

NAME  
       dmesg - print or control the kernel ring buffer

SYNOPSIS  
       dmesg [ -c ] [ -n level ] [ -s bufsize ]

DESCRIPTION  
       dmesg  is  used  to  examine  or  control  the kernel ring  
       buffer.

       The program helps users to print  out  their  bootup  mes-  
       sages.   Instead of copying the messages by hand, the user  
       need only:  
              dmesg > boot.messages  
       and mail the boot.messages file to whoever can debug their  
       problem.

OPTIONS  
       -c     Clear the ring buffer contents after printing.

       -sbufsize  
              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.

       -nlevel  
              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.  When the -n option is used, dmesg will not  
              print or clear the kernel ring buffer.

              When both options are used, only the last option on  
              the command line will have an effect.  
 

从LINUX提供的手册,我们可以得知一条最重要的信息   dmesg是从kernel 的ring buffer(环缓冲区)中读取信息的.  

---------------------------------------------over man --------------------------------------------------

---------------------------------------------over man --------------------------------------------------

 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/下的不同文件中.  

redhat linux知识库

以上就是如何理解LINUX日志,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI