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

Dear Monks,

I want to make a generic wrapper class for Log::Log4perl to be used from both Perl modules and Mason components. It works fine when called from Perl modules, but when called from Mason, the logfile reports HTML/Mason/Component.pm as source of the event instead of my source (see below). It seems like increasing the caller_depth jumps the call stack twice instead of just once.
# Module id = Log::Log4perl INST_VERSION 0.37 # Module id = HTML::Mason INST_VERSION 1.23 # This is perl, v5.6.1 built for i386-linux # Server version: Apache/1.3.23 (Unix) (Red-Hat/Linux) package MyLogger; use Log::Log4perl; Log::Log4perl->init("/opt/app/log4perl.conf"); my ($log, $module_name); sub new { my MyLogger $self = shift; unless (ref $self) { $self = fields::new ($self); } $module_name = shift; $log = Log::Log4perl->get_logger($module_name); # compensate for this wrapper class by adding 1 $Log::Log4perl::caller_depth++; return $self; } sub debug{ my ($self, $logmsg) = @_; $log->debug($logmsg) if ($log->is_debug()); }
Log entry sample
2004/01/15 10:48:27 DEBUG> /usr/lib/perl5/site_perl/5.6.1/HTML/Mason/C +omponent.pm:134 [7674] stop
Expected log entry:
2004/01/15 10:48:27 DEBUG> /opt/app/root_dir/login_:13 [7674] stop
Any clues how to make Log4perl report my real source in the log when called from a Mason component?

Andreas
--

Replies are listed 'Best First'.
Re: Log::Log4perl wrapper + Mason - wrong source file in log
by RMGir (Prior) on Jan 15, 2004 at 15:21 UTC
    I'm not sure I follow...

    What happens if you just don't increment caller_depth? Or increment it twice?

    I'm unfamiliar with both Mason and log4perl, so these may not be helpful suggestions. But they seem like the natural things to try.

    If that doesn't work, looking at the whole caller backtrace will probably cause a bright light to come on. :)

    I'm guessing Mason's event handling control flow is probably a bit different from what your mental model of it is.


    Mike
      I was just *stupid*.

      Log::Log4perl implements a static object. Thus incrementing the caller_depth for each new logger instance is a very bad idea(tm).

      Simply replace
      # Increase value value of $Log::Log4perl::caller_depth by one $Log::Log4perl::caller_depth++; # NO NO NO!!!
      With this:
      # Set value of $Log::Log4perl::caller_depth to one. Since Log4perl's # logger is a static object, the value must not be increased more # than once. $Log::Log4perl::caller_depth = 1;

      Andreas
      --