in reply to Sys::Syslog in OOP Module

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?

Replies are listed 'Best First'.
Re^2: Sys::Syslog in OOP Module
by Zarathustra (Beadle) on Aug 24, 2006 at 04:14 UTC

    > 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!

Re^2: Sys::Syslog in OOP Module
by Zarathustra (Beadle) on Aug 24, 2006 at 04:38 UTC

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