Re: how to log a process
by toolic (Bishop) on May 27, 2009 at 17:16 UTC
|
I am unable to get the logging to work.
You did not specify how it is not behaving as you expect. Also, I do not think you posted the actual code you are running since it does not compile for me: you must declare the $logfile variable. My guess is that the logging is not working properly because you open a filehandle named FILE for appending, but you print to a different filehandle, named LOGFILE. You should use warnings; to catch errors like that.
| [reply] [d/l] [select] |
|
I have changed it like this
#!/usr/local/bin/perl
# 2592000 = 30 day n min (60 *60 * 24 *30 )
use strict;
use POSIX;
use warnings;
# change the below to dlt - dl30
my $logfile = "/script_logs/30DyDlt.txt";
my $date = localtime();
my $dir = '/oven_web/iportal/cgi-bin/uploads';
opendir(DIR,$dir) || die "Can't open $dir : $!\n";
my @files = readdir(DIR); # you may want to grep only certain files he
+re
close(DIR);
### my $logmsg = $date/$dir/$file;
#print $logmsg = $date/$dir/$file;
open LOGFILE, ">> $logmsg " or die " cannot open logfie $logfile for a
+ppend $!";
foreach my $file(@files)
{
my $now = time;
my @stat = stat("$dir/$file");
my $logmsg = $date/$dir/$file;
if ($stat[9] < ($now - 2592000))
{
print "Deleting $dir/$file...";
#unlink("$dir/$file");
#print "Done.\n";
print LOGFILE $logmsg, "\n";
}
}
close LOGFILE;
It does not compile for me iether...
the error is;
/usr/bin/perl /oven_script/tst/dlt30day-oven1d-j2.pl
Global symbol "$logmsg" requires explicit package name at /oven_script/tst/dlt30day-oven1d-j2.pl line 21.
Execution of /oven_script/tst/dlt30day-oven1d-j2.pl aborted due to compilation errors.
---
I was thinking ( kinda) that i could append to a txt file the things I see if i just do a
print "Deleting $dir/$file..."
if I comment that (#)
and uncomment the unlink("$dir/$file");
the older files really do go away...
maybe it is harder than I thought...
this does work
#!/usr/local/bin/perl
# 2592000 = 30 day n min (60 *60 * 24 *30 )
use strict;
# change the below to dlt -
my $dir = '/oven_web/iportal/cgi-bin/uploads';
opendir(DIR,$dir) || die "Can't open $dir : $!\n";
my @files = readdir(DIR); # you may want to grep only certain files he
+re
close(DIR);
foreach my $file(@files)
{
my $now = time;
my @stat = stat("$dir/$file");
if ($stat[9] < ($now - 2592000))
{
#print "Deleting $dir/$file...";
unlink("$dir/$file");
#print "Done.\n";
}
}
| [reply] [d/l] [select] |
|
So compile error says that it doesn't like print LOGFILE $logmsg, "\n";The actual trouble may be with this: my $logmsg = $date/$dir/$file; because that looks like a division arithmetic expression. I would suggest changing:
my $logmsg = "$date/$dir/$file";
print LOGFILE "$logmsg \n";
Update:
I looked again and you've got open LOGFILE, ">> $logmsg " or die ".." I think you meant: open LOGFILE, ">>$logfile" or ... ?I don't see any problem with the logging idea. Opening a file for append is a very normal thing to do. If you are the only user of this file, this approach will work great. Nothing fancy needed. Get the code to compile and run, then report the run-time errors. | [reply] [d/l] [select] |
|
|
|
|
Re: how to log a process
by apl (Monsignor) on May 27, 2009 at 20:17 UTC
|
| [reply] |
|
Thanks for the reply.
I looked at that link -- and looke at the examples
so -- would you say that i could accomplish what i am trying to do with
$log->info
like
$log->info($date/$dir/$file);
am i headed in the right direction? | [reply] [d/l] [select] |
|
I think you've got it (though I believe you need to wrap the argument with double-quotes).
| [reply] |
|
This use of log handler is intersting -- i hadnot heard of it before -- I cahnged my script thussly:
#!/usr/local/bin/perl
# now WITH Log Handler
# 2592000 = 30 day n min (60 *60 * 24 *30 )
use strict;
use POSIX;
use warnings;
use Log::Handler::Simple;
my $log = Log::Handler::Simple->new(
filename => '/script_logs/30DyDlt-LOG.txt',
mode => 'append',
newline => 1,
maxlevel => 7,
minlevel => 0
);
# change the below to dlt - dl30
## my $logfile = "/script_logs/30DyDlt-LOG.txt";
my $date = localtime();
my $dir = '/oven_web/iportal/cgi-bin/uploads';
opendir(DIR,$dir) || die "Can't open $dir : $!\n";
my @files = readdir(DIR); # you may want to grep only certain files he
+re
close(DIR);
### my $logmsg = $date/$dir/$file;
###my $logmsg = "$date/$dir/$file";
$log->info
#print $logmsg = $date/$dir/$file;
open LOGFILE, ">> $logmsg " or die " cannot open logfie $logfile for a
+ppend $!";
foreach my $file(@files)
{
my $now = time;
my @stat = stat("$dir/$file");
if ($stat[9] < ($now - 2592000))
{
print "Deleting $dir/$file...";
#unlink("$dir/$file");
#print "Done.\n";
$log->info("$date/$dir/$file");
}
}
close LOGFILE;
i just noted i - likely need to snip off :: close LOGFILE
but that is not my - real problem . Of course I have not done it correctly . and it "lOOks like my Perl is ' missing ' something.
an attempt to run blows up like this.
.
/usr/bin/perl /oven_script/tst/dlt30day-oven1d-j4.pl
Can't locate Log/Handler/Simple.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at /oven_script/tst/dlt30day-oven1d-j4.pl line 7.
BEGIN failed--compilation aborted at /oven_script/tst/dlt30day-oven1d-j4.pl line 7.
well.
it is a new record for me on total numberof chars in an error --- so that much was fun!
| [reply] [d/l] |
|
Re: how to log a process
by Marshall (Canon) on May 27, 2009 at 23:48 UTC
|
I guess you got this code from somewhere else. This comment is on target....A directory is just a file. You will need to filter those out. -f option will get non-directory files. Also note that readdir only returns a filename, not a full path. when doing operations on that file, you need to supply the path to the DIR.
my @files = readdir(DIR); # you may want to grep only certain files he
+re
#I think you need something like:
my @files = grep{-f "$dir/$_"} readdir(DIR);
I didn't mean to be impolite and I have revised my text. No offense was intended. The #comment in the poster's own code was correct. The poster will need to use a filter to get the "simple files" from the DIR. | [reply] [d/l] |
|
Oh... i did not take it as impolite.
it is true that I did get the code elsewhere.
and -- if you haven't guessed -- i am no 'very good'
my level of understanding is one of welding together the wisdom of others...
as is the case here.
I attempted to integrate your wisdom
#!/usr/local/bin/perl
# 2592000 = 30 day n min (60 *60 * 24 *30 )
use strict;
use POSIX;
use warnings;
# change the below to dlt - dl30
my $logfile = "/script_logs/30DyDlt-LOG.txt";
my $date = localtime();
my $dir = '/oven_web/iportal/cgi-bin/uploads';
opendir(DIR,$dir) || die "Can't open $dir : $!\n";
#my @files = readdir(DIR); # you may want to grep only certain files h
+ere
my @files = grep{-f "$dir/$_"} readdir(DIR);
close(DIR);
### my $logmsg = $date/$dir/$file;
my $logmsg = "$date/$dir/$file";
#print $logmsg = $date/$dir/$file;
open LOGFILE, ">> $logmsg " or die " cannot open logfie $logfile for a
+ppend $!";
foreach my $file(@files)
{
my $now = time;
my @stat = stat("$dir/$file");
if ($stat[9] < ($now - 2592000))
{
print "Deleting $dir/$file...";
#unlink("$dir/$file");
#print "Done.\n";
print LOGFILE "$logmsg \n";
}
}
close LOGFILE;
that blew up with
/usr/bin/perl /oven_script/tst/dlt30day-oven1d-j3.pl
Global symbol "$file" requires explicit package name at /oven_script/tst/dlt30day-oven1d-j3.pl line 16.
Execution of /oven_script/tst/dlt30day-oven1d-j3.pl aborted due to compilation errors.
But -- thanks!
| [reply] [d/l] [select] |
|
Ok, here is a suggestion (not tested with data):
First, open the files and directories that you need so that this is all in one place at the beginning. I mean if this was a huge calculation monster, you wouldn't want it to run for an hour only to find out that you couldn't open the file to write the data to! That's a style thing for future reference.
File and directory handles will close when the program exits. This program will run in far less than one second and is just ~20 lines. Don't worry about close($x).
#!/usr/bin/perl -w
use strict;
my $logfile = "script_logs/30DyDlt-LOG.txt";
open (LOGFILE, ">>$logfile") or die "Cannot open logfile $logfile for
+append $!";
my $dir = 'oven_web/iportal/cgi-bin/uploads';
opendir(DIR,$dir) || die "Can't open directory $dir $!";
my @files = grep{-f "$dir/$_"} readdir(DIR);
my $current_date_time = time(); #this is seconds +- epoch value!
my $thirty_days = 60 * 60 * 24 *30; #num seconds in 30 days
foreach my $file(@files)
{
my $file_time = (stat("$dir/$file"))[9];
if ( $file_time < ($current_date_time - $thirty_days) )
{
print "Deleting $dir/$file...\n"; #for the user on terminal
unlink("$dir/$file") or die "unable to unlink file $file in $dir $
+!";
print LOGFILE "$dir/$file deleted\n";
}
}
Update:
could be better: have fun!
foreach $file (readdir D)
{
if ((-M "$dir/$file" > 30) && !unlink("$dir/$file"))
{
print "Oh no!!!!!\n";
}
# else it worked ###
}
| [reply] [d/l] [select] |
|
|
|