package Log::Error; use 5.008004; use strict; use Cwd; use CGI::Carp qw(carpout); use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use vars qw(@EXPORT @ISA $VERSION); require Exporter; @ISA = qw(Exporter); @EXPORT = qw( a_log e_log ); $VERSION = '0.01'; my $localtime = localtime; our $dir; BEGIN { if ($::dir) { $dir = $::dir; } else { $0 =~ '(.*[\\\/])\w+\.\w+$'; $dir = $1; } } # Set up main error log file my $log_file = "e_log.txt"; #Croak will get the line number of the calling program, not the library itself. open(LOG, ">>$dir/$log_file") or croak "Unable to append to error log: $!"; carpout(*LOG); ##### ***subroutine who print in Appplication Specific Error*** sub a_log{ my $a_localtime = localtime; my ($a_log, @data) = @_; #To determine the name of the currently running function, including its package by using the built-in function "caller". my $sub_al = (caller(0))[3]; #Make sure that the filename doesn't have spaces or extensions like exe or bat on it, #otherwise it will create file as app_log.txt instead. unless($a_log=~/^[a-zA-Z_]+\.(?!bat$|exe$)[a-zA-Z_]{3}$/gi) { $a_log="a_log.txt" } open(LOGAPP, ">>$dir$a_log") or croak "Unable to append to $a_log: $!"; #print LOGAPP "[$localtime] $a_log : @data ::::: $!\n"; print LOGAPP "[$a_localtime] $a_log : @data \n"; close LOGAPP or die "Cannot close log file:: $a_log : $!\n"; } sub e_log{ my $e_localtime = localtime; my ($e_log, @e_data) = @_; #To determine the name of the currently running function, including its package by using the built-in function "caller". my $sub_el = (caller(0))[3]; #Make sure that the filename doesn't have spaces or extensions like exe or bat on it, #otherwise it will create file as app_log.txt instead. unless($e_log=~/^[a-zA-Z_]+\.(?!bat$|exe$)[a-zA-Z_]{3}$/gi) { $e_log="errorapp_log.txt" } open(ERRAPPLOG, ">>$dir$e_log") or croak "Unable to append to $e_log: $!"; print ERRAPPLOG "[$e_localtime] $e_log : @e_data\n"; close ERRAPPLOG or die "Cannot close log file:: $e_log : $!\n"; } 1; __END__ =head1 DESCRIPTION Errors trapped by -w (Warning) will issue a warning and report the line number of the error. Die will cause the program to die and report the line number of the error. All the errors will be printed to e_log.txt in the same directory where the program that caused the error is running. Called like: &e_log('my_file.txt','Oh no, $hit hit the fan.');