您的位置:首页 > 运维架构 > Linux

嵌入式 Linux进程间通信(四)——Linux系统日志

2017-03-26 12:43 405 查看

嵌入式 Linux进程间通信(四)——Linux系统日志

        syslog 是一种工业标准的协议,用来记录设备的日志。Linux日志系统由系统日志监控程序syslogd和内核日志监控程序klogd组成,两个监控程序都是守护程序(daemon),且都注册成了系统服务。syslogd专门记录非内核的其他设备所产生的日志,当系统的控制权由系统交给init的时候,日志信息的记录由syslogd负责记录。Klogd主要负责内核所产生的日志。内核日志记录信息由dmesg /var/log/dmesg查看。
常见linux系统的日志文件:/var/log/dmesg      内核引导信息日志/var/log/message    标准系统错误信息日志/var/log/maillog    邮件系统信息日志/var/log/cron       计划任务日志/var/log/secure     安全信息日志
    完整的syslog日志中包含产生日志的程序模块(Facility)、严重性(Severity或 Level)、时间、主机名或IP、进程名、进程ID和正文。
系统日志信息的格式:timestamp hostname ident[pid]:log message

一、syslog函数

Linux系统提供了一组系统日志的接口函数,如下:
#include <syslog.h>
 
void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);
    调用openlog、closelog是可选择的。如果不调用openlog,则在第一次调用syslog时,自动调用openlog。
参数解读如下:
ident:
指向信息的指针,一般为程序名,ident所表示的字符串将固定地加在每行日志的前面以标识这个日志,通常就写成当前程序的名称以作标记
option:
LOG_CONS       Write directly to system console if there is an error while sending to system  logger.
LOG_NDELAY     Open  the  connection  immediately  (normally, the connection is opened when the first message is logged).
 
LOG_NOWAIT     Don’t wait for child processes that may have been created while logging  the  message.(The  GNU  C  library does not create a child process, so this option has no effect on Linux.)
LOG_ODELAY     The converse of LOG_NDELAY; opening of the connection is  delayed  until  syslog()  is called.  (This is the default, and need not be specified.)
LOG_PERROR     (Not in POSIX.1-2001.)  Print to stderr as well.
LOG_PID        Include PID with each message.
 
Facility:
LOG_AUTH       security/authorization messages (DEPRECATED Use LOG_AUTHPRIV instead)
LOG_AUTHPRIV   security/authorization messages (private)
LOG_CRON       clock daemon (cron and at)
LOG_DAEMON     system daemons without separate facility value
LOG_FTP        ftp daemon
LOG_KERN       kernel messages (these can’t be generated from user processes)
LOG_LOCAL0 through LOG_LOCAL7 reserved for local use
LOG_LPR        line printer subsystem
LOG_MAIL       mail subsystem
LOG_NEWS       USENET news subsystem
LOG_SYSLOG     messages generated internally by syslogd(8)
LOG_USER (default) generic user-level messages
LOG_UUCP       UUCP subsystem
 
Level:
LOG_EMERG      system is unusable
LOG_ALERT      action must be taken immediately
LOG_CRIT       critical conditions
LOG_ERR        error conditions
LOG_WARNING    warning conditions
LOG_NOTICE     normal, but significant, condition
LOG_INFO       informational message
LOG_DEBUG      debug-level message
 
Priority:
(security level|facility code)

二、syslog配置文件

        syslog工具由一个守护程序组成,接受访问系统的日志信息并且根据 /etc/syslog.conf配置文件中的指令处理日志信息。不同的linux发行版本使用不同的日志工具,但都遵循相同的syslog协议。
    配置文件:/etc/rsyslog.conf(CentOS 6.7)
    配置文件中每行表示一个项目,格式为:facility.level    action
    由两个部分组成:
    第一部分:选择条件(可以有一个或者多个条件),分为两个字段。
    选择条件本身分为两个字段,之间用一个小数点(.)分隔。前一字段是一项服务,后一字段是一个优先级。选择条件是对消息类型的一种分类,这种分类便于人们把不同类型的消息发送到不同的地方。在同一个syslog配置行上允许出现一个以上的选择条件,但必须用分号(;)隔开。
    常见facility:
kern                内核信息;
user                用户进程信息;
mail                电子邮件相关信息;
daemon          后台进程相关信息;
authpriv            包括特权信息如用户名在内的认证活动;
cron                计划任务信息;
syslog          系统日志信息
lpr             打印服务相关信息。
news            新闻组服务器信息
uucp                uucp 生成的信息
local0----local7        本地用户信息
    优先级级是选择条件的第二个字段,它代表消息的紧急程度。
按严重程度由低到高排序:
debug       不包含函数条件或问题的其他信息
info            提供信息的消息
none        没有重要级,通常用于排错
notice      具有重要性的普通条件
warning     预警信息
err         阻止工具或某些子系统部分功能实现的错误条件
crit            阻止某些工具或子系统功能实现的错误条件
alert           需要立即被修改的条件
emerg       该系统不可用
     不同的服务类型有不同的优先级,数值较大的优先级涵盖数值较小的优先级。如果某个选择条件只给出了一个优先级而没有使用任何优先级限定符,对应于这个优先 级的消息以及所有更紧急的消息类型都将包括在内。比如说,如果某个选择条件里的优先级是“warning”,它实际上将把“warning”、 “err”、“crit”、“alert”和“emerg”都包括在内。
    第二部分:操作动作
    日志信息可以分别记录到多个文件里,还可以发送到命名管道、其他程序甚至另一台机器。
syslog 主要支持以下活动:
file                    指定文件的绝对路径
terminal 或 prin        完全的串行或并行设备标志符
@host(@IP地址)    远程的日志服务器
/etc/rsyslog.conf内容如下:
[code=bash;toolbar:false"># Log all kernel messages to the console.# Logging much else clutters up the screen.#kern.*                                                 /dev/console# Log anything (except mail) of level info or higher.# Don't log private authentication messages!*.info;mail.none;authpriv.none;cron.none                /var/log/messages# The authpriv file has restricted access.authpriv.*                                              /var/log/secure# Log all the mail messages in one place.mail.*                                                  -/var/log/maillog# Log cron stuffcron.*                                                  /var/log/cron# Everybody gets emergency messages*.emerg                                                 *# Save news errors of level crit and higher in a special file.uucp,news.crit                                          /var/log/spooler# Save boot messages also to boot.loglocal7.*     /var/log/boot.log
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: