Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Log File Issue

Posted on 2011-03-23
12
Medium Priority
?
332 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 27

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 27

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 1000 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 27

Assisted Solution

by:wilcoxon
wilcoxon earned 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

782 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