
Linux系统编程42 进程控制 - 系统日志
发布日期:2021-05-07 13:25:50
浏览次数:23
分类:精选文章
本文共 4097 字,大约阅读时间需要 13 分钟。
Linux 系统日志:cd /var/log/
/var/log/ 下面的各种日志文件 中有一个 主日志文件 :message在 ubuntu16.04中 主日志文件是 syslog
-rw-r----- 1 syslog adm 258172 Feb 22 18:37 syslogsyslogd服务:所有需要写系统日志的进程,都把自己所写的系统日志提交给 syslogd服务
那么怎么提交自己的系统日志给 syslogd服务呢? 通过如下接口:
openlog()syslog()closelog()
这三个函数配合使用 会将需要日志打印到 /var/log/message 等主日志文件中
NAME closelog, openlog, syslog, vsyslog - send messages to the system loggerSYNOPSIS #includevoid openlog(const char *ident, int option, int facility);//参数 :名字 ,日志携带标志 ,记录消息的程序类型 void syslog(int priority, const char *format, ...);//参数: 日志级别 ,提交内容 void closelog(void);
openlog()为程序打开到系统日志记录器的连接。ident所指向的字符串被置于每条消息的前面,通常被设置为程序名。如果ident为空,则使用程序名。option参数指定了控制openlog()操作和后续对syslog()调用的标志
syslog()产生一条日志消息,通过syslogd(8)分发。priority参数是通过将工具和级别值(后面会解释)相加而形成的。剩下的参数是一种格式,如
printf(3)和格式要求的任何参数,除了两个字符序列%m将被错误消息字符串strerror(errno)替换。如果需要,可以添加末尾的换行符。option
The option argument to openlog() is an OR of any of these: LOG_CONS Write directly to system console if there is an error while sending to system logger. LOG_NDELAY Open the connection immediately (normally, the connection is opened when the first message is logged). LOG_NOWAIT Don't wait for child processes that may have been created 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.) Print to stderr as well. LOG_PID Include PID with each message.
facility
The facility argument is used to specify what type of program is logging the message. This lets the configuration file specify that messages 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 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
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#include #include #include #include #include #include #include #define FILENAME "/tmp/out"static int craetdeamon(void){ pid_t pid; int fd; pid = fork(); if(pid < 0) { perror("fork()"); return -1; } if(pid > 0) { printf("%d\n",getpid()); exit(0); } fd = open("/dev/null",O_RDWR); if(fd < 0) { perror("open()"); return -1; } dup2(fd,0); dup2(fd,1); dup2(fd,2); if(fd > 2) { close(fd); } setsid(); chdir("/"); return 0;}int main(int argc,char* argv[]){ FILE* fp; int i; openlog("craetdeamon",LOG_PID,LOG_DAEMON);//与系统日志建立联系 if(craetdeamon()) { syslog(LOG_ERR,"craetdeamon failed!");//上报 exit(1); }else{ syslog(LOG_INFO,"craetdeamon successded!"); } fp = fopen(FILENAME,"w"); if(fp == NULL) { syslog(LOG_ERR,"fopen %s failed!",FILENAME); exit(1); } syslog(LOG_INFO,"fopen %s successede!",FILENAME); for(i = 0; ;i++) { fprintf(fp,"%d\n",i); fflush(fp); syslog(LOG_DEBUG,"%d is printed!",i); sleep(1); } exit(0);}
以root 权限查看 /var/log/syslog 即可看到 系统日志文件输出
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年03月23日 00时59分34秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
vue项目通过vue.config.js配置文件进行proxy反向代理跨域
2019-03-05
Linux下安装MySql过程
2019-03-05
原生vue实现VantUI中IndexBar索引导航栏功能
2019-03-05
解决:android TextView上响应部分文字的事件
2019-03-05
android:使用audiotrack 类播放wav文件
2019-03-05
vue通过better-scroll 封装自定义的下拉刷新组件
2019-03-05
android解决:使用多线程和Handler同步更新UI
2019-03-05
vue自定义封装Loading组件
2019-03-05
解决移动端项目中苹果ios和安卓android手机点击输入框网页页面自动放大缩小
2019-03-05
Element UI 中动态路由的分析及实现
2019-03-05
使用springMVC配置视图管理器后找不到指定的页面
2019-03-05
关于js中对于Promise的深入理解
2019-03-05
对于js中的this指向的深入理解
2019-03-05
杭电 2007 平方和与立方和(输入数据的大小顺序并不能默认)
2019-03-05
十大排序算法之三:插入排序(Python)
2019-03-05
利用Python实现循环队列
2019-03-05
十大排序算法之四:希尔排序(Python)
2019-03-05
利用递归实现二叉树的前中后序遍历(Python)
2019-03-05
A*寻路算法(Python)
2019-03-05
Python刷题输入输出
2019-03-05