[log4perl-devel] $Log::Log4perl::caller_depth, layout %c and %T, and category

2011-01-24 Thread David Christensen
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 

Re: [log4perl-devel] $Log::Log4perl::caller_depth, layout %c and %T, and category

2011-01-30 Thread David Christensen
Mike Schilli wrote:
 There's a section in the Log4perl manual thattalks 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
  

Thank you for the reply.  :-)


Yes, I read that.


 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);

That code doesn't work on my system:

 2011-01-30 14:08:11 dpchrist@p43400e ~/sandbox
 $ cat log4perl-helper.pl
 #!/usr/bin/perl
 package Helper;
 BEGIN {
 Log::Log4perl-wrapper_register(__PACKAGE__);
 };
 use Log::Log4perl qw(:easy);

 2011-01-30 14:08:16 dpchrist@p43400e ~/sandbox
 $ perl log4perl-helper.pl
 Can't locate object method wrapper_register via package 
Log::Log4perl (perhaps you forgot to load Log::Log4perl?) at 
log4perl-helper.pl line 4.
 BEGIN failed--compilation aborted at log4perl-helper.pl line 5.


Putting the 'use' statement before the 'BEGIN' statement makes Perl 
happy, but %c still shows the Helper package, not 'main':

 2011-01-30 14:22:44 dpchrist@p43400e ~/sandbox
 $ nl log4perl-helper2.pl
  1 #!/usr/bin/perl
  2 package Helper;
  3 use Log::Log4perl qw(:easy);
  4 BEGIN {
  5 Log::Log4perl-wrapper_register(__PACKAGE__);
  6 };
  7 sub help { DEBUG(__FILE__, __LINE__, ' ', @_) }
  8 package main;
  9 use Log::Log4perl qw(:easy);
 10 Log::Log4perl-easy_init({layout='%c %m %n'});
 11 DEBUG(__FILE__, __LINE__);
 12 Helper::help(__FILE__, __LINE__);

 2011-01-30 14:23:00 dpchrist@p43400e ~/sandbox
 $ perl log4perl-helper2.pl
 main log4perl-helper2.pl11
 Helper log4perl-helper2.pl7 log4perl-helper2.pl12


Any suggestions?


David


2011-01-30 14:23:03 dpchrist@p43400e ~/sandbox
$ cat /etc/debian_version
5.0.8

2011-01-30 14:24:04 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-30 14:24:06 dpchrist@p43400e ~/sandbox
$ perl -MLog::Log4perl -e 'print $Log::Log4perl::VERSION, \n'
1.31

--
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


Re: [log4perl-devel] $Log::Log4perl::caller_depth, layout %c and %T, and category

2011-01-30 Thread David Christensen
Mike Schilli wrote:
  We get tons of spam on this list, so it's moderated.

That's what I thought.  I'll post to the list and BCC you.


 What you want to do instead is use the long form with get_logger():
 package Helper;
 use Log::Log4perl qw(get_logger);
 Log::Log4perl-wrapper_register(__PACKAGE__);
 sub help { get_logger()-debug(__FILE__, -, __LINE__, ' ', @_) }

That solves both %c and 'category' -- thanks!  The 'category' filtering 
will be very useful.  :-)


I'm not certain if %T is doing the right thing (?).  You said there 
might be a bug.  Do you mean that the:

 ... Helper::help(...) called at ...

stances should be

 ... Log::Log4perl::__ANON__(...) called at ...

?  It's not a burning issue for me; I can use the output as is.


David



2011-01-30 19:53:41 dpchrist@p43400e ~/sandbox
$ nl log4perl-helper4.pl
  1 #!/usr/bin/perl
  2 package Helper;
  3 use Log::Log4perl qw(get_logger);
  4 Log::Log4perl-wrapper_register(__PACKAGE__);
  5 sub help { get_logger()-debug(@_, ' ', __FILE__, '-', __LINE__); }

  6 package Foo;
  7 sub foo { Helper::help(@_, ' ', __FILE__, '-', __LINE__); }

  8 package main;
  9 use Log::Log4perl qw(:easy);
 10 Log::Log4perl-easy_init({layout='%m %T %n'});
 11 DEBUG(   __FILE__, '-', __LINE__);
 12 print \n;
 13 Helper::help(__FILE__, '-', __LINE__);
 14 print \n;
 15 Foo::foo(__FILE__, '-', __LINE__);

2011-01-30 19:55:32 dpchrist@p43400e ~/sandbox
$ perl log4perl-helper4.pl
log4perl-helper4.pl-13 Log::Log4perl::__ANON__('log4perl-helper4.pl', 
'-', 13) called at log4perl-helper4.pl line 13

log4perl-helper4.pl-15 log4perl-helper4.pl-5 
Helper::help('log4perl-helper4.pl', '-', 15) called at 
log4perl-helper4.pl line 15

log4perl-helper4.pl-17 log4perl-helper4.pl-8 log4perl-helper4.pl-5 
Helper::help('log4perl-helper4.pl', '-', 17, ' ', 'log4perl-helper4.pl', 
'-', 8) called at log4perl-helper4.pl line 8, 
Foo::foo('log4perl-helper4.pl', '-', 17) called at log4perl-helper4.pl 
line 17

2011-01-30 19:55:35 dpchrist@p43400e ~/sandbox
$ cat /etc/debian_version
5.0.8

2011-01-30 19:55:44 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-30 19:55:46 dpchrist@p43400e ~/sandbox
$ perl -MLog::Log4perl -e 'print $Log::Log4perl::VERSION, \n'
1.31

--
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


Re: [log4perl-devel] Log::Log4perl best practices

2011-09-28 Thread David Christensen
On 09/26/2011 10:14 AM, Mike Schilli wrote:
 Actually, :easy mode macros and get_logger() are identical in function,
 the difference is just typing convenience.

 I personally use :easy macros like DEBUG and INFO in conjunction with
 log4perl configuration files, unless for really simple scripts for which
 easy_init() is more appropriate.

Ok.  I've upgraded my wrapper functions to use caller() when calling 
get_logger().


Thanks!


David


--
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
___
log4perl-devel mailing list
log4perl-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/log4perl-devel