7.6. 日志信息
使用syslog()函数处理日志信息。
函数声明:
#include <>
void syslog(int priority, const char *message, arguments...);
priority参数的格式(severity level|facility code)
示例:
LOG_ERR|LOG_USER
severity level:
Priority Level Description
LOG_EMERG An emergency situation
LOG_ALERT High-priority problem, such as database corruption
LOG_CRIT Critical error, such as hardware failure
LOG_ERR Errors
LOG_WARNING Warning
LOG_NOTICE Special conditions requiring attention
LOG_INFO Informational messages
LOG_DEBUG Debug messages
facility value(转自头文件):
/* facility codes */
#define LOG_KERN (0<<3) /* kernel messages */
#define LOG_USER (1<<3) /* random user-level messages */
#define LOG_MAIL (2<<3) /* mail system */
#define LOG_DAEMON (3<<3) /* system daemons */
#define LOG_AUTH (4<<3) /* security/authorization messages */
#define LOG_SYSLOG (5<<3) /* messages generated internally by syslogd */
#define LOG_LPR (6<<3) /* line printer subsystem */
#define LOG_NEWS (7<<3) /* network news subsystem */
#define LOG_UUCP (8<<3) /* UUCP subsystem */
#define LOG_CRON (9<<3) /* clock daemon */
#define LOG_AUTHPRIV (10<<3) /* security/authorization messages (private) */
#define LOG_FTP (11<<3) /* ftp daemon */
示例代码:
#include <>
#include <>
int main(void)
{
FILE *f;
f = fopen("abc","r");
if(!f)
syslog(LOG_ERR|LOG_USER,"test - %m\n");
}
上面的日志信息由系统自动给出,我们也可过滤日志信息。用到以下函数:
#include <>
void closelog(void);
void openlog(const char *ident, int logopt, int facility);
int setlogmask(int maskpri);
logopt参数的选项:
logopt Parameter Description
LOG_PID Includes the process identifier, a unique number allocated to each process by the system, in the messages.
LOG_CONS Sends messages to the console if they can’t be logged.
LOG_ODELAY Opens the log facility at first call to .
LOG_NDELAY Opens the log facility immediately, rather than at first log.
示例代码:
#include <>
#include <>
#include <>
int main(void)
{
int logmask;
openlog("logmask", LOG_PID|LOG_CONS, LOG_USER); /*日志信息会包含进程id。*/
syslog(LOG_INFO, "informative message, pid=%d", getpid());
syslog(LOG_DEBUG,"debug message, should appear"); /*记录该日志信息。*/
logmask = setlogmask(LOG_UPTO(LOG_NOTICE)); /*设置屏蔽低于NOTICE级别的日志信息。*/
syslog(LOG_DEBUG, "debug message, should not appear"); /*该日志信息被屏蔽,不记录。*/
}
不同安全级别的日志信息存放在/var/log目录下的哪个文件中是由/etc/文件控制的,下面是我系统中文件的内容:
# /etc/ Configuration file for syslogd.
#
# For more information see (5)
# manpage.
#
# First some standard logfiles. Log by facility.
#
auth,authpriv.* /var/log/
*.*;auth, -/var/log/syslog
#cron.* /var/log/
daemon.* -/var/log/
kern.* -/var/log/
lpr.* -/var/log/
mail.* -/var/log/
user.* -/var/log/
uucp.* /var/log/
#
# Logging for the mail system. Split it up so that
# it is easy to write scripts to parse these files.
#
-/var/log/
-/var/log/
/var/log/
# Logging for INN news system
#
/var/log/news/
/var/log/news/
-/var/log/news/
#
# Some `catch-all' logfiles.
#
*.=debug;\
auth,;\
; -/var/log/debug
*.=info;*.=notice;*.=warn;\
auth,;\
cron,;\
mail, -/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;*.=warn /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.*;\
;;;\
*.=debug;*.=info;\
*.=notice;*.=warn |/dev/xconsole