write it out to a log file

At my current job I don't have the luxury of being able to install log4perl.

So I wrote my own. Here it is.


 

#!c:\perl5.8.6\bin\perl
use strict;
use Data::Dumper;

# print level

my $printLevel = {
'fatal' => '0',
'warn' => '1',
'error' => '1',
'info' => '2',
'debug' => '3',
'verbose' => '4',
'vverbose' => '5',
};
 
# set the max level my
$myPrintLevel = '3';
 
my $log = &createLog;
 
&writeLog( "created log" );
 
sub createLog {
my $script = $0;
$script =~ s/.pl/.log/;
$script = "log_" .
$script;
my $logfile = $script;
my $date = &date;
open( $log, ">$logfile" ) or die "$logfile: $!";
print "$date : info : initialize \n";
print $log "$date : info : init \n";
return $log;
}
sub date {
my $date = strftime '%m%d%Y %H:%M:%S', localtime;
return $date;

}

sub closeLog {

my $date = &date;
print "$date : END : The End \n";
print $log "$date : END : The End \n";
close $log; exit;

}

sub writeLog {

my $message = shift(@_);
my $level = shift(@_);
 
# if level is not defined
unless ($level) { $level = 'info'; }
 
# return if the print level is below the level
if ( $printLevel->{$level} > $myPrintLevel ) {
return;
}
 
my $date = &date;
chomp($message);
 
$message = $message . "\n";
 
my $sep = " : ";
 
printf "%18s %3s %5s %3s %s", $date , $sep , $level, $sep, $message ;
printf $log "%18s %3s %5s %3s %s", $date , $sep , $level, $sep, $message ;
 
if ( $level eq 'fatal' ) { &closeLog; }

} ## end sub writeLog