Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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 librar +y 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 it +s 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 ex +e 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 it +s 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 ex +e 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 lin +e number of the error. Die will cause the program to die and report the line number of the er +ror. All the errors will be printed to e_log.txt in the same directory wher +e the program that caused the error is running. Called like: &e_log('my_file.txt','Oh no, $hit hit the fan.');
2006-05-03 Retitled by Arunbear, as per Monastery guidelines
Original title: 'Module Question'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Logging module sometimes writes its file in the wrong directory
by davidrw (Prior) on May 03, 2006 at 15:56 UTC | |
by Anonymous Monk on May 03, 2006 at 16:55 UTC | |
by CountOrlok (Friar) on May 03, 2006 at 17:33 UTC | |
|
Re: Logging module sometimes writes its file in the wrong directory
by bart (Canon) on May 04, 2006 at 14:56 UTC |