/usr/share/perl5/Log/Log4perl/Appender/Limit.pm is in liblog-log4perl-perl 1.41-1.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | ######################################################################
# Limit.pm -- 2003, Mike Schilli <m@perlmeister.com>
######################################################################
# Special composite appender limiting the number of messages relayed
# to its appender(s).
######################################################################
###########################################
package Log::Log4perl::Appender::Limit;
###########################################
use strict;
use warnings;
use Storable;
our @ISA = qw(Log::Log4perl::Appender);
our $CVSVERSION = '$Revision: 1.7 $';
our ($VERSION) = ($CVSVERSION =~ /(\d+\.\d+)/);
###########################################
sub new {
###########################################
my($class, %options) = @_;
my $self = {
max_until_flushed => undef,
max_until_discarded => undef,
appender_method_on_flush
=> undef,
appender => undef,
accumulate => 1,
persistent => undef,
block_period => 3600,
buffer => [],
%options,
};
# Pass back the appender to be limited as a dependency
# to the configuration file parser
push @{$options{l4p_depends_on}}, $self->{appender};
# Run our post_init method in the configurator after
# all appenders have been defined to make sure the
# appenders we're connecting to really exist.
push @{$options{l4p_post_config_subs}}, sub { $self->post_init() };
bless $self, $class;
if(defined $self->{persistent}) {
$self->restore();
}
return $self;
}
###########################################
sub log {
###########################################
my($self, %params) = @_;
local $Log::Log4perl::caller_depth =
$Log::Log4perl::caller_depth + 2;
# Check if message needs to be discarded
my $discard = 0;
if(defined $self->{max_until_discarded} and
scalar @{$self->{buffer}} >= $self->{max_until_discarded} - 1) {
$discard = 1;
}
# Check if we need to flush
my $flush = 0;
if(defined $self->{max_until_flushed} and
scalar @{$self->{buffer}} >= $self->{max_until_flushed} - 1) {
$flush = 1;
}
if(!$flush and
(exists $self->{sent_last} and
$self->{sent_last} + $self->{block_period} > time()
)
) {
# Message needs to be blocked for now.
return if $discard;
# Ask the appender to save a cached message in $cache
$self->{app}->SUPER::log(\%params,
$params{log4p_category},
$params{log4p_level}, \my $cache);
# Save message and other parameters
push @{$self->{buffer}}, $cache if $self->{accumulate};
$self->save() if $self->{persistent};
return;
}
# Relay all messages we got to the SUPER class, which needs to render the
# messages according to the appender's layout, first.
# Log pending messages if we have any
$self->flush();
# Log current message as well
$self->{app}->SUPER::log(\%params,
$params{log4p_category},
$params{log4p_level});
$self->{sent_last} = time();
# We need to store the timestamp persistently, if requested
$self->save() if $self->{persistent};
}
###########################################
sub post_init {
###########################################
my($self) = @_;
if(! exists $self->{appender}) {
die "No appender defined for " . __PACKAGE__;
}
my $appenders = Log::Log4perl->appenders();
my $appender = Log::Log4perl->appenders()->{$self->{appender}};
if(! defined $appender) {
die "Appender $self->{appender} not defined (yet) when " .
__PACKAGE__ . " needed it";
}
$self->{app} = $appender;
}
###########################################
sub save {
###########################################
my($self) = @_;
my $pdata = [$self->{buffer}, $self->{sent_last}];
# Save the buffer if we're in persistent mode
store $pdata, $self->{persistent} or
die "Cannot save messages in $self->{persistent} ($!)";
}
###########################################
sub restore {
###########################################
my($self) = @_;
if(-f $self->{persistent}) {
my $pdata = retrieve $self->{persistent} or
die "Cannot retrieve messages from $self->{persistent} ($!)";
($self->{buffer}, $self->{sent_last}) = @$pdata;
}
}
###########################################
sub flush {
###########################################
my($self) = @_;
# Log pending messages if we have any
for(@{$self->{buffer}}) {
$self->{app}->SUPER::log_cached($_);
}
# call flush() on the attached appender if so desired.
if( $self->{appender_method_on_flush} ) {
no strict 'refs';
my $method = $self->{appender_method_on_flush};
$self->{app}->$method();
}
# Empty buffer
$self->{buffer} = [];
}
###########################################
sub DESTROY {
###########################################
my($self) = @_;
}
1;
__END__
=head1 NAME
Log::Log4perl::Appender::Limit - Limit message delivery via block period
=head1 SYNOPSIS
use Log::Log4perl qw(:easy);
my $conf = qq(
log4perl.category = WARN, Limiter
# Email appender
log4perl.appender.Mailer = Log::Dispatch::Email::MailSend
log4perl.appender.Mailer.to = drone\@pageme.com
log4perl.appender.Mailer.subject = Something's broken!
log4perl.appender.Mailer.buffered = 0
log4perl.appender.Mailer.layout = PatternLayout
log4perl.appender.Mailer.layout.ConversionPattern=%d %m %n
# Limiting appender, using the email appender above
log4perl.appender.Limiter = Log::Log4perl::Appender::Limit
log4perl.appender.Limiter.appender = Mailer
log4perl.appender.Limiter.block_period = 3600
);
Log::Log4perl->init(\$conf);
WARN("This message will be sent immediately.");
WARN("This message will be delayed by one hour.");
sleep(3601);
WARN("This message plus the last one will be sent now, separately.");
=head1 DESCRIPTION
=over 4
=item C<appender>
Specifies the name of the appender used by the limiter. The
appender specified must be defined somewhere in the configuration file,
not necessarily before the definition of
C<Log::Log4perl::Appender::Limit>.
=item C<block_period>
Period in seconds between delivery of messages. If messages arrive in between,
they will be either saved (if C<accumulate> is set to a true value) or
discarded (if C<accumulate> isn't set).
=item C<persistent>
File name in which C<Log::Log4perl::Appender::Limit> persistently stores
delivery times. If omitted, the appender will have no recollection of what
happened when the program restarts.
=item C<max_until_flushed>
Maximum number of accumulated messages. If exceeded, the appender flushes
all messages, regardless if the interval set in C<block_period>
has passed or not. Don't mix with C<max_until_discarded>.
=item C<max_until_discarded>
Maximum number of accumulated messages. If exceeded, the appender will
simply discard additional messages, waiting for C<block_period> to expire
to flush all accumulated messages. Don't mix with C<max_until_flushed>.
=item C<appender_method_on_flush>
Optional method name to be called on the appender attached to the
limiter when messages are flushed. For example, to have the sample code
in the SYNOPSIS section bundle buffered emails into one, change the
mailer's C<buffered> parameter to C<1> and set the limiters
C<appender_method_on_flush> value to the string C<"flush">:
log4perl.category = WARN, Limiter
# Email appender
log4perl.appender.Mailer = Log::Dispatch::Email::MailSend
log4perl.appender.Mailer.to = drone\@pageme.com
log4perl.appender.Mailer.subject = Something's broken!
log4perl.appender.Mailer.buffered = 1
log4perl.appender.Mailer.layout = PatternLayout
log4perl.appender.Mailer.layout.ConversionPattern=%d %m %n
# Limiting appender, using the email appender above
log4perl.appender.Limiter = Log::Log4perl::Appender::Limit
log4perl.appender.Limiter.appender = Mailer
log4perl.appender.Limiter.block_period = 3600
log4perl.appender.Limiter.appender_method_on_flush = flush
This will cause the mailer to buffer messages and wait for C<flush()>
to send out the whole batch. The limiter will then call the appender's
C<flush()> method when it's own buffer gets flushed out.
=back
If the appender attached to C<Limit> uses C<PatternLayout> with a timestamp
specifier, you will notice that the message timestamps are reflecting the
original log event, not the time of the message rendering in the
attached appender. Major trickery has been applied to accomplish
this (Cough!).
=head1 DEVELOPMENT NOTES
C<Log::Log4perl::Appender::Limit> is a I<composite> appender.
Unlike other appenders, it doesn't log any messages, it just
passes them on to its attached sub-appender.
For this reason, it doesn't need a layout (contrary to regular appenders).
If it defines none, messages are passed on unaltered.
Custom filters are also applied to the composite appender only.
They are I<not> applied to the sub-appender. Same applies to appender
thresholds. This behaviour might change in the future.
=head1 LICENSE
Copyright 2002-2013 by Mike Schilli E<lt>m@perlmeister.comE<gt>
and Kevin Goess E<lt>cpan@goess.orgE<gt>.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
Please contribute patches to the project on Github:
http://github.com/mschilli/log4perl
Send bug reports or requests for enhancements to the authors via our
MAILING LIST (questions, bug reports, suggestions/patches):
log4perl-devel@lists.sourceforge.net
Authors (please contact them via the list above, not directly):
Mike Schilli <m@perlmeister.com>,
Kevin Goess <cpan@goess.org>
Contributors (in alphabetical order):
Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp, Hutton
Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony
Foiani, James FitzGibbon, Carl Franks, Dennis Gregorovic, Andy
Grundman, Paul Harrington, Alexander Hartmaier David Hull,
Robert Jacobson, Jason Kohles, Jeff Macdonald, Markus Peter,
Brett Rann, Peter Rabbitson, Erik Selberg, Aaron Straup Cope,
Lars Thegler, David Viner, Mac Yang.
|