It looks like you are trying to setup an instance of Log::Log4perl up as a singleton (you are using my $log in the global scope of the module). Why bother with this when Log4perl already does this for you?

I would probably forgo the creation of an object, and just provide a class function in your module that gets a Log4perl object and initializes it first if it isn't already initialized. Something like this:

package MyLogger; # Wrapper class for Log::Log4perl. use strict; use warnings; use Log::Log4perl; sub get_logger { # initialize and return MyLogger object my $class = shift; my $module_name = shift; unless (Log::Log4perl->initialized) { Log::Log4perl->init("/opt/etc/log4perl.conf"); $Log::Log4perl::caller_depth = 1; } return Log::Log4perl->get_logger($module_name); }

Then in your code you get to use the actual Log::Log4perl object and you won't have to write helper functions like 'debug' and such. It is still efficient, since Log::Log4perl is a singleton. Here is how you could use it:

my $log = MyLogger->get_logger('database'); $log->debug('database blah blah'); $log = MyLogger->get_logger('apache'); $log->debug('apache blah blah');

The above code is untested, but it should work...

-Cees


In reply to Re: Wrapper class for log4perl redirects to wrong log file by cees
in thread 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.