File Coverage

blib/lib/Log/Log4perl/Filter/CallerMatch.pm
Criterion Covered Total %
statement 42 42 100.0
branch 13 14 92.8
condition 6 6 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 72 73 98.6


line stmt bran cond sub pod time code
1             package Log::Log4perl::Filter::CallerMatch;
2             BEGIN {
3 1     1   101295 $Log::Log4perl::Filter::CallerMatch::VERSION = '1.200';
4             }
5              
6             # ABSTRACT: Filter Log4perl messages based on call frames
7              
8 1     1   30 use 5.006;
  1         4  
  1         37  
9 1     1   6 use strict;
  1         2  
  1         60  
10 1     1   5 use Log::Log4perl::Config;
  1         2  
  1         30  
11 1     1   7 use base 'Log::Log4perl::Filter';
  1         2  
  1         103  
12 1     1   6 use Carp;
  1         2  
  1         411  
13              
14              
15             sub new {
16 12     12 1 26982 my ( $class, %options ) = @_;
17              
18 12         74 my $self = {
19             AcceptOnMatch => 1,
20             MinCallFrame => 0,
21             MaxCallFrame => 5,
22             %options,
23             };
24              
25 12         45 $self->{AcceptOnMatch} = Log::Log4perl::Config::boolean_to_perlish( $self->{AcceptOnMatch} );
26 12 100       212 $self->{SubToMatch} = defined $self->{SubToMatch} ? qr($self->{SubToMatch}) : qr/.*/;
27 12 100       104 $self->{PackageToMatch} = defined $self->{PackageToMatch} ? qr($self->{PackageToMatch}) : qr/.*/;
28 12 100       82 $self->{StringToMatch} = defined $self->{StringToMatch} ? qr($self->{StringToMatch}) : qr/.*/;
29              
30 12 100       41 if ( defined $self->{CallFrame} ) {
31 2         9 $self->{MinCallFrame} = $self->{MaxCallFrame} = $self->{CallFrame};
32             }
33              
34 12         34 bless $self, $class;
35              
36 12         48 return $self;
37             }
38              
39              
40             sub ok {
41 16     16 1 20840 my ( $self, %p ) = @_;
42              
43 16         29 my $message = join $Log::Log4perl::JOIN_MSG_ARRAY_CHAR, @{ $p{message} };
  16         44  
44              
45 16         69 my ( $s_regex, $p_regex, $m_regex ) = ( $self->{SubToMatch}, $self->{PackageToMatch}, $self->{StringToMatch} );
46              
47             # First climb out of Log4perl's internals (differs depending on whether Boolean is being used etc..
48 16         19 my $base = 0;
49 16         184 $base++ while caller($base) =~ m/^Log::Log4perl/;
50              
51 16         58 foreach my $i ( $self->{MinCallFrame} .. $self->{MaxCallFrame} ) {
52 44         181 my ( $package, $sub ) = ( caller $i + $base )[ 0, 3 ];
53 44 100       113 next unless $package;
54 16 50       34 next unless $sub;
55 1     1   5 no warnings;
  1         2  
  1         132  
56 16 100 100     231 if ( $sub =~ $s_regex && $package =~ $p_regex && $message =~ $m_regex ) {
      100        
57 9         51 return $self->{AcceptOnMatch};
58             }
59             }
60 7         39 return !$self->{AcceptOnMatch};
61             }
62              
63             1;
64              
65              
66             =pod
67              
68             =head1 NAME
69              
70             Log::Log4perl::Filter::CallerMatch - Filter Log4perl messages based on call frames
71              
72             =head1 VERSION
73              
74             version 1.200
75              
76             =head1 DESCRIPTION
77              
78             This Log4perl custom filter checks the call stack using caller() and filters
79             the subroutine and package using user-provided regular expressions. You can specify
80             a specific call frame to test against, or have the filter iterate through a range of call frames.
81              
82             =head1 ATTRIBUTES
83              
84             =head2 StringToMatch
85              
86             A perl5 regular expression, matched against the log message.
87              
88             =head2 AcceptOnMatch
89              
90             Defines if the filter is supposed to pass or block the message on a match (C or C).
91              
92             =head2 PackageToMatch
93              
94             A perl5 regular expression, matched against the 1st item in the array returned by caller() (e.g. "package")
95              
96             =head2 SubToMatch
97              
98             A perl5 regular expression, matched against the 4th item in the array returned by caller() (e.g. "subroutine")
99              
100             =head2 CallFrame
101              
102             The call frame to use when requesting information from caller(). (e.g. $i in caller($i)
103              
104             =head2 MinCallFrame
105              
106             The first call frame tested against when iterating through a series of call frames. Ignored if CallFrame specified.
107              
108             =head2 MaxCallFrame
109              
110             The last call frame tested against when iterating through a series of call frames. Ignored if CallFrame specified.
111              
112             =head1 METHODS
113              
114             =head2 new
115              
116             Constructor. Refer to L for more information
117              
118             =head2 ok
119              
120             Decides whether log message should be accepted or not. Refer to L for more information
121              
122             =head1 USAGE
123              
124             # log.conf
125             log4perl.logger = ALL, A1
126             log4perl.appender.A1 = Log::Log4perl::Appender::TestBuffer
127             log4perl.appender.A1.Filter = MyFilter
128             log4perl.appender.A1.layout = Log::Log4perl::Layout::SimpleLayout
129              
130             log4perl.filter.MyFilter = Log::Log4perl::Filter::CallerMatch
131             log4perl.filter.MyFilter.SubToMatch = WebGUI::Session::ErrorHandler
132             log4perl.filter.MyFilter.PackageToMatch = Flux::
133             log4perl.filter.MyFilter.StringToMatch = Operand1
134              
135             =head1 SEE ALSO
136              
137             L,
138             L,
139             L,
140             L,
141             L
142              
143             =head1 AUTHOR
144              
145             Patrick Donelan
146              
147             =head1 COPYRIGHT AND LICENSE
148              
149             This software is copyright (c) 2010 by Patrick Donelan.
150              
151             This is free software; you can redistribute it and/or modify it under
152             the same terms as the Perl 5 programming language system itself.
153              
154             =cut
155              
156              
157             __END__