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
use strict; use warnings; package TeeStdErr; use Fcntl qw(:flock); my $log_file = "$ENV{DOCUMENT_ROOT}/state/err.log"; open (my $orig_stderr, '>&', \*STDERR); sub TIEHANDLE { my $i; bless \$i, shift } sub PRINT { my $r = shift; print $orig_stderr @_; if ( open my $FH, '>>', $log_file ) { flock $FH, LOCK_EX; print $FH "" . localtime, "\t", "ErrorLog: ", @_; close $FH; } else { warn "Open $log_file '$!'", @_; } } package main; $| = 1; # Tee für STDERR tie *STDERR, "TeeStdErr"; print STDERR "print STDERR\n"; warn "warn";
QuoteFor security reasons, starting with version 2.0.46, non-printable and other special characters in %r, %i and %o are escaped using \xhh sequences, where hh stands for the hexadecimal representation of the raw byte. Exceptions from this rule are " and \, which are escaped by prepending a backslash, and all whitespace characters, which are written in their C-style notation (\n, \t, etc). In versions prior to 2.0.46, no escaping was performed on these strings so you had to be quite careful when dealing with raw log files.