Logfile name hack

Discussion in 'Perl Misc' started by magoo, Jul 2, 2004.

  1. magoo

    magoo Guest

    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
     
    magoo, Jul 2, 2004
    #1
    1. Advertising

  2. magoo

    Paul Lalli Guest

    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
     
    Paul Lalli, Jul 2, 2004
    #2
    1. Advertising

  3. magoo

    Daedalus Guest

    > 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
     
    Daedalus, Jul 2, 2004
    #3
  4. magoo

    Daedalus Guest

    > > 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
     
    Daedalus, Jul 2, 2004
    #4
  5. magoo

    magoo Guest

    In article <GCgFc.33804$>,
    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...
     
    magoo, Jul 2, 2004
    #5
  6. magoo

    Daedalus Guest

    > 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
     
    Daedalus, Jul 2, 2004
    #6
  7. magoo

    J. Romano Guest

    magoo <> wrote in message news:<-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
     
    J. Romano, Jul 2, 2004
    #7
  8. magoo

    Tintin Guest

    "magoo" <> wrote in message
    news:-pc.org...
    > In article <GCgFc.33804$>,
    > 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);
     
    Tintin, Jul 2, 2004
    #8
  9. (J. Romano) wrote in
    news::

    > magoo <> wrote in message
    > news:<-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
    (reverse each component for email address)
     
    A. Sinan Unur, Jul 3, 2004
    #9
  10. magoo

    Joe Smith Guest

    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
     
    Joe Smith, Jul 3, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TXVubnU=?=

    How to access logfile directory path from IIS

    =?Utf-8?B?TXVubnU=?=, Apr 22, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    506
    =?Utf-8?B?TXVubnU=?=
    Apr 22, 2005
  2. Andreas

    Logfile analysis API

    Andreas, May 10, 2005, in forum: Java
    Replies:
    0
    Views:
    357
    Andreas
    May 10, 2005
  3. Muruga Raj

    Logfile name format in logging

    Muruga Raj, Dec 23, 2008, in forum: Ruby
    Replies:
    3
    Views:
    124
    Muruga Raj
    Dec 24, 2008
  4. Replies:
    5
    Views:
    273
    Paul Lalli
    Mar 3, 2007
  5. Cameron Simpson
    Replies:
    1
    Views:
    83
    Marko Rauhamaa
    Mar 23, 2014
Loading...

Share This Page