Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Logfile name hack

Reply
Thread Tools

Logfile name hack

 
 
magoo
Guest
Posts: n/a
 
      07-02-2004

Just got through writing a program which wrote some output to a logfile.

Not wanting to overwrite a previous log file, I threw this together:
#================================================= ===============

$logfile_version = 1;
$logfile = "${dir_name}/logfile_v${logfile_version}.log";

#Pre-existing logfile?
while ( -e $logfile ) {
($logfile_version = $logfile) =~ s/\D//g;
$logfile_version += 1;
$logfile = "${dir_name}/logfile_v${logfile_version}.log";
}

open(LOGFILE, ">$logfile) or die "Blah, blah, blah...";

#================================================= ===============

This creates a series of logfiles named:
logfile_v1.log
logfile_v2.log
etc

I would be interested in hearing any input concerning creating,
using, logfiles that anyone might want to share.

Regards,
magoo




 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-02-2004
On Fri, 2 Jul 2004, magoo wrote:

> Just got through writing a program which wrote some output to a logfile.
>
> Not wanting to overwrite a previous log file, I threw this together:
> #================================================= ===============
>
> $logfile_version = 1;
> $logfile = "${dir_name}/logfile_v${logfile_version}.log";


They don't hurt, but the { } are unnecessary. Neither / nor . are valid
variable name characters.

>
> #Pre-existing logfile?
> while ( -e $logfile ) {
> ($logfile_version = $logfile) =~ s/\D//g;


I don't quite understand why you're doing this. You are speciifically
creating the $logfile string. You know what it looks like. You know what
it contains. In every iteration, you're assigning $logfile_version to
exactly what it already is.

This could, in fact, cause a bug in your algorithm, should $dir_name ever
contain any digits.


> $logfile_version += 1;
> $logfile = "${dir_name}/logfile_v${logfile_version}.log";
> }
>
> open(LOGFILE, ">$logfile) or die "Blah, blah, blah...";
>
> #================================================= ===============
>
> This creates a series of logfiles named:
> logfile_v1.log
> logfile_v2.log
> etc


> I would be interested in hearing any input concerning creating,
> using, logfiles that anyone might want to share.


Out of curiousity, what do you want to happen if the directory contains
the two files:

logfile_v1.log
logfile_v3.log

Your method will create a new file logfile_v2.log. This may be what you
want. If it's not, you'll have to change your algorithm a bit. Or, you
may decide this isn't a possibility, that you'll never remove a log file
once it exists. That's for you to decide, of course.

Paul Lalli
 
Reply With Quote
 
 
 
 
Daedalus
Guest
Posts: n/a
 
      07-02-2004
> Just got through writing a program which wrote some output to a logfile.
>
> Not wanting to overwrite a previous log file, I threw this together:
> #================================================= ===============
>
> $logfile_version = 1;
> $logfile = "${dir_name}/logfile_v${logfile_version}.log";
>
> #Pre-existing logfile?
> while ( -e $logfile ) {
> ($logfile_version = $logfile) =~ s/\D//g;
> $logfile_version += 1;
> $logfile = "${dir_name}/logfile_v${logfile_version}.log";
> }
>
> open(LOGFILE, ">$logfile) or die "Blah, blah, blah...";
>
> #================================================= ===============
>
> This creates a series of logfiles named:
> logfile_v1.log
> logfile_v2.log
> etc
>
> I would be interested in hearing any input concerning creating,
> using, logfiles that anyone might want to share.


Could use the current date to make different log files name (if you want
several logs per days maybe date-time) Code would looks like this:

open DATE, "date|" or die "Failed to open DATE: $!";
(my $date = <DATE>) =~ s/[^\d]//g;
my $dir_name = "/your/log/dir/";
my $logfile = "${dir_name}log-$date.log";
open LOGFILE, ">>$logfile" or die "Failed to open LOGFILE: $!";

It could be useful to have log files named by date (or date-time).

DAE


 
Reply With Quote
 
Daedalus
Guest
Posts: n/a
 
      07-02-2004
> > This creates a series of logfiles named:
> > logfile_v1.log
> > logfile_v2.log
> > etc
> >
> > I would be interested in hearing any input concerning creating,
> > using, logfiles that anyone might want to share.


Anyway if you want the program to create a new log file each it runs, here's
an simple example that would more simply do what you initialy wanted:

my $dir_name = "/your/log/dir/";
my $file_name = "logfile_v";
my $version = 1;
$version++ while -e "$dir_name$filename$version.log";
my $logfile = "$dir_name$filename$version.log";
open LOGFILE, ">$logfile" or die "Failed to open LOGFILE: $!";

This would create : logfile_v1 then logfile_v2 and so on each time it runs.

DAE


 
Reply With Quote
 
magoo
Guest
Posts: n/a
 
      07-02-2004
In article <GCgFc.33804$(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> > > This creates a series of logfiles named:
> > > logfile_v1.log
> > > logfile_v2.log
> > > etc
> > >
> > > I would be interested in hearing any input concerning creating,
> > > using, logfiles that anyone might want to share.

>
> Anyway if you want the program to create a new log file each it runs, here's
> an simple example that would more simply do what you initialy wanted:
>
> my $dir_name = "/your/log/dir/";
> my $file_name = "logfile_v";
> my $version = 1;
> $version++ while -e "$dir_name$filename$version.log";
> my $logfile = "$dir_name$filename$version.log";
> open LOGFILE, ">$logfile" or die "Failed to open LOGFILE: $!";
>
> This would create : logfile_v1 then logfile_v2 and so on each time it runs.
>
> DAE
>
>
>

After much consideration, I am going to go with this now:

chomp($date = `date`);
$date =~ s/\s+/_/g;
$logfile = $my_directory/logfile.$date;

output file now is named somthing like:
logfile.Fri_Jul_2_13:12:57_CDT_2004

Simplicity trumps complexity every time...

 
Reply With Quote
 
Daedalus
Guest
Posts: n/a
 
      07-02-2004
> After much consideration, I am going to go with this now:
>
> chomp($date = `date`);
> $date =~ s/\s+/_/g;
> $logfile = $my_directory/logfile.$date;


Anyway your date format is different than the one I got on a w2k box, wich
is something like : Fri. 02-07-2004.
so the line : (my $date = `DATE`) =~ s/[^\d]//g; wich give to $date:
"02072004", wouldn't work as is on you *nix box.

> output file now is named somthing like:
> logfile.Fri_Jul_2_13:12:57_CDT_2004


Sounds good... *nix don't mind to have colons ( in filenames. But this
wouldn't work under a windows or a Mac box since the colons have special
path meanings under these OS. You see if you want your code to be the more
portable possible.

> Simplicity trumps complexity every time...


Yep, as long as the simple gives you what you're looking for.

DAE





 
Reply With Quote
 
J. Romano
Guest
Posts: n/a
 
      07-02-2004
magoo <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)-pc.org>...
> Just got through writing a program which wrote some output to a logfile.
>
> Not wanting to overwrite a previous log file, I threw this together:

<code snipped>
>
> This creates a series of logfiles named:
> logfile_v1.log
> logfile_v2.log
> etc
>
> I would be interested in hearing any input concerning creating,
> using, logfiles that anyone might want to share.


That's a cool hack, but you can shorten it (to basically one line)
with the following:

# Specify logfile name:
$logfile = "logfile_v1.log";

# Loop until we find a log file name that is not used yet:
$logfile =~ s/(\d+)/$1+1/e while (-e $logfile);

# Open log file:
open(LOGFILE, "> $logfile") or die "Cannot open $logfile: $!";

Anyway, thanks for sharing your hack, magoo.

-- Jean-Luc
 
Reply With Quote
 
Tintin
Guest
Posts: n/a
 
      07-02-2004

"magoo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)-pc.org...
> In article <GCgFc.33804$(E-Mail Removed)>,
> After much consideration, I am going to go with this now:
>
> chomp($date = `date`);
> $date =~ s/\s+/_/g;
> $logfile = $my_directory/logfile.$date;
>
> output file now is named somthing like:
> logfile.Fri_Jul_2_13:12:57_CDT_2004


I would suggest that a better (and certainly more portable) way is:

use POSIX 'strftime';
my $logfile = strftime("logfile-%Y-%m-%d",localtime);


 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-03-2004
(E-Mail Removed) (J. Romano) wrote in
news:(E-Mail Removed) om:

> magoo <(E-Mail Removed)> wrote in message
> news:<(E-Mail Removed)-pc.org>...
>> Just got through writing a program which wrote some output to a
>> logfile.
>>
>> Not wanting to overwrite a previous log file, I threw this together:

> <code snipped>
>>
>> This creates a series of logfiles named:
>> logfile_v1.log
>> logfile_v2.log
>> etc
>>
>> I would be interested in hearing any input concerning creating,
>> using, logfiles that anyone might want to share.

>
> That's a cool hack, but you can shorten it (to basically one line)
> with the following:
>
> # Specify logfile name:
> $logfile = "logfile_v1.log";
>
> # Loop until we find a log file name that is not used yet:
> $logfile =~ s/(\d+)/$1+1/e while (-e $logfile);
>
> # Open log file:
> open(LOGFILE, "> $logfile") or die "Cannot open $logfile: $!";
>
> Anyway, thanks for sharing your hack, magoo.


Given the race condition here, it would probably be a good idea to use
sysopen with O_EXCL.


--
A. Sinan Unur
(E-Mail Removed) (reverse each component for email address)
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      07-03-2004
magoo wrote:

> After much consideration, I am going to go with this now:
> chomp($date = `date`);


Why are you calling an external program to do that?
Using localtime() in scalar context returns everything but the timezone.
$date = localtime();

-Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Logfile name format in logging Muruga Raj Ruby 3 12-24-2008 03:40 AM
how to find the "yesterday" logfile name? robertchen117@gmail.com Perl Misc 5 03-03-2007 02:44 PM
Logfile analysis API Andreas Java 0 05-10-2005 06:53 PM
How to access logfile directory path from IIS =?Utf-8?B?TXVubnU=?= ASP .Net 0 04-22-2005 04:34 PM
Logfile Nick Withers Computer Support 1 11-18-2003 11:35 AM



Advertisments