Logfile name hack

M

magoo

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
 
P

Paul Lalli

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
 
D

Daedalus

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
 
D

Daedalus

This creates a series of logfiles named:
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
 
M

magoo

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...
 
D

Daedalus

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
 
J

J. Romano

magoo said:
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:
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
 
T

Tintin

magoo said:
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);
 
A

A. Sinan Unur

(e-mail address removed) (J. Romano) wrote in
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.
 
J

Joe Smith

magoo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top