Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Sys::Syslog in OOP Module

by Zarathustra (Beadle)
on Aug 24, 2006 at 03:43 UTC ( [id://569276]=perlquestion: print w/replies, xml ) Need Help??

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

What's the "most correctest" way to open, use/call, and close the Sys::Syslog library in one's OOP module/class? I've used Sys::Syslog many times in scripts, and it's quite obvious and simple how to use. But I've got an existing class, and I'm wondering how to best go about utilzing Sys::Syslog... Should I put the:
setlogsock( 'unix' ); openlog( 'FooClass', 'pid', 'local6' );
lines in a BEGIN block? and the:
closelog()
in an END block? Something like:
package FooClass; use strict; use warnings; use Sys::Syslog qw( :DEFAULT setlogsock ); BEGIN { setlogsock( 'unix' ); openlog( 'IPScan', 'pid', 'local6' ); } END { closelog() } sub new { my $invocant = shift; my $class = ref( $invocant ) || $invocant; my %args = @_; my $self = bless { %args }, $class; syslog( 'info', "blah blah blah" ); return $self; } sub foobar { my $self = shift; syslog( 'info', 'foobarred' ); # do stuff } 1;
Any help/advice is appreciated, thanks! UPDATE: When I attempt the above described pattern, I get an error message to the extent of:
no connection to syslog available - _PATH_LOG not available in syslog.h at /path/to/FooClass.pm +line 24
where line 24 is the first attempt to use the syslog call, for instance, at:
syslog( 'info', "blah blah blah" );
... in the new() method.

Replies are listed 'Best First'.
Re: Sys::Syslog in OOP Module
by Tanktalus (Canon) on Aug 24, 2006 at 03:59 UTC

    There are many ways to approach this - yours seems to be one reasonable method.

    Another way might be to use a centralised logging module which used a singleton approach to do something similar. The advantage here is that by delaying the openlog to the first call, you may go through an entire execution without ever opening the log - which may or may not be of significance. There would only be one call to openlog in your entire program because they would all go through MyLogger::logthis() - which would open the log if it wasn't already open. Then, using the magic of singletons, the DESTROY method would close it, or you could use the END block as above to detect if the log was opened, and, if so, then close it.

    I suppose a method that maps closest to how you developed procedurally would have the advantage of being a philosophy close to what you're used to, so my question is - before you tried the magic of OO, how did you use syslog?

    Update: The above was written before the real problem appeared in Zarathustra's update. The error message seems to say that your syslog.h doesn't have a macro required for this to work - that's a compile-time problem, not a run-time problem. Does your non-module use of syslog work on this same machine? What platform is it?

      > There are many ways to approach this - yours seems to be one reasonable method. >

      Unfortunately, mine doesn't work! (c8=

      I'm missing something regarding how the Sys::Syslog module ( or any similarly functioning module ) propagates its namespace/subroutines into another module/package.... obviously it works fine in a simple script environment who's package is main, however there apppears to be some "trick" I'm missing to get it available within another modules/packages namespace -- especially a class module such as the example.

      I tried appending &main:: and FooClass:: to the syslog call ( in a feeble attempt to find the wherever Sys::Syslog imported its functions, but that didn't help either. )

      > before you tried the magic of OO, how did you use syslog?

      Well, I've been using perl's OOP for many, many years - both as a user and as a module builder. Not until now have I needed a module which I also wanted to have write to syslog. The way I used syslog in simple (non-oop) scripts has been exactly the way it's documented in its perldoc.

      Cheers!

      > Does your non-module use of syslog work on this same machine? What platform is it?

      Many thanks for your continued assistance -- I'm at a loss...

      But to o answer your question: Yes, it works as expected in non-oop-modules ( and I've been using Sys::Syslog in scripts for quite some time ), even on the same machine - which is a linux platform ( gentoo ).

      While waiting for further assistance, I noticed that I had my:

      use Sys::Syslog qw( :DEFAULT setlogsock );

      _outside_ of the BEGIN block... so I figured that I should put that into a require into the BEGIN, because the BEGIN happens first:

      BEGIN { require Sys::Syslog; Sys::Syslog->import( 'setlogsock', 'openlog', 'syslog', 'closelo +g' ); setlogsock( 'unix' ); openlog( 'IPScan', 'pid', 'local0' ); }

      But that didn't appear to make a difference...

        Oh, man, I feel dumb...

        "Yes, it works as expected in non-oop-modules ( and I've been using Sys::Syslog in scripts for quite some time ), even on the same machine - which is a linux platform ( gentoo )."

        Lies.... all lies

        I went ahead and verified this ( which I obviously should have done first ), and no... it, in fact, did not work on the same machine in non-module'd code.

        So sorry for that, you're suspicion was exactly correct.

        By simply commenting out the 'setlogsock( 'unix' );', it all works as expected...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://569276]
Approved by chargrill
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-24 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found