Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to send messages from C program and from Perl script to a file (for example, /var/log/my_log). I don't have access to the syslog daemon configuration, so I think in launch a differente syslog daemon (syslog-ng).

I'm able to send messages from the command line, using logger command and its -u option. This log messages are logged on my_log instead of the usual messages file, but I cannot reproduce the same behavior from C or Perl.

The openlog function, apparently, doesn't have any option to specify a socket for logging, or the PID of the log process or something that allows me to send the messages to my own log instead the system log.

Can it be done or is it impossible using syslog library?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You are correct, the libc openlog() does not let you specify a socket, but that would usually only be useful if you needed a separate syslogd. (A socket is not the same as thing as the file that messages are logged to, of course.)

logger defaults to (on most systems I think) user.notice, so if it's logging to the correct place you should do something like this in C:

#include <syslog.h>
  ...
  openlog("myprog",LOG_PID,LOG_USER);
  syslog(LOG_INFO,"%s","my message...");

Or in perl:

use Sys::Syslog qw(:DEFAULT setlogsock);
setlogsock 'unix';   
# setlogsock('unix','/opt/my_root/dev/log');

openlog("myprog","pid","user");
syslog("info","my message...");

See your openlog(3) and Sys::Syslog(3) man pages for details on the constants.

If you really do want to log to a different socket than /dev/log you can do this easily in perl with second form of setlogsocket() as above. You may need to use a recent Sys::Syslog package, I have seen it fail with v0.17 when a path is specified.

It's slightly tricker in C, you need to connect() to the AF_UNIX socket and write() to it with the correct message format. I recommend you read the source for logger (functions myopenlog() and mysyslog()) in a recent-ish util-linux-2.x version.

On Linux (glibc) and FreeBSD the path that openlog() uses is hard-coded (_PATH_LOG from /usr/include/syslog.h), I know of no way to alter it at run time (other than a chroot, or .so DLL injection).

share|improve this answer
    
Thanks for your response. I need a separate syslog daemon indeed, because we cannot configure the default syslogd of the system. My idea is to launch, in my user space, a new daemon instance (I can choose between syslogd and syslog-ng, both are installed) and configure the openlog call to use my daemon instead the default one (use /opt/my_root/dev/log instead /dev/log as _PATH_LOG). –  JoseLSegura Mar 1 '13 at 14:19
    
Gotcha, updated. –  mr.spuratic Mar 1 '13 at 15:10
    
Thanks for your update, again :-D. I can't believe that I didn't think about take the logger sources!!! Thanks for the suggestion. I think that I will suggest this approach, creating a syslog-like library that allows to specify that kind of options that the standar libc doesn't allow. Thanks for your help –  JoseLSegura Mar 1 '13 at 15:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.