Linux 之log 日志记录

在Linux 中可以用syslog 函数向系统日志文件中写入日志记录,日志记录正常在/var/log/messages文件里,在syslog使用时在哪里需要记录日志的地方调用该函数即可。 

openlog用于指定些特定log 输出,譬如用来制定日志由那个程序产生。

   #include <syslog.h>

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

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

参数:

priority:表示日志等级

format:日志输出形式

 

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.)  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.
:

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

参数:

ident:设置字符串,该字符串会添加在日志前面

option:对后续syslog 调用配置行为进行配置,默认值可以看后面man 手册。

facility:记录一个将用于后续系统syslog调用设置值,默认LOG_USER

 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.)  Print to stderr as well.

       LOG_PID        Include PID with each message.
:

 

 

例:

#include<syslog.h>
#include<stdio.h>
#include<stdlib.h>

int main()
{
	
	
	FILE *fp;
	char str[20]="zdg_program";
	fp=fopen("zdg.txt","r");
	if(fp==NULL)
	{	
		openlog(str,LOG_PID,LOG_USER);//用于在程序加入我们需要指明的日志打印程序是那个
		
		syslog(LOG_ERR,"zdg-%m\n");//向系统日志文件记录日志。
		
		exit(0);
	}


	return 0;
}

因为不存在该文件,所以打开会出错,在var/log/messages下回生成日志记录,%m是表示错误输出格式。

an 14 17:06:21 localhost NetworkManager[1752]: <info>   domain name 'localdomain'
Jan 14 17:06:21 localhost dhclient[1774]: bound to 192.168.27.128 -- renewal in 744 seconds.
Jan 14 17:18:13 localhost zdg_program[3298]: zdg-No such file or directory
[zdg@localhost My_project]$ 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值