Solved

Log File Issue

Posted on 2011-03-23
12
307 Views
Last Modified: 2012-05-11
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,
0
Comment
Question by:new_perl_user
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35200877
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35200889
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
 
LVL 12

Expert Comment

by:mwochnick
ID: 35200912
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:new_perl_user
ID: 35200919
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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35200968
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
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35201012
try this

close(FILE_HANDLE_FOR_FILE);
unlink  "/path/to/logfile" if -s $logfilename == 0
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35201035
the above closes the file and removes (unlink) if empty
0
 
LVL 5

Expert Comment

by:tsmgeek
ID: 35201464
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
 

Author Comment

by:new_perl_user
ID: 35202051

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
 
LVL 8

Accepted Solution

by:
point_pleasant earned 250 total points
ID: 35202159
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
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 250 total points
ID: 35202356
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
 

Author Comment

by:new_perl_user
ID: 35202412
I placed this code after open log statement. But after your correction it is working now.

Thanks,
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question