Set a Logging Variable
my $logging=1
Conditionally Open and Close the Logfile
At the earliest reasonable time, open the logfile in append mode:if($logging){open FILE , ">>./my.log";}At the latest possible time, close the logfileif($logging){close FILE};Don'tforget to close the logfile as part of your error handling routines as
well. Perl cleans things up nicely for you, but there are always
exceptions to those kinds of rules.
Logging Subroutine
A simple subroutine to enable logging as follows:sub logit{
if ($logging){
print FILE "$_[0]\n";
}
}Itprints whatever it receives in the first parameter to the log file. If
you need to print something simple, just call "logit('text to log');"
and your log entry will be created.
Source : http://www.stevekallestad.com/blog/simple_logging_in_perl.html


No comments:
Post a Comment