Log files in a Date / Time Stamped Directory

Discussion in 'Perl Misc' started by Manzoorul Hassan, Mar 17, 2005.

  1. I would like to store my logs in a Directory / Subdirectory tree that
    corresponds to the date / time the script is executed. For example, if
    the script is executed today and noon, then the logs would be stored
    under 20050317/1200/

    How do I do this?

    - manzoor
     
    Manzoorul Hassan, Mar 17, 2005
    #1
    1. Advertising

  2. Manzoorul Hassan wrote:
    > I would like to store my logs in a Directory / Subdirectory tree that
    > corresponds to the date / time the script is executed. For example, if
    > the script is executed today and noon, then the logs would be stored
    > under 20050317/1200/
    >
    > How do I do this?


    What have you tried? How did it fail?
     
    Brian McCauley, Mar 17, 2005
    #2
    1. Advertising

  3. Manzoorul Hassan wrote:

    > I would like to store my logs in a Directory / Subdirectory tree that
    > corresponds to the date / time the script is executed. For example, if
    > the script is executed today and noon, then the logs would be stored
    > under 20050317/1200/
    >
    > How do I do this?


    perldoc -f localtime
     
    Brian McCauley, Mar 17, 2005
    #3
  4. Manzoorul Hassan <> wrote:
    > I would like to store my logs in a Directory / Subdirectory tree that
    > corresponds to the date / time the script is executed. For example, if
    > the script is executed today and noon, then the logs would be stored
    > under 20050317/1200/
    >
    > How do I do this?



    perldoc -f time
    perldoc -f localtime
    perldoc -f gmtime
    perldoc -f mkdir


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 17, 2005
    #4
  5. OK, I tried the following for the Date and it works:

    #!/usr/bin/perl -l
    use Time::localtime;
    $time = localtime;

    printf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1, $time->mday);
    printf ("%02d%02d\n", $time->hour, $time->min);

    But I can't store this as a value for my Date and Time Dir to be opened
    / created.

    - manzoor
     
    Manzoorul Hassan, Mar 17, 2005
    #5
  6. Manzoorul Hassan

    Paul Lalli Guest

    "Manzoorul Hassan" <> wrote in message
    news:...
    > OK, I tried the following for the Date and it works:
    >


    To whom are you speaking? Have you read the posting guidelines for this
    group? Please quote an appropirate amount of context.

    > #!/usr/bin/perl -l


    you forgot use strict; and use warnings;

    > use Time::localtime;
    > $time = localtime;
    >
    > printf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1,

    $time->mday);
    > printf ("%02d%02d\n", $time->hour, $time->min);
    >
    > But I can't store this as a value for my Date and Time Dir to be

    opened
    > / created.


    Why can't you? What have you tried that failed?

    Or do you just mean that you "can't" because printf() prints to the
    screen instead of storing to a variable? That's what printf() does.
    It's cousin sprintf(), on the other hand, returns the formatted string.
    Perhaps that's what you're looking for?

    Paul Lalli
     
    Paul Lalli, Mar 17, 2005
    #6
  7. well, I was trying to do the following:

    $date_val = printf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1,
    $time->mday);
    opendir(DIR, "$date_val) || die "Cannot open Dir\n";

    That didn't work. Now I am trying with the sprintf, but still not
    opening.

    - manzoor
     
    Manzoorul Hassan, Mar 17, 2005
    #7
  8. Manzoorul Hassan

    Paul Lalli Guest

    "Manzoorul Hassan" <> wrote in message
    news:...
    > well, I was trying to do the following:
    >
    > $date_val = printf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1,
    > $time->mday);


    This is non-sensical. printf() prints a string to the screen.
    sprintf() returns the string.

    > opendir(DIR, "$date_val) || die "Cannot open Dir\n";
    >
    > That didn't work.


    Of course not. It's a syntax error. Post real code.

    > Now I am trying with the sprintf, but still not opening.


    Post your real code that is failing, along with the error messages you
    are receiving. It's not possible to help you otherwise.

    Paul Lalli
     
    Paul Lalli, Mar 17, 2005
    #8
  9. #!/usr/bin/perl

    use Time::localtime;
    $time = localtime;

    $date = sprintf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1,
    $time->mday);
    opendir(DATE_DIR, "$date") || die "Cannot open directory";

    closedir(DATE_DIR);
     
    Manzoorul Hassan, Mar 17, 2005
    #9
  10. Manzoorul Hassan wrote:

    > #!/usr/bin/perl
    >
    > use Time::localtime;
    > $time = localtime;
    >
    > $date = sprintf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1,
    > $time->mday);
    > opendir(DATE_DIR, "$date") || die "Cannot open directory";
    >
    > closedir(DATE_DIR);


    And what, exactly, is this code supposed to do? Because, as near as
    I can tell, it doesn't do anything, other than die with "Cannot open
    directory" if the directory in $date doesn't exist.
    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
     
    Chris Mattern, Mar 17, 2005
    #10
  11. "Manzoorul Hassan" <> wrote in
    news::

    Please make an effort to compose proper follow-ups by quoting an
    appropriate amount of context. Please *read* and *follow* the posting
    guidelines for this group.

    > #!/usr/bin/perl


    use strict;
    use warnings;

    missing.

    > use Time::localtime;
    > $time = localtime;


    my $time = localtime;

    > $date = sprintf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1,
    > $time->mday);


    my $date = sprintf(
    q{%04d%02d%02d},
    $time->year + 1900,
    $time->mon + 1,
    $time->mday
    );

    Did you really, really want that \n in the directory name?

    > opendir(DATE_DIR, "$date") || die "Cannot open directory";


    opendir my $dir, $date or die "Cannot open $date: $!";

    Get all the help you can from perl. See also:

    perldoc -q always

    Putting it all together:

    #! /usr/bin/perl

    use strict;
    use warnings;

    use Time::localtime;
    my $time = localtime;

    my $date = sprintf(
    q{%04d%02d%02d},
    $time->year + 1900,
    $time->mon + 1,
    $time->mday
    );

    opendir my $dir, $date or die "Cannot open $date: $!";

    # do something

    closedir $dir;

    __END__

    D:\Home> v
    Cannot open 20050317: No such file or directory at D:\Home\v.pl line 16.

    Sinan.
     
    A. Sinan Unur, Mar 17, 2005
    #11
  12. Thanx to everyone for your paitience and help. After all of this, I
    have finally realized that this does not create a new directory. I was
    hoping to have a directory, and a subdirectory, created to store all
    the log files. But I guess, I'll just use the Date as part of the
    filename of the log.

    - manzoor
     
    Manzoorul Hassan, Mar 17, 2005
    #12
  13. "Manzoorul Hassan" <> wrote in
    news::

    > Thanx to everyone for your paitience and help.


    You are welcome.

    > After all of this, I have finally realized that this does not create a
    > new directory.


    "this" does indeed not create a directory. For that, you'll need to use
    the appropriate function, as was suggested in an earlier post:

    perldoc -f mkdir

    If you had taken a little bit of care in composing your follow-ups
    carefully, I would have known about your original purpose.

    > I was hoping to have a directory, and a subdirectory,
    > created to store all the log files.


    What, apart from not bothering to read documentation, is stopping you
    from doing that?

    > But I guess, I'll just use the Date as part of the filename of
    > the log.


    Whatever floats your boat.

    Sinan.
     
    A. Sinan Unur, Mar 17, 2005
    #13
  14. Manzoorul Hassan <> wrote:


    > opendir(DATE_DIR, "$date") || die "Cannot open directory";

    ^ ^
    ^ ^
    ^ ^ perldoc -q vars

    I thought you said you want to *create* a directory?

    opendir, strangly enough, _opens_ a directory after it has been created.

    perldoc -f mkdir


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 17, 2005
    #14
  15. well, I was thining this would be similar to Files - if it doesn't
    exist, create it.

    But I guess Directories don't work the same way :eek:(

    - manzoor
     
    Manzoorul Hassan, Mar 17, 2005
    #15
  16. Manzoorul Hassan wrote:

    > use Time::localtime;
    > $time = localtime;
    >
    > $date = sprintf ("%04d%02d%02d\n", $time->year+1900, $time->mon+1,
    > $time->mday);


    While at some level there's nothing wrong with doing it long-hand using
    the basic sprintf() and addinf the necessary offsets there is a special
    sprintf()-like function for fomatting localtime() to be found one of the
    standard Perl modules.

    For details:

    perldoc -f localtime

    Oh, and for $DEITY's sake get into the habit of always declaring all
    variables as lexically scoped in the smallest applicable scope unless
    there is a reson to do otherwise. Do this pre-emptively. Do not wait to
    experience the pain that will inevitably result from not doing so.
     
    Brian McCauley, Mar 18, 2005
    #16
    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. Chris Berg
    Replies:
    0
    Views:
    795
    Chris Berg
    Oct 27, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,278
    Michael Borgwardt
    May 30, 2004
  3. Keith Cochrane
    Replies:
    2
    Views:
    626
    Keith Cochrane
    Aug 6, 2006
  4. Chris Ward

    JPA and time stamped data

    Chris Ward, Jul 24, 2007, in forum: Java
    Replies:
    4
    Views:
    725
    Chris Ward
    Jul 24, 2007
  5. Jayesh Kamdar
    Replies:
    2
    Views:
    159
    Gunnar Hjalmarsson
    May 25, 2005
Loading...

Share This Page