log4perl-devel:

I'm a new Log::Log4perl user and am trying to create some Log4perl 
helper logger functions.


I've found that:

     $Log::Log4perl::caller_depth


seems to affect the following layout placeholders:

     %C Fully qualified package (or class) name of the caller

     %l Fully qualified name of the calling method followed by the
        callers source the file name and line number between
        parentheses.

     %L Line number within the file where the log statement was issued

     %M Method or function where the logging request was issued

but it does not seem to affect the following layout placeholders:

     %c Category of the logging event

     %T A stack trace of functions called

nor the following argument to easy_init():

     category


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

Reply via email to