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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
|
###########################################################################
#
# File.pm
#
# Copyright (C) 1999 Raphael Manfredi.
# Copyright (C) 2002-2017 Mark Rogaski, mrogaski@cpan.org;
# all rights reserved.
#
# See the README file included with the
# distribution for license information.
#
##########################################################################
use strict;
require Log::Agent::Driver;
########################################################################
package Log::Agent::Driver::File;
use vars qw(@ISA);
@ISA = qw(Log::Agent::Driver);
#
# ->make -- defined
#
# Creation routine.
#
# Attributes (and switches that set them):
#
# prefix the application name
# duperr whether to duplicate "error" channels to "output"
# stampfmt stamping format ("syslog", "date", "own", "none") or closure
# showpid whether to show pid after prefix in []
# channels where each channel ("error", "output", "debug") goes
# chanperm what permissions each channel ("error", "output", "debug") has
# magic_open flag to tell whether ">>file" or "|proc" are allowed filenames
# rotate default rotating policy for logfiles
#
# Additional switches:
#
# file sole channel, implies -duperr = 0 and supersedes -channels
# perm file permissions that supersedes all channel permissions
#
# Other attributes:
#
# channel_obj opened channel objects
#
sub make {
my $self = bless {}, shift;
my (%args) = @_;
my $prefix;
my $file;
my $perm;
my %set = (
-prefix => \$prefix, # Handled by parent via _init
-duperr => \$self->{'duperr'},
-channels => \$self->{'channels'},
-chanperm => \$self->{'chanperm'},
-stampfmt => \$self->{'stampfmt'},
-showpid => \$self->{'showpid'},
-magic_open => \$self->{'magic_open'},
-file => \$file,
-perm => \$perm,
-rotate => \$self->{'rotate'},
);
while (my ($arg, $val) = each %args) {
my $vset = $set{lc($arg)};
unless (ref $vset) {
require Carp;
Carp::croak("Unknown switch $arg");
}
$$vset = $val;
}
#
# If -file was used, it supersedes -duperr and -channels
#
if (defined $file && length $file) {
$self->{'channels'} = {
'debug' => $file,
'output' => $file,
'error' => $file,
};
$self->{'duperr'} = 0;
}
#
# and we do something similar for file permissions
#
if (defined $perm && length $perm) {
$self->{chanperm} = {
debug => $perm,
output => $perm,
error => $perm
};
}
$self->_init($prefix, 0); # 1 is the skip Carp penalty for confess
$self->{channels} = {} unless $self->channels; # No defined channels
$self->{chanperm} = {} unless $self->chanperm; # No defined perms
$self->{channel_obj} = {}; # No opened files
#
# Check for logfile rotation, which can be specified on a global or
# file by file basis. Since Log::Agent::Rotate is a separate extension,
# it may not be installed.
#
my $use_rotate = defined($self->rotate) ? 1 : 0;
unless ($use_rotate) {
foreach my $chan (keys %{$self->channels}) {
$use_rotate = 1 if ref $self->channels->{$chan} eq 'ARRAY';
last if $use_rotate;
}
}
if ($use_rotate) {
eval {
require Log::Agent::File::Rotate;
};
if ($@) {
warn $@;
require Carp;
Carp::croak("Must install Log::Agent::Rotate to use rotation");
}
}
return $self;
}
#
# Attribute access
#
sub duperr { $_[0]->{duperr} }
sub channels { $_[0]->{channels} }
sub chanperm { $_[0]->{chanperm} }
sub channel_obj { $_[0]->{channel_obj} }
sub stampfmt { $_[0]->{stampfmt} }
sub showpid { $_[0]->{showpid} }
sub magic_open { $_[0]->{magic_open} }
sub rotate { $_[0]->{rotate} }
#
# ->prefix_msg -- defined
#
# NOP: channel handles prefixing for us.
#
sub prefix_msg {
my $self = shift;
return $_[0];
}
#
# ->chanfn
#
# Return channel file name.
#
sub chanfn {
my $self = shift;
my ($channel) = @_;
my $filename = $self->channels->{$channel};
if (ref $filename eq 'ARRAY') {
$filename = $filename->[0];
}
# No channel defined, use 'error'
$filename = $self->channels->{'error'} unless
defined $filename && length $filename;
$filename = '<STDERR>' unless defined $filename;
return $filename;
}
#
# ->channel_eq -- defined
#
# Compare two channels.
#
# It's hard to know for certain that two channels are equivalent, so we
# compare filenames. This is not correct, of course, but it will do for
# what we're trying to achieve here, namely avoid duplicates if possible
# when traces are remapped to Carp::Datum.
#
sub channel_eq {
my $self = shift;
my ($chan1, $chan2) = @_;
my $fn1 = $self->chanfn($chan1);
my $fn2 = $self->chanfn($chan2);
return $fn1 eq $fn2;
}
#
# ->write -- defined
#
sub write {
my $self = shift;
my ($channel, $priority, $logstring) = @_;
my $chan = $self->channel($channel);
return unless $chan;
$chan->write($priority, $logstring);
}
#
# ->channel
#
# Return channel object (one of the Log::Agent::Channel::* objects)
#
sub channel {
my $self = shift;
my ($name) = @_;
my $obj = $self->channel_obj->{$name};
$obj = $self->open_channel($name) unless $obj;
return $obj;
}
#
# ->open_channel
#
# Open given channel according to the configured channel description and
# return the object file descriptor.
#
# If no channel of that name was defined, use 'error' or STDERR.
#
sub open_channel {
my $self = shift;
my ($name) = @_;
my $filename = $self->channels->{$name};
#
# Handle possible logfile rotation, which may be defined globally
# or on a file by file basis.
#
my $rotate; # A Log::Agent::Rotate object
if (ref $filename eq 'ARRAY') {
($filename, $rotate) = @$filename;
} else {
$rotate = $self->rotate;
}
my @common_args = (
-prefix => $self->prefix,
-stampfmt => $self->stampfmt,
-showpid => $self->showpid,
);
my @other_args;
my $type;
#
# No channel defined, use 'error', or revert to STDERR
#
unless (defined $filename && length $filename) {
$filename = $self->channels->{'error'};
($filename, $rotate) = @$filename if ref $filename eq 'ARRAY';
}
unless (defined $filename && length $filename) {
require Log::Agent::Channel::Handle;
select((select(main::STDERR), $| = 1)[0]);
$type = "Log::Agent::Channel::Handle";
@other_args = (-handle => \*main::STDERR);
} else {
require Log::Agent::Channel::File;
$type = "Log::Agent::Channel::File";
@other_args = (
-filename => $filename,
-magic_open => $self->magic_open,
-share => 1,
);
push(@other_args, -fileperm => $self->chanperm->{$name})
if $self->chanperm->{$name};
push(@other_args, -rotate => $rotate) if ref $rotate;
}
return $self->channel_obj->{$name} =
$type->make(@common_args, @other_args);
}
#
# ->emit_output
#
# Force error message to the regular 'output' channel with a specified tag.
#
sub emit_output {
my $self = shift;
my ($prio, $tag, $str) = @_;
my $cstr = $str->clone; # We're prepending tag on a copy
$cstr->prepend("$tag: ");
$self->write('output', $prio, $cstr);
}
###
### Redefined routines to handle duperr
###
#
# ->logconfess
#
# When `duperr' is true, emit message on the 'output' channel prefixed
# with FATAL.
#
sub logconfess {
my $self = shift;
my ($str) = @_;
$self->emit_output('critical', "FATAL", $str) if $self->duperr;
$self->SUPER::logconfess($str); # Carp strips calls within hierarchy
}
#
# ->logxcroak
#
# When `duperr' is true, emit message on the 'output' channel prefixed
# with FATAL.
#
sub logxcroak {
my $self = shift;
my ($offset, $str) = @_;
my $msg = Log::Agent::Message->make(
$self->carpmess($offset, $str, \&Carp::shortmess)
);
$self->emit_output('critical', "FATAL", $msg) if $self->duperr;
#
# Carp strips calls within hierarchy, so that new call should not show,
# there's no need to adjust the frame offset.
#
$self->SUPER::logdie($msg);
}
#
# ->logdie
#
# When `duperr' is true, emit message on the 'output' channel prefixed
# with FATAL.
#
sub logdie {
my $self = shift;
my ($str) = @_;
$self->emit_output('critical', "FATAL", $str) if $self->duperr;
$self->SUPER::logdie($str);
}
#
# ->logerr
#
# When `duperr' is true, emit message on the 'output' channel prefixed
# with ERROR.
#
sub logerr {
my $self = shift;
my ($str) = @_;
$self->emit_output('error', "ERROR", $str) if $self->duperr;
$self->SUPER::logerr($str);
}
#
# ->logcluck
#
# When `duperr' is true, emit message on the 'output' channel prefixed
# with WARNING.
#
sub logcluck {
my $self = shift;
my ($str) = @_;
$self->emit_output('warning', "WARNING", $str) if $self->duperr;
$self->SUPER::logcluck($str); # Carp strips calls within hierarchy
}
#
# ->logwarn
#
# When `duperr' is true, emit message on the 'output' channel prefixed
# with WARNING.
#
sub logwarn {
my $self = shift;
my ($str) = @_;
$self->emit_output('warning', "WARNING", $str) if $self->duperr;
$self->SUPER::logwarn($str);
}
#
# ->logxcarp
#
# When `duperr' is true, emit message on the 'output' channel prefixed
# with WARNING.
#
sub logxcarp {
my $self = shift;
my ($offset, $str) = @_;
my $msg = Log::Agent::Message->make(
$self->carpmess($offset, $str, \&Carp::shortmess)
);
$self->emit_output('warning', "WARNING", $msg) if $self->duperr;
$self->SUPER::logwarn($msg);
}
#
# ->DESTROY
#
# Close all opened channels, so they may be removed from the common pool.
#
sub DESTROY {
my $self = shift;
my $channel_obj = $self->channel_obj;
return unless defined $channel_obj;
foreach my $chan (values %$channel_obj) {
$chan->close if defined $chan;
}
}
1; # for require
__END__
=head1 NAME
Log::Agent::Driver::File - file logging driver for Log::Agent
=head1 SYNOPSIS
use Log::Agent;
require Log::Agent::Driver::File;
my $driver = Log::Agent::Driver::File->make(
-prefix => "prefix",
-duperr => 1,
-stampfmt => "own",
-showpid => 1,
-magic_open => 0,
-channels => {
error => '/tmp/output.err',
output => 'log.out',
debug => '../appli.debug',
},
-chanperm => {
error => 0777,
output => 0666,
debug => 0644
}
);
logconfig(-driver => $driver);
=head1 DESCRIPTION
The file logging driver redirects logxxx() operations to specified files,
one per channel usually (but channels may go to the same file).
The creation routine make() takes the following arguments:
=over 4
=item C<-channels> => I<hash ref>
Specifies where channels go. The supplied hash maps channel names
(C<error>, C<output> and C<debug>) to filenames. When C<-magic_open> is
set to true, filenames are allowed magic processing via perl's open(), so
this allows things like:
-channels => {
'error' => '>&FILE',
'output' => '>newlog', # recreate each time, don't append
'debug' => '|mailx -s whatever user',
}
If a channel (e.g. 'output') is not specified, it will go to the 'error'
channel, and if that one is not specified either, it will go to STDERR instead.
If you have installed the additional C<Log::Agent::Rotate> module, it is
also possible to override any default rotating policy setup via the C<-rotate>
argument: instead of supplying the channel as a single string, use an array
reference where the first item is the channel file, and the second one is
the C<Log::Agent::Rotate> configuration:
my $rotate = Log::Agent::Rotate->make(
-backlog => 7,
-unzipped => 2,
-max_write => 100_000,
-is_alone => 1,
);
my $driver = Log::Agent::Driver::File->make(
...
-channels => {
'error' => ['errors', $rotate],
'output' => ['output, $rotate],
'debug' => ['>&FILE, $rotate], # WRONG
},
-magic_open => 1,
...
);
In the above example, the rotation policy for the C<debug> channel will
not be activated, since the channel is opened via a I<magic> method.
See L<Log::Agent::Rotate> for more details.
=item C<-chanperm> => I<hash ref>
Specifies the file permissions for the channels specified by C<-channels>.
The arguemtn is a hash ref, indexed by channel name, with numeric values.
This option is only necessary to override the default permissions used by
Log::Agent::Channel::File. It is generally better to leave these
permissive and rely on the user's umask.
See L<perlfunc(3)/umask> for more details..
=item C<-duperr> => I<flag>
When true, all messages normally sent to the C<error> channel are also
copied to the C<output> channel with a prefixing made to clearly mark
them as such: "FATAL: " for logdie(), logcroak() and logconfess(),
"ERROR: " for logerr() and "WARNING: " for logwarn().
Note that the "duplicate" is the original error string for logconfess()
and logcroak(), and is not strictly identical to the message that will be
logged to the C<error> channel. This is a an accidental feature.
Default is false.
=item C<-file> => I<file>
This switch supersedes both C<-duperr> and C<-channels> by defining a
single file for all the channels.
=item C<-perm> => I<perm>
This switch supersedes C<-chanperm> by defining consistent for all
the channels.
=item C<-magic_open> => I<flag>
When true, channel filenames beginning with '>' or '|' are opened using
Perl's open(). Otherwise, sysopen() is used, in append mode.
Default is false.
=item C<-prefix> => I<prefix>
The application prefix string to prepend to messages.
=item C<-rotate> => I<object>
This sets a default logfile rotation policy. You need to install the
additional C<Log::Agent::Rotate> module to use this switch.
I<object> is the C<Log::Agent::Rotate> instance describing the default
policy for all the channels. Only files which are not opened via a
so-called I<magic open> can be rotated.
=item C<-showpid> => I<flag>
If set to true, the PID of the process will be appended within square
brackets after the prefix, to all messages.
Default is false.
=item C<-stampfmt> => (I<name> | I<CODE>)
Specifies the time stamp format to use. By default, my "own" format is used.
The following formats are available:
date "[Fri Oct 22 16:23:10 1999]"
none
own "99/10/22 16:23:10"
syslog "Oct 22 16:23:10".
You may also specify a CODE ref: that routine will be called every time
we need to compute a time stamp. It should not expect any parameter, and
should return a string.
=back
=head1 CHANNELS
All the channels go to the specified files. If a channel is not configured,
it is redirected to 'error', or STDERR if no 'error' channel was configured
either.
Two channels not opened via a I<magic> open and whose logfile name is the
same are effectively I<shared>, i.e. the same file descriptor is used for
both of them. If you supply distinct rotation policies (e.g. by having a
default policy, and supplying another policy to one of the channel only),
then the final rotation policy will depend on which one was opened first.
So don't do that.
=head1 CAVEAT
Beware of chdir(). If your program uses chdir(), you should always specify
logfiles by using absolute paths, otherwise you run the risk of having
your relative paths become invalid: there is no anchoring done at the time
you specify them. This is especially true when configured for rotation,
since the logfiles are recreated as needed and you might end up with many
logfiles scattered throughout all the directories you chdir()ed to.
Logging channels with the same pathname are shared, i.e. they are only
opened once by C<Log::Agent::Driver::File>. Therefore, if you specify
different rotation policy to such channels, the channel opening order will
determine which of the policies will be used for all such shared channels.
Such errors are flagged at runtime with the following message:
Rotation for 'logfile' may be wrong (shared with distinct policies)
emitted in the logs upon subsequent sharing.
=head1 AUTHORS
Originally written by Raphael Manfredi E<lt>Raphael_Manfredi@pobox.comE<gt>,
currently maintained by Mark Rogaski E<lt>mrogaski@cpan.orgE<gt>.
Thanks to Joseph Pepin for suggesting the file permissions arguments
to make().
=head1 LICENSE
Copyright (C) 1999 Raphael Manfredi.
Copyright (C) 2002 Mark Rogaski; all rights reserved.
See L<Log::Agent(3)> or the README file included with the distribution for
license information.
=head1 SEE ALSO
Log::Agent::Driver(3), Log::Agent(3), Log::Agent::Rotate(3).
=cut
|