Link to home
Start Free TrialLog in
Avatar of new_perl_user
new_perl_user

asked on

Log File Issue

Hi,
 One of my perlscript is performing some task and generates a log file with required data in it.

But when it executes if there is no data still it generates an empty file.  So is there a way to stop generating this empty file.

Thanks,
Avatar of point_pleasant
point_pleasant
Flag of United States of America image

without seeing the code you could probably close the file, then check for zero length and remove if empty.
or
you could write the oupput to an array and before you open the log file check to see if the array is empty if so don't write a log file otherwise write the array to the logfile.


Avatar of wilcoxon
Yes.  It certainly can be done.  How it is done depends on how your code is written.  We can't give any details without knowing what you have done.
hard to answer without seeing the code - but the short answer is yes.  are you using a logging framework like log4perl or something custom built?
Avatar of new_perl_user
new_perl_user

ASKER

If possible can you give me a code snippet for "you could write the oupput to an array and before you open the log file check to see if the array is empty if so don't write a log file otherwise write the array to the logfile."

The script task is it reads a table in the database and based on condition in query it picks up the records and print them to a log file.DB.log

Right now when there are no records from the query result  it still generates and empty DB.log.
my @lines;
while ($whatever) {
    push @lines, "whatever you want to output";
}
if (@lines) {
    open OUT, '>output_file' or die "could not write output_file: $!";
    print OUT join("\n", @lines), "\n";
    close OUT;
}
try this

close(FILE_HANDLE_FOR_FILE);
unlink  "/path/to/logfile" if -s $logfilename == 0
the above closes the file and removes (unlink) if empty
look here

http://www.perlhowto.com/dbi_get_the_number_of_rows_affected_by_a_sql_statement

you can then check the number of rows returned then decide if you want to create the log file or exit the script

I tried  both the solutions (wilcoxon,point pleasant) but did not work. I am copying the current working script.

#!/share_nfs/perl/5.8.8/bin/perl
#use strict;
use DBI;
#use File::Copy qw(cp);


my $db = DBI->connect( "dbi:Oracle:"", "", "" )
    || die( $DBI::errstr . "\n" );
$db->{AutoCommit}    = 0;
$db->{RaiseError}    = 1;
$db->{ora_check_sql} = 0;
$db->{RowCacheSize}  = 16;




my($sec, $min, $hour, $mday, $mon, $year) = localtime;
my $logFilename = sprintf("QAmanifest_%d%02d%02d%02d%02d%02d_mhl.txt", 1900+$year, $mon+1, $mday, $hour, $min, $sec);
open(LOG, ">/usr/qasystem_test/$logFilename");


my $SEL = $db->prepare("SELECT SEQUENCE_NUMBER,NUMBER,ID FROM stage where status = 'P'");
$SEL->execute();
while(my $subref = $SEL->fetchrow_hashref()) {
    my $number=$subref->{'NUMBER'};
    my $id=$subref->{'ID'};
    my $sequence_number=$subref->{'SEQUENCE_NUMBER'};

print LOG "$id \n";

# replace $path with the path to the dir structures

my $path = "/usr/qa/TEST/$number/$id";
my $dest = "/usr/qa/output_test";

print "Content-type: text/htmlnn";
`cp -R $path $dest`;



my $sth = $db->prepare("UPDATE info set status = 14  where id = '$id'");
  $sth->execute() or die $DBI::errstr;
  print "Number of rows updated :" + $sth->rows;
  $sth->finish();
  $db->commit or die $DBI::errstr;

my $sth = $db->prepare("Insert into History(sequence_number,date,id,status)
values($sequence_number,sysdate,'QA',14)");
  $sth->execute() or die $DBI::errstr;
 print "Number of rows updated :" + $sth->rows;
  $sth->finish();
  $db->commit or die $DBI::errstr;


my $sth = $db->prepare("delete from stage where unique_id ='$id'");
  $sth->execute( ) or die $DBI::errstr;
 print "Number of rows deleted :" + $sth->rows;
  $sth->finish();
  $db->commit or die $DBI::errstr;

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of point_pleasant
point_pleasant
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I placed this code after open log statement. But after your correction it is working now.

Thanks,