Log into a specific file using rsyslog functions

Viewed 47

Although this topic is discussed by other people but I could not get it done through reading explanations of other people here.

I would like to use syslog functions to log into a specific file. I can see the logged message but I could not have the logs printed into a specific file.

What I did is:

#define log_info(...) syslog(LOG_INFO, __VA_ARGS__);

First approach:

 openlog("PingWatchdog", LOG_PID|LOG_CONS, LOG_USER);
 log_info("[INFO]: PingWatchdog: pingDispatcher thread starting.");
 closelog();

in /etc/rsyslog.d there is a config file in which I added this rule :
 
if:syslogtag, isequal, "PingWatchdog:" /var/log/pingwatchdog.log
&stop

second approach:

openlog("PingWatchdog", 0, LOG_LOCAL1);
log_info("[INFO]: PingWatchdog: pingDispatcher thread starting.");
closelog();

in /etc/rsyslog.d there is a config file in which I added this rule :
local1.info /var/log/pingwatchdog.log

but these two methods could not help me to write into my desired file which is: /var/log/pingwatchdog.log my program name is PingWatchdog

I also tried this rule but not helpful: if $programname == 'PingWatchdog' then /var/log/pingwatchdog.log

any Idea what should I do?

1 Answers

Add below in rsyslog conf.

if ($syslogtag contains 'PingWatchdog') then {
    *.* /var/log/pingwatchdog.log.log
    stop
}
Related