spoovy has asked for the wisdom of the Perl Monks concerning the following question:

Hello all. Probably lots of faults as I'm new to programming, but what I'm trying to do is get the process pid in the logfile, like this entry:
Jan 19 10:29:09 mgmt yum[26377]: Installed: perl-Unix-Syslog-1.1-3.el6 +.x86_64
I'm using Unix::Syslog to do the logging but I suspect my problem is related to perl rather than that module specifically. My code:
use constant LOGINFO => 'LOG_INFO'; use constant LOGPID => 'LOG_PID'; # Open syslog channel openlog('my-program.pl', @{[ LOGPID ]}, 3); # Daemonise $daemon->Init(); syslog (@{[ LOGINFO ]}, "my-program.pl started and daemonised"); # Run the thing &main_function(); closelog();
But what I get in syslog is this:
Jan 19 17:20:46 mgmt my-program.pl: my-program.pl started and daemonis +ed
..and the following error on starting the program:
Argument "LOG_PID" isn't numeric in subroutine entry at ./my-program.p +l line 66.
It seems it want a numeric value, but is there a numeric value for LOG_PID? Also it seems I should be able to put text in here so I can specify multiple option rather than just one. Any help appreciated. spoov

Replies are listed 'Best First'.
Re: Numeric values required in Unix::Syslog?
by toolic (Bishop) on Jan 19, 2015 at 17:58 UTC
    Just a wild guess from reading the Unix::Syslog SYNOPSIS and EXAMPLES, but does this work for you?
    use warnings; use strict; use Unix::Syslog qw(:macros :subs); # Open syslog channel openlog('my-program.pl', LOG_PID, 3);
      Yes I did read the SYNOPSIS and EXAMPLES. The reason that doesn't work for me is that barewords aren't allowed. Hence my wierd use of constants to try to get around it.
Re: Numeric values required in Unix::Syslog?
by Anonymous Monk on Jan 19, 2015 at 18:15 UTC
    It seems it want a numeric value, but is there a numeric value for LOG_PID?
    man 3 syslog

    It's a constant in syslog.h

    Get rid of use constant. And get rid of @{[ LOGPID ]} - what is that for anyway? Constants should be bitwise or-ed. Like this: LOG_PID | LOG_CONS | LOG_WHATEVER. Those should be constants exported by Unix::Syslog, not your own.
      As above; barewords not allowed:
      # Open syslog channel openlog('my-program', LOG_PID, 3);
      [me@mgmt test-project (master)]# ./my-program.pl Bareword "LOG_PID" not allowed while "strict subs" in use at ./my-prog +ram.pl line 65.
        use Unix::Syslog qw( :macros )

        Also, you might want to read how Exporter works

Re: Numeric values required in Unix::Syslog?
by flexvault (Monsignor) on Jan 19, 2015 at 18:19 UTC

    ,

    Maybe try this:

    syslog (@{[ LOGINFO ]}, "[$$] started and daemonised");
    What I do is create a 'sub' that is called with the message and debug level, that way if I'm in test mode I get all the errors, and in production only important errors. The 'sub' adds the pid ($$) to the error message.

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin