分类: LINUX
2008-12-03 17:22:08
#include
void syslog(int
priority, const char *message, arguments...);
Facility values (from syslog.h)包含LOG_USER,LOG_LOCAL0, LOG_LOCAL1, up to LOG_LOCAL7(它们由本地管理员决定)。
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
Log信息包含消息头,消息体。消息头由facility indicator创建,包含日期时间。消息体由the message parameter to syslog创建。%m允许在当前消息中用错误信息代替错误号。
实例:
# cat syslog.c
#include
#include
#include
int main()
{
FILE *f;
f =
fopen("not_here","r");
if(!f)
syslog(LOG_ERR|LOG_USER,"oops - %m\n");
exit(0);
}
运行后可以在/var/log/messages中看到:
Dec 3 16:54:13
localhost syslog: oops - No such file or directory
其他功能:
#include
void closelog(void);
void openlog(const char *ident, int logopt, int
facility);
int setlogmask(int
maskpri);
上面ident用来指示创建log的应用程序。
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 syslog.
LOG_NDELAY Opens the log facility immediately, rather
than at first log.
使用syslog不需要先调用openlog,它会自动调用。LOG_MASK(priority)可以创建单个级别,LOG_UPTO(priority)包含多个级别。
实例:
# cat logmask.c
#include
#include
#include
#include
int main()
{
int logmask;
openlog("logmask", LOG_PID|LOG_CONS, LOG_USER);
syslog(LOG_INFO,"informative message, pid = %d", getpid());
syslog(LOG_DEBUG,"debug message, should appear");
logmask =
setlogmask(LOG_UPTO(LOG_NOTICE));
syslog(LOG_DEBUG,"debug message, should not appear");
exit(0);
}
运行结果:
/var/log/message
Dec 3 17:07:05
localhost logmask[27491]: informative message, pid = 27491