Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write error to log file

Status
Not open for further replies.

sun9

Programmer
Dec 13, 2006
31
US
hi,

I have a script that opens a file and throws an error to the standard output if the file does not exist, how do i get the error to be written into a log file in the same directory as the script? Also how do I go about checking if the file contains no data?

open(FILE, $file)|| die("$file\n$!, Cannot create files without test.txt\n");

Thanks.
 
I usually have a log subroutine..
something like:

open(FILE, $file)|| &log("$file $!", "exit");

sub log {
$logentry = shift @_;
$status = shift @_;
$time = localtime;
open(LOG, ">>$log") || die "$log can't be appended to: $!\n";
print LOG "$time: $logentry\n";
close LOG;
if ($status eq "exit") {
exit;
}
}
 
Thanks travs69 that helped.
 
For a comprehensive logging solution, check out log4perl, a port of the Apache log4j (Java) utility. also available from CPAN...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top