syslog

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 1、void openlog(const char *ident, int option, int facility);第一个参数ident将是一个标记,ident所表示的字符串将固定地加在每行日志的前面以标识这个日志,通常就写成当前程序的名称以作标记。

 1、void openlog(const char *ident, int option, int facility);

第一个参数ident将是一个标记,ident所表示的字符串将固定地加在每行日志的前面以标识这个日志,通常就写成当前程序的名称以作标记。
第二个参数option是下列值取与运算的结果:LOG_CONS,LOG_NDELAY, LOG_NOWAIT, LOG_ODELAY, LOG_PERROR,LOG_PID,各值意义请参考man openlog手册:
    LOG_CONS
       Writedirectly to system console if there is an error while sending tosystem logger.
    LOG_NDELAY
       Openthe connection immediately (normally, the connection is opened whenthe first message is logged).
    LOG_NOWAIT
       Don’t wait for childprocesses that may have been created while logging themessage.  (The GNU C library does not createa child process, so this option has no effect onLinux.)
    LOG_ODELAY
       The converseof LOG_NDELAY; opening of the connection is delayed until syslog()is called.  (This is the default,  and need not be specified.)
    LOG_PERROR
       (Notin SUSv3.) Print to stderr as well.
    LOG_PID
        IncludePID with each message. 
第三个参数facility指明记录日志的程序的类型。
        The facility argument is used to specify what type ofprogram  is logging  the  message.
        This  lets the configuration file specify thatmessages from different facilities will be
        handled differently.
        LOG_AUTH      security/authorization messages (DEPRECATED Use LOG_AUTHPRIVinstead)
        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 generage 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

 

2、void syslog(int priority, const char *format, ...);

        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 tospecified levels only.

 

3、int setlogmask(int maskpri);

进程有一个日志优先级掩码,该掩码决定哪些调用syslog(3)可能被记录。所有其他调用都将被忽略。日志是为在掩码中设置相应位的优先级启用的。最初的掩码是这样的,日志是为所有优先级启用的。
setlogmask()函数为当前的进程设置这个日志掩码,并返回先前的掩码。如果掩码参数为0,那么当前的日志掩码不会被修改。
这8个优先级分别是LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO and LOG_DEBUG.与优先级p相对应的位是LOG_MASK(p)。一些系统还提供了一个宏LOG_UPTO(p),以在上面的列表中包括p的所有优先级。
 

4、调用openlog是可选择的。如果不调用openlog,则在第一次调用syslog时,自动调用openlog。调用closelog也是可选择的,它只是关闭被用于与syslog守护进程通信的描述符。调用openlog使我们可以指定一个ident,以后, 此ident 将被加至每则记录消息中。

 

5、代码示例

#include <syslog.h>  
int main(int argc, char **argv)  
{  
   openlog("MyMsgMARK", LOG_CONS | LOG_PID, 0);  
   syslog(LOG_DEBUG,  
          "This is a syslog test message generated by program '%s'\n",  
          argv[0]);  
   closelog();  
   return0;  
}  

编译生成可执行程序后,运行一次程序将向/var/log/message文件添加一行信息如下:
Feb 12 08:48:38 localhost MyMsgMARK[7085]: This is a syslog testmessage generated by program './a.out'  

 

相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
目录
相关文章
|
SQL 缓存 监控
纯干货:客户端代码框架设计!
纯干货:客户端代码框架设计!
纯干货:客户端代码框架设计!
|
2天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
13天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1283 5
|
12天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1312 87
|
1天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
171 82
2025年阿里云域名备案流程(新手图文详细流程)