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,
new_perl_userAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
point_pleasantConnect With a Mentor Commented:
Where did you put the check; see below I put outside the while loop



#!/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;  
 
}
close(LOG);
unlink "/usr/qasystem_test/$logFilename" if -s "/usr/qasystem_test/$logFilename" == 0
0
 
point_pleasantCommented:
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.


0
 
wilcoxonCommented:
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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mwochnickCommented:
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?
0
 
new_perl_userAuthor Commented:
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.
0
 
wilcoxonCommented:
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;
}
0
 
point_pleasantCommented:
try this

close(FILE_HANDLE_FOR_FILE);
unlink  "/path/to/logfile" if -s $logfilename == 0
0
 
point_pleasantCommented:
the above closes the file and removes (unlink) if empty
0
 
tsmgeekCommented:
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
0
 
new_perl_userAuthor Commented:

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

0
 
wilcoxonConnect With a Mentor Commented:
So, given your code, my solution would look like...
#!/share_nfs/perl/5.8.8/bin/perl
#use strict;
use DBI;
#use File::Copy qw(cp);

# removed extra quote from the next line
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 @log;

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'};

    push @log, $id;

# 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;

}

if (@log) {
    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");
    print LOG join("\n", @log), "\n";
    close LOG;
}

Open in new window

0
 
new_perl_userAuthor Commented:
I placed this code after open log statement. But after your correction it is working now.

Thanks,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.