my logroll code, please critique

Discussion in 'Ruby' started by Derek Smith, May 5, 2009.

  1. Derek Smith

    Derek Smith Guest

    My goal is to keep 10 files each at 100Mb. Please critique and suggest
    any corrections I need to make. Thank you!



    #!/usr/bin/ruby -w -W 2

    require 'ftools'
    require 'fileutils'

    =begin
    #-----------------------------
    File::LOCK_SH # shared lock (for reading)
    File::LOCK_EX # exclusive lock (for writing)
    File::LOCK_NB # non-blocking request
    File::LOCK_UN # free lock
    #-----------------------------
    =end


    ###-- 10 files at 100Mb before rolling begins ---##

    FSIZE = 104860000

    Dir.chdir("/cygdrive/c/temp/log") \
    or raise StandardError, "Change dir failed to ~aevrlog!"

    unless File.exists?("/cygdrive/c/temp/log/zipped")
    File.makedirs("zipped") \
    or raise StandardError, "Make dir failed to ~aevrlog/zipped!"
    end

    array1 = Dir.glob("dev*.log")
    array2 = Dir.glob("mon*.log")
    array3 = Dir.glob("fer*_se*.out")
    array4 = Dir.glob("fer*_se*.log")
    array5 = Dir.glob("fer*_in*.log")

    if (array1.length >= 1)
    array1.each { |file|

    if (File.size(file) > FSIZE)
    `/usr/bin/gzip "#{file}"`
    FileUtils.mv("#{file}.gz","/cygdrive/c/temp/log/zipped")
    FileUtils.touch("development.log")
    end
    }
    end

    Dir.chdir("/cygdrive/c/temp/log/zipped") \
    or raise StandardError, "Change dir failed to ~aevrlog!"

    array1_1 = Dir["/cygdrive/c/temp/log/zipped/dev*.gz"]

    if (array1_1.length >= 1)
    if File.exists?("development.log.11.gz")
    FileUtils.rm_r
    Dir.glob("/cygdrive/c/temp/log/zipped/dev*11.gz")
    end
    if File.exists?("development.log.10.gz")
    File.copy("development.log.10.gz", "development.log.11.gz")
    end
    if File.exists?("development.log.8.gz")
    File.copy("development.log.8.gz", "development.log.9.gz")
    end
    if File.exists?("development.log.7.gz")
    File.copy("development.log.7.gz", "development.log.8.gz")
    end
    if File.exists?("development.log.6.gz")
    File.copy("development.log.6.gz", "development.log.7.gz")
    end
    if File.exists?("development.log.5.gz")
    File.copy("development.log.5.gz", "development.log.6.gz")
    end
    if File.exists?("development.log.4.gz")
    File.copy("development.log.4.gz", "development.log.5.gz")
    end
    if File.exists?("development.log.3.gz")
    File.copy("development.log.3.gz", "development.log.4.gz")
    end
    if File.exists?("development.log.2.gz")
    File.copy("development.log.2.gz", "development.log.3.gz")
    end
    if File.exists?("development.log.1.gz")
    File.copy("development.log.1.gz", "development.log.2.gz")
    end
    if File.exists?("development.log.gz")
    File.copy("development.log.gz", "development.log.1.gz")
    end
    end
    --
    Posted via http://www.ruby-forum.com/.
    Derek Smith, May 5, 2009
    #1
    1. Advertising

  2. Derek Smith

    matt neuburg Guest

    Derek Smith <> wrote:

    > if File.exists?("development.log.10.gz")
    > File.copy("development.log.10.gz", "development.log.11.gz")
    > end
    > if File.exists?("development.log.8.gz")
    > File.copy("development.log.8.gz", "development.log.9.gz")
    > end
    > # etc, over and over and over and over


    You forgot log.9, and the very fact that this can happen, along with the
    patterned repetition here, would appear to be a Bad Smell (as in
    "Refactoring"). Why 30 hard-coded lines when a 5-line loop will do? m.

    --
    matt neuburg, phd = , http://www.tidbits.com/matt/
    Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
    AppleScript - http://www.amazon.com/gp/product/0596102119
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt neuburg, May 5, 2009
    #2
    1. Advertising

  3. On 05.05.2009 03:52, matt neuburg wrote:
    > Derek Smith <> wrote:
    >
    >> if File.exists?("development.log.10.gz")
    >> File.copy("development.log.10.gz", "development.log.11.gz")
    >> end
    >> if File.exists?("development.log.8.gz")
    >> File.copy("development.log.8.gz", "development.log.9.gz")
    >> end
    >> # etc, over and over and over and over

    >
    > You forgot log.9, and the very fact that this can happen, along with the
    > patterned repetition here, would appear to be a Bad Smell (as in
    > "Refactoring"). Why 30 hard-coded lines when a 5-line loop will do? m.


    Another thing that struck me is the usage of File.copy. File.rename
    would be much more efficient plus it would not change the last
    modification time of the file.

    Kind regards

    robert
    Robert Klemme, May 5, 2009
    #3
  4. Derek Smith

    Tom Cloyd Guest

    Derek Smith wrote:
    > My goal is to keep 10 files each at 100Mb. Please critique and suggest
    > any corrections I need to make. Thank you!
    >
    >
    >
    > #!/usr/bin/ruby -w -W 2
    >
    > require 'ftools'
    > require 'fileutils'
    >
    > =begin
    > #-----------------------------
    > File::LOCK_SH # shared lock (for reading)
    > File::LOCK_EX # exclusive lock (for writing)
    > File::LOCK_NB # non-blocking request
    > File::LOCK_UN # free lock
    > #-----------------------------
    > =end
    >
    >
    > ###-- 10 files at 100Mb before rolling begins ---##
    >
    > FSIZE = 104860000
    >
    > Dir.chdir("/cygdrive/c/temp/log") \
    > or raise StandardError, "Change dir failed to ~aevrlog!"
    >
    > unless File.exists?("/cygdrive/c/temp/log/zipped")
    > File.makedirs("zipped") \
    > or raise StandardError, "Make dir failed to ~aevrlog/zipped!"
    > end
    >
    > array1 = Dir.glob("dev*.log")
    > array2 = Dir.glob("mon*.log")
    > array3 = Dir.glob("fer*_se*.out")
    > array4 = Dir.glob("fer*_se*.log")
    > array5 = Dir.glob("fer*_in*.log")
    >
    > if (array1.length >= 1)
    > array1.each { |file|
    >
    > if (File.size(file) > FSIZE)
    > `/usr/bin/gzip "#{file}"`
    > FileUtils.mv("#{file}.gz","/cygdrive/c/temp/log/zipped")
    > FileUtils.touch("development.log")
    > end
    > }
    > end
    >
    > Dir.chdir("/cygdrive/c/temp/log/zipped") \
    > or raise StandardError, "Change dir failed to ~aevrlog!"
    >
    > array1_1 = Dir["/cygdrive/c/temp/log/zipped/dev*.gz"]
    >
    > if (array1_1.length >= 1)
    > if File.exists?("development.log.11.gz")
    > FileUtils.rm_r
    > Dir.glob("/cygdrive/c/temp/log/zipped/dev*11.gz")
    > end
    > if File.exists?("development.log.10.gz")
    > File.copy("development.log.10.gz", "development.log.11.gz")
    > end
    > if File.exists?("development.log.8.gz")
    > File.copy("development.log.8.gz", "development.log.9.gz")
    > end
    > if File.exists?("development.log.7.gz")
    > File.copy("development.log.7.gz", "development.log.8.gz")
    > end
    > if File.exists?("development.log.6.gz")
    > File.copy("development.log.6.gz", "development.log.7.gz")
    > end
    > if File.exists?("development.log.5.gz")
    > File.copy("development.log.5.gz", "development.log.6.gz")
    > end
    > if File.exists?("development.log.4.gz")
    > File.copy("development.log.4.gz", "development.log.5.gz")
    > end
    > if File.exists?("development.log.3.gz")
    > File.copy("development.log.3.gz", "development.log.4.gz")
    > end
    > if File.exists?("development.log.2.gz")
    > File.copy("development.log.2.gz", "development.log.3.gz")
    > end
    > if File.exists?("development.log.1.gz")
    > File.copy("development.log.1.gz", "development.log.2.gz")
    > end
    > if File.exists?("development.log.gz")
    > File.copy("development.log.gz", "development.log.1.gz")
    > end
    > end
    >

    Derek, I'm interested in what your trying to do here, and in the
    comments it's elicited. Could you share you revised code, when it's
    ready (or backchannel it, if you wish). I could definitely USE this!

    Tom
    Tom Cloyd, May 5, 2009
    #4
  5. Derek Smith

    Tim Pease Guest

    On Tue, May 5, 2009 at 2:55 AM, Tom Cloyd <> wrote:
    > Derek Smith wrote:
    >>
    >> My goal is to keep 10 files each at 100Mb. Please critique and suggest
    >> any corrections I need to make. =A0Thank you!
    >>
    >>
    >>

    >
    > Derek, I'm interested in what your trying to do here, and in the comments
    > it's elicited. Could you share you revised code, when it's ready (or
    > backchannel it, if you wish). I could definitely USE this!
    >


    Take a look at the "logging" gem. The RollingFile appender will do
    exactly this -- rolling files based on size or age. Moreover, it will
    safely roll the files even if multiple processes are writing to the
    same file.

    rubyforge.org/projects/logging

    Blessings,
    TwP
    Tim Pease, May 5, 2009
    #5
  6. Derek Smith

    Derek Smith Guest

    Tim Pease wrote:
    > On Tue, May 5, 2009 at 2:55 AM, Tom Cloyd <> wrote:
    >> backchannel it, if you wish). I could definitely USE this!
    >>

    >
    > Take a look at the "logging" gem. The RollingFile appender will do
    > exactly this -- rolling files based on size or age. Moreover, it will
    > safely roll the files even if multiple processes are writing to the
    > same file.
    >
    > rubyforge.org/projects/logging
    >
    > Blessings,
    > TwP


    I did look into this gem, but the documentation was weak and I tried to
    work it, but could not and failed so I wrote my own since I am new to
    Ruby. A good learning exp!....yucky Perl, ugly code is not for me!

    Will anyone share good sample code for logging?
    Thanks to all that replied, but Matt will you show me this loop you
    referred to?


    Thank you!
    --
    Posted via http://www.ruby-forum.com/.
    Derek Smith, May 5, 2009
    #6
  7. Derek Smith

    Eric Hodel Guest

    On May 5, 2009, at 15:44, Derek Smith wrote:
    > Tim Pease wrote:
    >> On Tue, May 5, 2009 at 2:55 AM, Tom Cloyd <>
    >> wrote:
    >>> backchannel it, if you wish). I could definitely USE this!
    >>>

    >>
    >> Take a look at the "logging" gem. The RollingFile appender will do
    >> exactly this -- rolling files based on size or age. Moreover, it will
    >> safely roll the files even if multiple processes are writing to the
    >> same file.
    >>
    >> rubyforge.org/projects/logging
    >>
    >> Blessings,
    >> TwP

    >
    > I did look into this gem, but the documentation was weak and I tried
    > to
    > work it, but could not and failed so I wrote my own since I am new to
    > Ruby. A good learning exp!....yucky Perl, ugly code is not for me!
    >
    > Will anyone share good sample code for logging?
    > Thanks to all that replied, but Matt will you show me this loop you
    > referred to?


    I use SyslogLogger (which adapts Syslog to a Logger API) and newsyslog
    to rotate logs.
    Eric Hodel, May 5, 2009
    #7
  8. Derek Smith wrote:
    > Will anyone share good sample code for logging?


    There's the logger.rb in the ruby standard library (or did someone
    mention that already?). It handles rotation, log levels, but perhaps not
    multiprocess safety.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, May 6, 2009
    #8
  9. Derek Smith

    Derek Smith Guest

    Joel VanderWerf wrote:
    > Derek Smith wrote:
    >> Will anyone share good sample code for logging?

    >
    > There's the logger.rb in the ruby standard library (or did someone
    > mention that already?). It handles rotation, log levels, but perhaps not
    > multiprocess safety.


    Here is my updated code, but how do I look at the documentation from the
    CLI for logger, logging or any gem? For example, if I want to look at
    Perl docs I use perldoc -f "builtin function" or perldoc -q "F.A.Q."
    Finally, what about a central logging block for when at anytime 'raise
    StandardError' is called, the script will log to one logfile. Will
    anyone suggest code or how to do this?


    thank you!


    #!/usr/bin/ruby -w -W 2

    require 'ftools'
    require 'fileutils'

    =begin
    #-----------------------------
    File::LOCK_SH # shared lock (for reading)
    File::LOCK_EX # exclusive lock (for writing)
    File::LOCK_NB # non-blocking request
    File::LOCK_UN # free lock
    #-----------------------------
    =end


    ###-- 10 files at 100Mb before rolling begins ---##

    FSIZE = 104860000

    Dir.chdir("/cygdrive/c/temp/log") \
    or raise StandardError, "Change dir failed to ~aevrlog!"

    unless File.exists?("/cygdrive/c/temp/log/zipped")
    File.makedirs("zipped") \
    or raise StandardError, "Make dir failed to ~aevrlog/zipped!"
    end

    array1 = Dir.glob("dev*.log")
    array2 = Dir.glob("mon*.log")
    array3 = Dir.glob("fer*_se*.out")
    array4 = Dir.glob("fer*_se*.log")
    array5 = Dir.glob("fer*_in*.log")

    if (array1.length >= 1)
    array1.each { |file|

    if (File.size(file) > FSIZE)
    File.open(file, "r+") do |f|
    if (f.flock(File::LOCK_SH)) == 0
    `/usr/bin/gzip "#{file}"`
    FileUtils.mv("#{file}.gz","/cygdrive/c/temp/log/zipped")
    end
    end
    FileUtils.touch("development.log")
    end
    }
    end

    Dir.chdir("/cygdrive/c/temp/log/zipped") \
    or raise StandardError, "Change dir failed to ~aevrlog!"

    array1_1 = Dir["/cygdrive/c/temp/log/zipped/dev*.gz"]

    if (array1_1.length >= 1)
    if File.exists?("development.log.11.gz")
    FileUtils.rm_r
    Dir.glob("/cygdrive/c/temp/log/zipped/dev*11.gz")
    end
    if File.exists?("development.log.10.gz")
    File.rename("development.log.10.gz",
    "development.log.11.gz")
    end
    if File.exists?("development.log.9.gz")
    File.rename("development.log.9.gz", "development.log.10.gz")
    end
    if File.exists?("development.log.8.gz")
    File.rename("development.log.8.gz", "development.log.9.gz")
    end
    if File.exists?("development.log.7.gz")
    File.rename("development.log.7.gz", "development.log.8.gz")
    end
    if File.exists?("development.log.6.gz")
    File.rename("development.log.6.gz", "development.log.7.gz")
    end
    if File.exists?("development.log.5.gz")
    File.rename("development.log.5.gz", "development.log.6.gz")
    end
    if File.exists?("development.log.4.gz")
    File.rename("development.log.4.gz", "development.log.5.gz")
    end
    if File.exists?("development.log.3.gz")
    File.rename("development.log.3.gz", "development.log.4.gz")
    end
    if File.exists?("development.log.2.gz")
    File.rename("development.log.2.gz", "development.log.3.gz")
    end
    if File.exists?("development.log.1.gz")
    File.rename("development.log.1.gz", "development.log.2.gz")
    end
    if File.exists?("development.log.gz")
    File.rename("development.log.gz", "development.log.1.gz")
    end
    end

    --
    Posted via http://www.ruby-forum.com/.
    Derek Smith, May 6, 2009
    #9
  10. Derek Smith wrote:
    > Here is my updated code, but how do I look at the documentation from the
    > CLI for logger, logging or any gem? For example, if I want to look at
    > Perl docs I use perldoc -f "builtin function" or perldoc -q "F.A.Q."


    http://www.ruby-doc.org/stdlib/

    Look in the left panel for "logger".

    This site is still pretty useful (though I don't know if it is
    frequently updated).

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, May 6, 2009
    #10
  11. Derek Smith

    Tom Cloyd Guest

    Derek Smith wrote:
    > Joel VanderWerf wrote:
    >
    >> Derek Smith wrote:
    >>
    >>> Will anyone share good sample code for logging?
    >>>

    >> There's the logger.rb in the ruby standard library (or did someone
    >> mention that already?). It handles rotation, log levels, but perhaps not
    >> multiprocess safety.
    >>

    >
    > Here is my updated code, but how do I look at the documentation from the
    > CLI for logger, logging or any gem? For example, if I want to look at
    > Perl docs I use perldoc -f "builtin function" or perldoc -q "F.A.Q."
    > Finally, what about a central logging block for when at anytime 'raise
    > StandardError' is called, the script will log to one logfile. Will
    > anyone suggest code or how to do this?
    >
    >

    Google "ruby logger" and you get right to the RDoc documentation -
    http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/index.html

    Access the "logger" class and you'll get some very good documentation.

    Re: errors - the standard error stream goes to STDERR, a constant ruby
    automatically creates. Normally, this goes to STDOUT - the standard
    output stream, assigned to your screen, but you can reassign it:

    $stderr = File.new( 'myerrs.txt','w' )

    Hope this helps...

    t.

    --

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
    Bellingham, Washington, U.S.A: (360) 920-1226
    << >> (email)
    << TomCloyd.com >> (website)
    << sleightmind.wordpress.com >> (mental health weblog)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Tom Cloyd, May 6, 2009
    #11
  12. Derek Smith

    Tom Cloyd Guest

    Derek Smith wrote:
    > Joel VanderWerf wrote:
    >
    >> Derek Smith wrote:
    >>
    >>> Will anyone share good sample code for logging?
    >>>

    >> There's the logger.rb in the ruby standard library (or did someone
    >> mention that already?). It handles rotation, log levels, but perhaps not
    >> multiprocess safety.
    >>

    >
    > Here is my updated code, but how do I look at the documentation from the
    > CLI for logger, logging or any gem? For example, if I want to look at
    > Perl docs I use perldoc -f "builtin function" or perldoc -q "F.A.Q."
    > Finally, what about a central logging block for when at anytime 'raise
    > StandardError' is called, the script will log to one logfile. Will
    > anyone suggest code or how to do this?
    >
    >

    More on finding documentation -

    I can't help you if you're on a Winxxxx system, but on my Kubuntu Linux
    OS I can find useful documentation for the "logger" standard library
    member (and others) by issuing at my CLI -

    which logger

    I get a pile of locations on the filesys where "logger" appears. Many
    are irrelevant, but this one is not:

    /usr/share/doc/ruby1.8-examples/examples/logger

    There I find 3 ruby code files illustrating library use. Nice.

    (I didn't know about this until I set out to find an answer to your
    question, so thanks to you for that.)

    t.

    --

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
    Bellingham, Washington, U.S.A: (360) 920-1226
    << >> (email)
    << TomCloyd.com >> (website)
    << sleightmind.wordpress.com >> (mental health weblog)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Tom Cloyd, May 6, 2009
    #12
    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. Michael Strorm
    Replies:
    26
    Views:
    768
    J. Campbell
    Nov 10, 2003
  2. Rv5

    Code Critique Please

    Rv5, Nov 16, 2003, in forum: C++
    Replies:
    3
    Views:
    360
    Benny Hill
    Nov 16, 2003
  3. gorda
    Replies:
    16
    Views:
    770
    Karl Heinz Buchegger
    Jul 29, 2004
  4. Adrian

    Code critique please

    Adrian, Oct 30, 2004, in forum: C++
    Replies:
    2
    Views:
    346
    Adrian
    Oct 31, 2004
  5. Rv5

    Code Critique Please

    Rv5, Nov 16, 2003, in forum: C Programming
    Replies:
    2
    Views:
    334
    -berlin.de
    Nov 16, 2003
Loading...

Share This Page