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

I have a large project consisting of cron jobs and cgis that do some screen scrapes. Life became unmanagebale - cutting, pasting, and dying over and over again. I finally turned to OOP. I barely know my $self, but it is beautiful. Wait till I discover my inheritance.

I'm now turning to Log4perl and log4perl conf files to clean up my mess of "print 'foo' if's," and rewriting my .pl's and .cgi's to use my module 'EZ::Emulate.pm.' I want Emulate.pm's logging behavior to be determined by the Log4perl conf file of the program calling on Emulate.pm.

I currently have it working with this in main:

my $log_conf = './log.fifo.conf'; use Log::Log4perl qw(get_logger :levels); Log::Log4perl->init("$log_conf"); our $logger = get_logger(); $logger->info('Foo');
and in Emulate.pm with:

$main::logger->info('Bar');

However, having the name of a main package variable hard coded in the module just feels... dirty.

Should I instead be doing something like this in main:

use EZ::Emulate; $EZ::Emulate::log_conf = $log_conf;
and then in Emulate.pm use "use Log4perl" and create loggers in the EZ::Emulate namespace, or should I be approaching this differently?

I'd really rather do this right the second time instead of the third, and I'm not sure that I'm not missing something simple here. Any suggestions?

Thanks,

Queeg

Replies are listed 'Best First'.
Re: Log4perl with my module
by adrianh (Chancellor) on Dec 30, 2005 at 00:57 UTC
    I want Emulate.pm's logging behavior to be determined by the Log4perl conf file of the program calling on Emulate.pm.

    If Emulate.pm isn't doing anything OO-ish then something like the following should work (untested):

    package Emulate; use Log::Log4perl; my $Logger = Log::Log4perl->get_logger( __PACKAGE__ ); ... sub something_that_needs_to_log { $Logger->info('Foo'); }

    If Emulate.pm is implementing a class, especially one that's likely to be subclassed, you need to be a little bit more careful about just using __PACKAGE__ as the logging category passed to get_logger(). See the section "Pitfalls with Categories" in the Log::Log4perl docs for more info.

      If Emulate.pm isn't doing anything OO-ish..

      I do have methods in it, but no inheritance. Adding: qw(get_logger :levels)to use Log::Log4perl; got your code working as desired. Thanks!

      Queeg