I've set up a simple Apache web server with Mason and mod_perl. The Mason code accesses a MySQL database through a dedicated perl module called "database.pm". Log::Log4perl is used for logging. I'm trying to log messages from the Mason component and the messages from the database module to two separate logfiles. When Log4perl is initialized and called directly, it all works fine. When is use a wrapper class (which in turn calls Log4perl), all messages seems to end up in one of the logfiles.

I don't think this is a hierarchy issue in since the two categories don't inherit each other.

What's wrong with my wrapper class "MyLogger"? Does anyone have a working wrapper class for Log::Log4perl?
package MyLogger; # Wrapper class for Log::Log4perl. use strict; use warnings; use fields; use Log::Log4perl; my $log; my $module_name; sub new { # initialize and return MyLogger object my MyLogger $self = shift; unless (ref $self) { $self = fields::new ($self); } $module_name = shift; if (!Log::Log4perl::initialized()) { Log::Log4perl->init("/opt/etc/log4perl.conf"); $Log::Log4perl::caller_depth = 1; } $log = Log::Log4perl->get_logger($module_name); return $self; } sub debug{ # log a single message my ($self, $logmsg) = @_; $log->debug($logmsg); } ...
Sample calling code that works (not using wrapper class: Messages appear as expected in "database.log" and "apache.log" according to given parameter):
Log::Log4perl->init("/opt/etc/log4perl.conf"); my $log = Log::Log4perl->get_logger("database"); ... $log->debug("blah blah");
Sample calling code that does *not* work (using wrapper class: All messages end up in "apache.log")
my $log MyLogger::->new("database"); ... $log->debug("blah blah");
The setup is as follows:
OS RH7.3 Linux 2.4.20-20.7 i686 Apache/1.3.23 (Unix) (Red-Hat/Linux) HTML::Mason INST_VERSION 1.23 mod_perl INST_VERSION 1.26 Log::Log4perl INST_VERSION 0.37
The configuration file /opt/etc/log4perl.conf:
log4perl.logger.database = DEBUG, FileAppenderDatabase log4perl.logger.apache = DEBUG, FileAppenderApache ### APPENDERS ### log4perl.appender.FileAppenderDatabase= Log::Log4perl::Appender::F +ile log4perl.appender.FileAppenderDatabase.filename=/var/log/database.log log4perl.appender.FileAppenderDatabase.mode= append log4perl.appender.FileAppenderApache= Log::Log4perl::Appender:: +File log4perl.appender.FileAppenderApache.filename= /var/log/apache.log log4perl.appender.FileAppenderApache.mode= append ### LAYOUTS ### log4perl.appender.FileAppenderDatabase.layout=PatternLayout log4perl.appender.FileAppenderDatabase.layout.ConversionPattern=%d %p> + %F:%L [%P] %M - %m%n log4perl.appender.FileAppenderApache.layout=PatternLayout log4perl.appender.FileAppenderApache.layout.ConversionPattern=%d %p> % +C %F:%L [%P] %M - %m%n
Update: Yes it's a typo, it should read "my $log =MyLogger::->new("database");"

In reply to Wrapper class for log4perl redirects to wrong log file by andreas1234567

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.