Posted on 2006-07-11 22:19:15-07 by vigil
Patch to Log.pm
This patch adds msgPrepend (prepend a string to each msg), and dateFormat (strftime-compatible support for dateTimeStamp).
--- old/Log.pm Mon Jul 10 17:52:15 2006 +++ new/Log.pm Mon Jul 10 17:46:50 2006 @@ -57,6 +57,8 @@ $self->{'storeexptext'} = 0; $self->{'logfiledatetime'} = 0; $self->{'_expCnt'} = 0; + $self->{'msgPrepend'} = ''; + $self->{'dateFormat'} = ''; @@ -191,7 +193,7 @@ my $now = ''; my $pid = ''; - + my $msgprepend = ''; # Do we have enough parameters @_ > 1 or confess 'Usage: log->msg(debugLevel, "message string"|@messageStrings)'; @@ -202,14 +204,28 @@ my $str = join('', @_); # Set the timestamp if required - $now = scalar(localtime()).' ' if ($self->{'datetimestamp'}); + if ($self->{'datetimestamp'}) + { + if ($self->{'dateformat'}) + { + require POSIX; + $now = POSIX::strftime($self->{'dateformat'}, localtime) . ' '; + } + else + { + $now = scalar(localtime()).' '; + } + } # Set the process ID if required - $pid = $$.' ' if ($self->{'pidstamp'}); + $pid = $$ . ' ' if ($self->{'pidstamp'}); + + # Prepend text if necessary + $msgprepend = $self->{'msgprepend'}; # Format the string and print it to the logfile - $str =~ s/\n(?=.)/\n$pid$now/gs; - print {$self->{'_fileHandle'}} "$pid$now".$str; + $str =~ s/\n(?=.)/\n$pid$now$msgprepend/gs; + print {$self->{'_fileHandle'}} $pid, $now, $msgprepend, $str; } @@ -220,6 +236,7 @@ my $now = ''; my $pid = ''; + my $msgprepend = ''; # Do we have enough parameters @_ >= 1 or confess 'Usage: log->msg(debugLevel, "message string"|@messageStrings)'; @@ -230,17 +247,31 @@ my $str = join('', @_); # Set the timestamp if required - $now = scalar(localtime()).' ' if ($self->{'datetimestamp'}); + if ($self->{'datetimestamp'}) + { + if ($self->{'dateformat'}) + { + require POSIX; + $now = POSIX::strftime($self->{'dateformat'}, localtime) . ' '; + } + else + { + $now = scalar(localtime()).' '; + } + } # Set the process ID if required $pid = $$.' ' if ($self->{'pidstamp'}); + # Prepend text if necessary + $msgprepend = $self->{'msgprepend'}; + # Format the string and print it to the logfile - $str =~ s/\n(?=.)/\n** $pid$now/gs; - print {$self->{'_fileHandle'}} "** $pid$now$str"; + $str =~ s/\n(?=.)/\n** $pid$now$msgprepend/gs; + print {$self->{'_fileHandle'}} "** $pid$now$msgprepend$str"; # Append the sting if store mode is true - $_expText .= "** $pid$now$str" if $self->{'storeexptext'}; + $_expText .= "** $pid$now$msgprepend$str" if $self->{'storeexptext'}; } @@ -362,6 +393,7 @@ appName => 'myApplicationName', # The name of the application PIDstamp => 1, # Stamp the log data with the Process ID storeExpText => 1, # Store internally all exp text + msgPrepend => '', # Text to prepend to each message }); # Minimal instance, logfile name based on application name @@ -464,6 +496,14 @@ method. This can be useful if there may be multiple exceptions which you then want to report on ( +other than in the log file) as one text string. +=item msgPrepend + +If anything (default is nothing), prepends its value to the end of each message passed to msg. + +=item dateFormat + +If defined, holds the strftime-compatible format for dateTimeStamp. + =back
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.