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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send Errors to a File

Status
Not open for further replies.

rbri

Programmer
Jun 27, 2002
84
US
Hello Everyone

Does anyone know if there is a way to specify a file to send errors to rather than the screen in the die function.
I wrote a script to switch out report logs in a cron right now though all error messages go to the terminal. I would like to send them to a file that I could check to see if everything went ok. The die function is the only way I easily to capture errors but, it seem that you can only send errors to the terminal. Any help will be greatly appreciated.

Thanks Randy
 
Hello Randy,

When you start your Perl script - do this

perl_script.pl 2>/tmp/my_error_file.txt

This sends die output - and everything else that comes out on STDERR - to the file /tmp/my_error.txt

Mike

To err is human,
but to really foul things up -
you require a man Mike.

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
You could write the error to a sub routine and then die within that.

Code:
#!c:\Perl

$string = "Error found";

if ($string eq "Error found")  {
    die write_log("ERROR:Write Your message here");
}


sub write_log  {
	
        $logfile = 'c:/tmp/LIB/my_error_file.txt';
	&get_date;
	open(LOG,">".$logfile);
	print LOG "\n\n".$logdate." ".$hour.":".$min.":".$sec." ".$$."\n";
	foreach $value(@_)  {
		print LOG "\t".$value."\n";
		print "\n\t".$value."\n";
	}
	close LOG;
	
}


sub get_date  {

	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	@days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	@months = ('January','February','March','April','May','June','July','August','September','October','November','December');
	if ($sec < 10) {
		$sec = "0$sec";
	}
	if ($min < 10) {
		$min = "0$min";
	}
	if ($hour < 10) {
		$hour = "0$hour";
	}
	$mon = ($mon + 1);
	$year = $year + 1900;
	
	if (substr($mday, 1, 1) eq "" && $mday < 10)  {  $d = "0".$mday;  }  else  {  $d = $mday;  }
	if (substr($mon, 1, 1) eq "" && $mon < 10)  {  $m = "0".$mon;  }  else  {  $m = $mon;  }
	
	$logdate = $year.$m.$d;
	$date = $year.$m.$d;

}

Rob Waite
 
look up the select function and you can point all output to a log file, STDERR, and STDOUT

--Paul
 
Thanks everyone for your answers they helped out alot.
 
The code should like something like below:

Code:
$sterr = *STDERR;
open(STDERR, "> stderr.txt");
select(STDERR);
$| = 1;
close(STDERR);
STDERR = $$stderr;

Not positive about the last line???


Michael Libeson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top