| 1 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # Limit.pm -- 2003, Mike Schilli <m@perlmeister.com> | ||||||||||||||||||||||||||||||||||||||||||
| 3 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # Special composite appender limiting the number of messages relayed | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # to its appender(s). | ||||||||||||||||||||||||||||||||||||||||||
| 6 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 9 | package Log::Log4perl::Appender::Limit; | ||||||||||||||||||||||||||||||||||||||||||
| 10 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Storable; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | our @ISA = qw(Log::Log4perl::Appender); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | our $VERSION = '1.53'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 8 | my($class, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 8 | my $self = { | |||||||||||||||||||||||||||||||||||||||||
| 26 | max_until_flushed => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | max_until_discarded => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | appender_method_on_flush | ||||||||||||||||||||||||||||||||||||||||||
| 29 | => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | appender => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | accumulate => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | persistent => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | block_period => 3600, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | buffer => [], | ||||||||||||||||||||||||||||||||||||||||||
| 35 | %options, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | # Pass back the appender to be limited as a dependency | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # to the configuration file parser | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 8 | push @{$options{l4p_depends_on}}, $self->{appender}; | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | # Run our post_init method in the configurator after | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # all appenders have been defined to make sure the | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # appenders we're connecting to really exist. | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 8 | ▶ | push @{$options{l4p_post_config_subs}}, sub { $self->post_init() }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 8 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 8 | ▶ | if(defined $self->{persistent}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | $self->restore(); | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 8 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 32 | ▶ | sub log { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log: called CC=12 SCAR=25.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 32 | my($self, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 32 | local $Log::Log4perl::caller_depth = | |||||||||||||||||||||||||||||||||||||||||
| 62 | $Log::Log4perl::caller_depth + 2; | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # Check if message needs to be discarded | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 32 | my $discard = 0; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 32 | ▶ | if(defined $self->{max_until_discarded} and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"max_until_discarded"} and scalar @{$self->{"buffer"};} >= $self->{"max_until_discarded"} - 1
MC/DC50%1/2
defined $self->{"max_until_discarded"} and scalar @{$self->{"buffer"};} >= $self->{"max_until_discarded"} - 1
defined $self->{"max_until_discarded"}
scalar @{$self->{"buffer"};} >= $self->{"max_until_discarded"} - 1
Truth table66%2/3
A and B
A= defined $self->{"max_until_discarded"}B= scalar @{$self->{"buffer"};} >= $self->{"max_until_discarded"} - 1
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 22 | scalar @{$self->{buffer}} >= $self->{max_until_discarded} - 1) { | |||||||||||||||||||||||||||||||||||||||||
| 68 | 22 | $discard = 1; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | # Check if we need to flush | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 32 | my $flush = 0; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 32 | ▶ | if(defined $self->{max_until_flushed} and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"max_until_flushed"} and scalar @{$self->{"buffer"};} >= $self->{"max_until_flushed"} - 1
MC/DC100%2/2
defined $self->{"max_until_flushed"} and scalar @{$self->{"buffer"};} >= $self->{"max_until_flushed"} - 1
defined $self->{"max_until_flushed"}
scalar @{$self->{"buffer"};} >= $self->{"max_until_flushed"} - 1
Truth table100%3/3
A and B
A= defined $self->{"max_until_flushed"}B= scalar @{$self->{"buffer"};} >= $self->{"max_until_flushed"} - 1
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 6 | scalar @{$self->{buffer}} >= $self->{max_until_flushed} - 1) { | |||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | $flush = 1; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 32 | ▶ | if(!$flush and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $flush and (exists $self->{"sent_last"} and $self->{"sent_last"} + $self->{"block_period"} > time)
exists $self->{"sent_last"} and $self->{"sent_last"} + $self->{"block_period"} > time
MC/DC66%2/3
not $flush and (exists $self->{"sent_last"} and $self->{"sent_last"} + $self->{"block_period"} > time)
not $flush
exists $self->{"sent_last"}
$self->{"sent_last"} + $self->{"block_period"} > time
Truth table75%3/4
not(A) and B and C
A= not $flushB= exists $self->{"sent_last"}C= $self->{"sent_last"} + $self->{"block_period"} > time
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | (exists $self->{sent_last} and | ||||||||||||||||||||||||||||||||||||||||||
| 80 | $self->{sent_last} + $self->{block_period} > time() | ||||||||||||||||||||||||||||||||||||||||||
| 81 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 82 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # Message needs to be blocked for now. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 23 | ▶ | return if $discard; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # Ask the appender to save a cached message in $cache | ||||||||||||||||||||||||||||||||||||||||||
| 87 | $self->{app}->SUPER::log(\%params, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | $params{log4p_category}, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | $params{log4p_level}, \my $cache); | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | # Save message and other parameters | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | ▶ | push @{$self->{buffer}}, $cache if $self->{accumulate}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | ▶ | $self->save() if $self->{persistent}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # Relay all messages we got to the SUPER class, which needs to render the | ||||||||||||||||||||||||||||||||||||||||||
| 100 | # messages according to the appender's layout, first. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | # Log pending messages if we have any | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 9 | $self->flush(); | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # Log current message as well | ||||||||||||||||||||||||||||||||||||||||||
| 106 | $self->{app}->SUPER::log(\%params, | ||||||||||||||||||||||||||||||||||||||||||
| 107 | $params{log4p_category}, | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 9 | $params{log4p_level}); | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 9 | $self->{sent_last} = time(); | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # We need to store the timestamp persistently, if requested | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 9 | ▶ | $self->save() if $self->{persistent}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 8 | ▶ | sub post_init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post_init: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 8 | my($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 8 | ▶ | if(! exists $self->{appender}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | die "No appender defined for " . __PACKAGE__; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 8 | my $appenders = Log::Log4perl->appenders(); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 8 | my $appender = Log::Log4perl->appenders()->{$self->{appender}}; | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 8 | ▶ | if(! defined $appender) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | die "Appender $self->{appender} not defined (yet) when " . | |||||||||||||||||||||||||||||||||||||||||
| 130 | __PACKAGE__ . " needed it"; | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 8 | $self->{app} = $appender; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub save { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | my($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $pdata = [$self->{buffer}, $self->{sent_last}]; | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | # Save the buffer if we're in persistent mode | ||||||||||||||||||||||||||||||||||||||||||
| 144 | store $pdata, $self->{persistent} or | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | die "Cannot save messages in $self->{persistent} ($!)"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | sub restore { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
restore: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | my($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | if(-f $self->{persistent}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | my $pdata = retrieve $self->{persistent} or | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | die "Cannot retrieve messages from $self->{persistent} ($!)"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ($self->{buffer}, $self->{sent_last}) = @$pdata; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 14 | ▶ | sub flush { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flush: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 14 | my($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | # Log pending messages if we have any | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 14 | for(@{$self->{buffer}}) { | |||||||||||||||||||||||||||||||||||||||||
| 167 | 3 | $self->{app}->SUPER::log_cached($_); | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | # call flush() on the attached appender if so desired. | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 14 | ▶ | if( $self->{appender_method_on_flush} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 2 | my $method = $self->{appender_method_on_flush}; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 2 | $self->{app}->$method(); | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | # Empty buffer | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 14 | $self->{buffer} = []; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 1 | my($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||||||||||||||||||||||