Linux:syslog()的使用和示例

本文详细解释了Linux系统日志库中的openlog(),syslog(),closelog()函数,包括参数含义、功能描述以及如何设置默认标识符和优先级。此外,还介绍了不同标志选项如LOG_PID和facility级别的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

man手册

命令行man openlog即可查看;写的非常详细,看完其实就懂了。

NAME
       closelog, openlog, syslog, vsyslog - send messages to the system logger

SYNOPSIS
       #include <syslog.h>

       void openlog(const char *ident, int option, int facility);
       void syslog(int priority, const char *format, ...);
       void closelog(void);

       void vsyslog(int priority, const char *format, va_list ap);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       vsyslog():
           Since glibc 2.19:
               _DEFAULT_SOURCE
           Glibc 2.19 and earlier:
               _BSD_SOURCE

DESCRIPTION
   openlog()
       openlog() opens a connection to the system logger for a program.

       The  string  pointed  to by ident is prepended to every message, and is
       typically set to the program name.  If ident is NULL, the program  name
       is  used.   (POSIX.1-2008  does  not specify the behavior when ident is
       NULL.)

       The option argument specifies flags  which  control  the  operation  of
       openlog()  and  subsequent  calls  to  syslog().  The facility argument
       establishes a default to be used if none  is  specified  in  subsequent
       calls  to  syslog().   The  values that may be specified for option and
       facility are described below.

       The use of openlog() is optional; it will automatically  be  called  by
       syslog() if necessary, in which case ident will default to NULL.

   syslog() and vsyslog()
       syslog()  generates  a  log  message, which will be distributed by sys‐
       logd(8).

       The priority argument is formed by ORing together a facility value  and
       a  level  value  (described  below).  If no facility value is ORed into
       priority, then the default value set by openlog() is used, or, if there
       was no preceding openlog() call, a default of LOG_USER is employed.

       The  remaining  arguments  are a format, as in printf(3), and any argu‐
       ments required by the format, except that the two-character sequence %m
       will be replaced by the error message string strerror(errno).  The for‐
       mat string need not include a terminating newline character.

       The function vsyslog() performs the same task as syslog() with the dif‐
       ference that it takes a set of arguments which have been obtained using
       the stdarg(3) variable argument list macros.

   closelog()
       closelog() closes the file descriptor being used to write to the system
       logger.  The use of closelog() is optional.
   Values for option
       The  option  argument  to  openlog() is a bit mask constructed by ORing
       together any of the following values:

       LOG_CONS       Write directly to the system  console  if  there  is  an
                      error while sending to the system logger.

       LOG_NDELAY     Open  the  connection immediately (normally, the connec‐
                      tion is opened when the first message is logged).   This
                      may  be  useful,  for example, if a subsequent chroot(2)
                      would make the pathname used internally by  the  logging
                      facility unreachable.

       LOG_NOWAIT     Do not  wait  for child processes that may have been cre‐
                      ated 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 or  POSIX.1-2008.)   Also  log  the
                      message to stderr.

       LOG_PID        Include the caller s PID with each message.

   Values for facility
       The  facility  argument is used to specify what type of program is log‐
       ging the message.  This lets the configuration file specify  that  mes‐
       sages from different facilities will be handled differently.

       LOG_AUTH       security/authorization messages

       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 pro‐
                      cesses)

       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

Values for level
       This determines the importance of the  message.   The  levels  are,  in
       order of decreasing importance:

       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

       The function setlogmask(3) can be used to restrict logging to specified
       levels only.

示例

#include <unistd.h>
#include <iostream>
#include <syslog.h>
using namespace std;
int main(int argc, char const *argv[])
{
    openlog("writenbycwd",LOG_PID,LOG_USER);
    syslog(LOG_INFO, "my syslog OK");
    closelog();
    return 0;
}
 

效果

需注意,要在sudo下运行,因为写入log的权限可能不够。

查看文件/var/log/syslog

cwd@cwd:~$ cd /var/log
cwd@cwd:/var/log$ grep "writenbycwd" syslog
Mar  1 09:20:57 cwd writenbycwd[4446]: my syslog OK
Mar  1 09:21:56 cwd writenbycwd[4561]: my syslog OK
Mar  1 09:28:11 cwd writenbycwd[4821]: my syslog OK

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术探索者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值