【syslogd守护进程】
由于守护进程没有控制终端进行信息的输出,而有些情况还需要根据进程提供的信息进行系统管理和维护工作。因此Linux提供了syslogd守护进程,专门用于接受其他守护进程提供的信息记录在指定位置来解决日志记录的问题。
1.syslogd守护进程
负责记录、发送系统或者工具产生的信息,根据配置文件信息对消息的去向做出处理(记录到日志、输出到控制台、转发给用户、转发到网络上其他主机的syslogd进程处理)。
2.syslogd守护进程配置文件
配置文件/etc/syslog.conf内容如下:
cd /etc
ls -l syslog.conf
-rw-r--r-- 1 root root 1626 2008-08-30 08:40 syslog.conf
cat syslog.conf
# /etc/syslog.conf Configuration file for syslogd.
#
# For more information see syslog.conf(5)
# manpage.
#
# First some standard logfiles. Log by facility.
#
auth,authpriv.* /var/log/auth.log
*.*;auth,authpriv.none -/var/log/syslog
#cron.* /var/log/cron.log
daemon.* -/var/log/daemon.log
kern.* -/var/log/kern.log
lpr.* -/var/log/lpr.log
mail.* -/var/log/mail.log
user.* -/var/log/user.log //下面示例程序log信息追加的文件
#
# Logging for the mail system. Split it up so that
# it is easy to write scripts to parse these files.
#
mail.info -/var/log/mail.info
mail.warning -/var/log/mail.warn
mail.err /var/log/mail.err
# Logging for INN news system
#
news.crit /var/log/news/news.crit
news.err /var/log/news/news.err
news.notice -/var/log/news/news.notice
#
# Some `catch-all' logfiles.
#
*.=debug;\
auth,authpriv.none;\
news.none;mail.none -/var/log/debug
*.=info;*.=notice;*.=warning;\
auth,authpriv.none;\
cron,daemon.none;\
mail,news.none -/var/log/messages
#
# Emergencies are sent to everybody logged in.
#
*.emerg *
#
# I like to have messages displayed on the console, but only on a virtual
# console I usually leave idle.
#
#daemon,mail.*;\
# news.=crit;news.=err;news.=notice;\
# *.=debug;*.=info;\
# *.=notice;*.=warning /dev/tty8
# The named pipe /dev/xconsole is for the `xconsole' utility. To use it,
# you must invoke `xconsole' with the `-file' option:
#
# $ xconsole -file /dev/xconsole [...]
#
# NOTE: adjust the list below, or you'll go crazy if you have a reasonably
# busy site..
#
daemon.*;mail.*;\
news.err;\
*.=debug;*.=info;\
*.=notice;*.=warning |/dev/xconsole
说明(具体参数取值略):
以user.* -/var/log/user.log为例:
(1)消息类型:格式为facility.level,user.*表示facility为user(用户日志信息,对应函数参数为LOG_USER),level为*表示所有优先级的消息
(2)处理方案:将日志信息记录在-/var/log/user.log文件中
【守护进程日志实现】
主要涉及三个函数:
1)void openlog(const char* ident, int option, int facility);
打开系统日志链接,必须步骤。
ident:信息来源,哪个可执行程序
option:控制标志的参数,多个可以同时使用,用|操作合并
facility:指定消息类型,与配置文件对应,日志会写入配置文件指定位置。
2)void syslog(int priority,const char* format,....);
priority:消息级别,与openlog中的facility可以共同决定日志写入位置。
format:消息格式,再后面为格式对应的参数,类似printf的使用
3)void closelog();
关闭系统日志链接,可选步骤。
【代码】
工作流程:
|-------------------------------------------|
|openlog()------>syslog()------>colselog() |
|-------------------------------------------|
#include
#include
int main(int argc,char* argv[])
{
int i=0;
openlog(argv[0],LOG_CONS|LOG_PID,LOG_USER);
while(i<=5)
{
syslog(LOG_INFO,"%d log info test....",i); //增加6条log信息
i++;
}
closelog();
return 0;
}
【执行结果】
gcc -o syslog.o syslog.c
./syslog.o
cd /var/log
cat user.log //查看对应日志文件增加内容
Mar 10 23:55:45 gaolu-desktop Cleanup, done. Exitting...
Mar 11 21:40:50 gaolu-desktop pulseaudio[5136]: ltdl-bind-now.c: Failed to find original dlopen loader.
Mar 11 21:40:50 gaolu-desktop pulseaudio[5139]: alsa-util.c: Device front:0 doesn't support 44100 Hz, changed to 44099 Hz.
Mar 11 21:41:38 gaolu-desktop pulseaudio[5139]: module-x11-xsmp.c: X11 session manager not running.
Mar 11 21:41:38 gaolu-desktop pulseaudio[5139]: module.c: Failed to load module "module-x11-xsmp" (argument: ""): initialization failed.
...............................//略去中间内容
Mar 15 18:57:30 gaolu-desktop pulseaudio[5125]: alsa-util.c: Device front:0 doesn't support 44100 Hz, changed to 44099 Hz.
Mar 15 18:57:59 gaolu-desktop pulseaudio[5125]: module-x11-xsmp.c: X11 session manager not running.
Mar 15 18:57:59 gaolu-desktop pulseaudio[5125]: module.c: Failed to load module "module-x11-xsmp" (argument: ""): initialization failed.
Mar 15 19:00:08 gaolu-desktop python: io/hpmud/pp.c 627: unable to read device-id ret=-1
Mar 15 19:00:12 gaolu-desktop hp: io/hpmud/pp.c 627: unable to read device-id ret=-1
Mar 15 20:53:41 gaolu-desktop ./syslog.o[7429]: 0 log info test....
Mar 15 20:53:41 gaolu-desktop ./syslog.o[7429]: 1 log info test....
Mar 15 20:53:41 gaolu-desktop ./syslog.o[7429]: 2 log info test....
Mar 15 20:53:41 gaolu-desktop ./syslog.o[7429]: 3 log info test....
Mar 15 20:53:41 gaolu-desktop ./syslog.o[7429]: 4 log info test....
Mar 15 20:53:41 gaolu-desktop ./syslog.o[7429]: 5 log info test....
阅读(3335) | 评论(1) | 转发(0) |