Is there an easy way to time tag errors going to stderr? In order to troubleshoot problems, I need to know when an error occurred.
Example:
Dec 10 12:00:00 Can't call method "str" on an undefined value
Thanks!
Define custom handlers for handling warnings and fatal errors:
use strict;
use warnings;
$SIG{__WARN__} = sub { warn sprintf("[%s] ", scalar localtime), @_ };
$SIG{__DIE__} = sub { die sprintf("[%s] ", scalar localtime), @_ };
warn;
die;
Output:
[Fri Dec 11 14:35:37 2009] Warning: something's wrong at c:\temp\foo.pl line 7.
[Fri Dec 11 14:35:37 2009] Died at c:\temp\foo.pl line 8.
You might want to use gmtime
instead of localtime
.
__WARN__
handlers only catch warnings. They don't catch print STDERR
. If you want to catch everything, you have to tie STDERR.
#!/usr/bin/perl -w
package Tie::Handle::Timestamp;
use strict;
use warnings;
use base 'Tie::Handle';
sub stamp {
return scalar localtime() .": ";
}
sub TIEHANDLE {
my $class = shift;
my $handle = shift;
# Dup the filehandle otherwise we'll be calling ourself
open my $fh, ">&", $handle or die $!;
return bless $fh, $class;
}
sub PRINT {
my $self = shift;
print { $self } $self->stamp, @_;
}
sub PRINTF {
my $self = shift;
my $format = shift;
printf { $self } $self->stamp . $format, @_;
}
sub CLOSE {
my $self = shift;
close $self;
}
package main;
# Tie it at compile time to catch compile time warnings
BEGIN {
tie *STDERR, "Tie::Handle::Timestamp", *STDERR or die;
}
print STDERR "Foo\n";
warn "Bar";
undef == 2;
This has its own perils as you're now relying on how good your tie implementation is. I'd be surprised there's not already a CPAN module for this, but I don't know of any one I could recommend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With