Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Subclassing L4P appears to generate fixed category log messages

by Bloodnok (Vicar)
on Feb 20, 2013 at 14:29 UTC ( [id://1019789]=perlquestion: print w/replies, xml ) Need Help??

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

Hi there fellow monasterians ,

I appear to have encountered an unexpected problem whilst trying to extend Log::Log4perl by way of subclassing it as in (simple form) ...

#! /usr/bin/env perl package L4P::Subclass; use Log::Log4perl qw/:no_extra_logdie_message :levels/; our @ISA = qw/Log::Log4perl/; Log::Log4perl->wrapper_register( __PACKAGE__ ); Log::Log4perl->easy_init( { name => 'LOG', file => "STDOUT", layout => '[%d - %r] %p (%c) %M:%L %m%n', level => $TRACE, } ) or die 'easy_init() failed - '; sub new { my $pkg = shift; my $log = bless Log::Log4perl->get_logger(caller()), __PACKAGE__; warn $log->get_logger(caller())->category(); return $log; } sub trace_start { my $self = shift; $self->debug("Starting with args: " . join ', ', @_); } #our $AUTOLOAD; sub AUTOLOAD { my $self = shift; (my $method = $AUTOLOAD) =~ s/.*://; Log::Log4perl->get_logger(caller())->$method(@_); } package main; my $log = L4P::Subclass->new(); $log->trace_start("Please don't let it be L4P::Subclass");

However, when the script is run, the output is

L4P.Subclass at work/repos/Git/tools/lib/IBM-PTC-Core/LogTest.pl line +22. [2013/02/20 14:22:48 - 1] DEBUG (L4P.Subclass) main:::44 Starting with + args: Please don't let it be L4P::Subclass
- the, not necessarily clear, problem is that although the package name and line number are correct (as in main::44), the category of the logged message is always L4P.SubClass c/w the expected value of main.

Question is, have I done something wrong in my code or is there actually something wrong ?

As always, any help &/or hints will be gratefully received.

TIA & rgds ,

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re: Subclassing L4P appears to generate fixed category log messages
by zwon (Abbot) on Feb 23, 2013 at 13:55 UTC

    Your problem starts here:

    my $log = bless Log::Log4perl->get_logger(caller()), __PACKAGE__;

    You're subclassing Log::Log4perl package, but in constructor you're creating Log::Log4perl::Logger object and reblessing it into L4P::Subclass. I think you should in L4P::Subclass implement your own get_logger method which will return L4P::Subclass::Logger object.

    Later in AUTOLOAD you're ignoring this logger object and creating a new one:

    Log::Log4perl->get_logger(caller())->$method(@_);
    but AUTOLOAD is called from your package from trace_start, so category is "L4P.Subclass" and not main. You can replace it with caller(1) or just use get_logger() as Log::Log4perl will automatically skip frames belonging to your module because you've registered it as wrapper. But in order this to work you should also fix the constructor, something as simple as:
    sub new { return bless {}, shift; }
    will do the thing, reblessing Logger object into L4P::Subclass somehow confuses Log4perl and AUTOLOAD invokes itself recursively.

    PS: also note, that you can create custom log levels. I can't find documentation, but the code is in Log::Log4perl::Logger

      TFT, I take your point about the call to bless() - I came to the same conclusion as yourself and blessed an anon scalar into the Subclass - which appears to correct (or, at least) avoid the confusion to L4P.

      A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1019789]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found