On Sun, 23 Jan 2011, David Christensen wrote:

> I've found that: $Log::Log4perl::caller_depth
> ... does not seem to affect the following layout placeholders:
>     %c Category of the logging event
>     %T A stack trace of functions called

Hi David,

sorry for the delay. There's a subtle difference between the package
class hierarchy in your application and Log4perl's categories: While
they're almost always the same, they are in fact independent concepts.

That's is why %c does not change automatically when you set caller_depth,
because caller_depth refers to the package hierarchy, while %c refers to
the Log4perl category. There's a section in the Log4perl manual that
talks about this (not easy to find, though):

     
http://search.cpan.org/~mschilli/Log-Log4perl-1.31/lib/Log/Log4perl.pm#Using_Log::Log4perl_with_wrapper_functions_and_classes

If you scroll down to "Also, note that if you're writing a subclass of
Log4perl", it talks about using

     Log::Log4perl->wrapper_register(__PACKAGE__);

to register a wrapper class, so that get_logger() skips it and uses
the application's class as a category instead. Note that if you're using
qw(:easy), you need to use

     package Helper;
     BEGIN {
         Log::Log4perl->wrapper_register(__PACKAGE__);
     };
     use Log::Log4perl qw(:easy);

to make sure Log4perl's easy mode stealth loggers are set up
correctly.

With regards to %T, I think that's a bug, I'll investigate.

Hope that helps.

-- Mike

Mike Schilli
m...@perlmeister.com


> How do I write a helper function that does affect %c, %T, and category
> -- e.g. so that calls to my helper logger functions are handled by
> Log::Log4perl the same way that calls to Log::Log4perl logger functions
> are handled?
>
>
> TIA,
>
> David
>
>
>
> 2011-01-23 14:30:18 dpchrist@p43400e ~/sandbox
> $ cat log4perl-easy
> #!/usr/bin/perl
>
> $! = 1;
>
> #############
> package Foo;
> #############
> use strict;
> use warnings;
> use Log::Log4perl qw(:easy);
> sub foo {
>     DEBUG(       @_, 'foo', __LINE__);
>     Helper::help(@_, 'foo', __LINE__);
> }
>
> ###############
> package Helper;
> ###############
> use strict;
> use warnings;
> use Log::Log4perl qw(:easy);
> sub help
> {
>     local $Log::Log4perl::caller_depth =
>     $Log::Log4perl::caller_depth + 1;
>     DEBUG(@_, 'help', __LINE__);
> }
>
> #############
> package main;
> #############
> use strict;
> use warnings;
> use Log::Log4perl qw(:easy);
>
> Log::Log4perl->easy_init( {
>     layout    => '%%c %c %n'
>                . '%%C %C %n'
>                . '%%d %d %n'
>                . '%%F %F %n'
>                . '%%H %H %n'
>                . '%%l %l %n'
>                . '%%L %L %n'
>                . '%%m %m %n'
>                . '%%M %M %n'
>                . '%%p %p %n'
>                . '%%P %P %n'
>                . '%%r %r %n'
> ### %R is broken in Log::Log4perl version 1.16
> ### Invalid conversion in sprintf: "%R" at
> /usr/share/perl5/Log/Log4perl/Layout/PatternLayout.pm line 286.
> #              . '%%R %R %n'
>                . '%%T %T %n'
>                . '%%x %x %n',
> });
>
> print "### call DEBUG()\n";
> DEBUG(       'main', __LINE__);
> print "### call Foo::foo()\n";
> Foo::foo(    'main', __LINE__);
> print "### call Helper::help()\n";
> Helper::help('main', __LINE__);
>
>
>
> 2011-01-23 14:30:20 dpchrist@p43400e ~/sandbox
> $ perl log4perl-easy
> ### call DEBUG()
> %c main
> %C main
> %d 2011/01/23 14:30:24
> %F log4perl-easy
> %H p43400e
> %l main:: log4perl-easy (57)
> %L 57
> %m main57
> %M main::
> %p DEBUG
> %P 5030
> %r 41
> %T Log::Log4perl::__ANON__('main', 57) called at log4perl-easy line 57
> %x [undef]
> ### call Foo::foo()
> %c Foo
> %C Foo
> %d 2011/01/23 14:30:24
> %F log4perl-easy
> %H p43400e
> %l Foo::foo log4perl-easy (12)
> %L 12
> %m main59foo12
> %M Foo::foo
> %p DEBUG
> %P 5030
> %r 45
> %T Foo::foo('main', 59) called at log4perl-easy line 59
> %x [undef]
> %c Helper
> %C Foo
> %d 2011/01/23 14:30:24
> %F log4perl-easy
> %H p43400e
> %l Foo::foo log4perl-easy (13)
> %L 13
> %m main59foo13help26
> %M Foo::foo
> %p DEBUG
> %P 5030
> %r 45
> %T Helper::help('main', 59, 'foo', 13) called at log4perl-easy line 13,
> Foo::foo('main', 59) called at log4perl-easy line 59
> %x [undef]
> ### call Helper::help()
> %c Helper
> %C main
> %d 2011/01/23 14:30:24
> %F log4perl-easy
> %H p43400e
> %l main:: log4perl-easy (61)
> %L 61
> %m main61help26
> %M main::
> %p DEBUG
> %P 5030
> %r 46
> %T Helper::help('main', 61) called at log4perl-easy line 61
> %x [undef]
>
>
>
> 2011-01-23 14:22:11 dpchrist@p43400e ~/sandbox
> $ cat log4perl-easy-category
> #!/usr/bin/perl
>
> $! = 1;
>
> #############
> package Foo;
> #############
> use strict;
> use warnings;
> use Log::Log4perl qw(:easy);
> sub foo {
>     DEBUG(       @_, 'foo', __LINE__);
>     Helper::help(@_, 'foo', __LINE__);
> }
>
> ###############
> package Helper;
> ###############
> use strict;
> use warnings;
> use Log::Log4perl qw(:easy);
> sub help
> {
>     local $Log::Log4perl::caller_depth =
>     $Log::Log4perl::caller_depth + 1;
>     DEBUG(@_, 'help', __LINE__);
> }
>
> #############
> package main;
> #############
> use strict;
> use warnings;
> use Log::Log4perl qw(:easy);
>
> print "### no category\n";
> Log::Log4perl->easy_init(
>     {layout=>'%m category=%c %n'}
> );
> DEBUG(       'main', __LINE__);
> Foo::foo(    'main', __LINE__);
> Helper::help('main', __LINE__);
>
> print "### category 'main'\n";
> Log::Log4perl->easy_init(
>     {layout=>'%m category=%c %n', category=>'main'}
> );
> DEBUG(       'main', __LINE__);
> Foo::foo(    'main', __LINE__);
> Helper::help('main', __LINE__);
>
> print "### category 'Foo'\n";
> Log::Log4perl->easy_init(
>     {layout=>'%m category=%c %n', category=>'Foo'}
> );
> DEBUG(       'main', __LINE__);
> Foo::foo(    'main', __LINE__);
> Helper::help('main', __LINE__);
>
>
>
> 2011-01-23 14:22:51 dpchrist@p43400e ~/sandbox
> $ perl log4perl-easy-category
> ### no category
> main40 category=main
> main41foo12 category=Foo
> main41foo13help26 category=Helper
> main42help26 category=Helper
> ### category 'main'
> main48 category=main
> ### category 'Foo'
> main57foo12 category=Foo
>
>
>
> 2011-01-23 14:22:56 dpchrist@p43400e ~/sandbox
> $ cat /etc/debian_version
> 5.0.8
>
>
>
> 2011-01-23 14:23:23 dpchrist@p43400e ~/sandbox
> $ perl -v
>
> This is perl, v5.10.0 built for i486-linux-gnu-thread-multi
>
> Copyright 1987-2007, Larry Wall
>
> Perl may be copied only under the terms of either the Artistic License
> or the
> GNU General Public License, which may be found in the Perl 5 source kit.
>
> Complete documentation for Perl, including FAQ lists, should be found on
> this system using "man perl" or "perldoc perl".  If you have access to the
> Internet, point your browser at http://www.perl.org/, the Perl Home Page.
>
>
>
> 2011-01-23 14:23:26 dpchrist@p43400e ~/sandbox
> $ perl -MLog::Log4perl -e 'print $Log::Log4perl::VERSION, "\n"'
> 1.16
>
>
> ------------------------------------------------------------------------------
> Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
> Finally, a world-class log management solution at an even better price-free!
> Download using promo code Free_Logger_4_Dev2Dev. Offer expires
> February 28th, so secure your free ArcSight Logger TODAY!
> http://p.sf.net/sfu/arcsight-sfd2d
> _______________________________________________
> log4perl-devel mailing list
> log4perl-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/log4perl-devel
>

------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires 
February 28th, so secure your free ArcSight Logger TODAY! 
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
log4perl-devel mailing list
log4perl-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/log4perl-devel

Reply via email to