Multiple processes logging to single file

Discussion in 'Ruby' started by pachl, May 22, 2008.

  1. pachl

    pachl Guest

    When using the native ruby logger, is it save to have multiple
    processes writing to the same log file?

    For instance, I have the following ruby source example:

    # test.rb
    require 'logger'

    log = Logger.new('/tmp/RLOG')

    20.times do
    log.error "PID: #{$$}"
    sleep 1
    end

    log.close
    # end test.rb

    Then I execute like so:

    $ ruby test.rb & ruby test.rb & ruby test.rb

    After inspection of the log file, each process logged 20 times, all
    messages interleaved of course. It looks like it works, but am I
    delusional? I'm on an OpenBSD box. Does the OS handle caching/writing
    from multiple sources?

    -pachl
     
    pachl, May 22, 2008
    #1
    1. Advertisements

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    pachl wrote:

    | After inspection of the log file, each process logged 20 times, all
    | messages interleaved of course. It looks like it works, but am I
    | delusional? I'm on an OpenBSD box. Does the OS handle caching/writing
    | from multiple sources?

    Yes, the OS should manage that (it is file-system level, rather than
    userspace level). However, you can tweak your code to have
    'transactional' logs (i.e. that every application logs a meaningful
    chunk of information in one go).

    Anyway, the file system and drivers take care of the actual writing.

    Though, having everything log into one logfile *may* result in fractured
    files, or odd race conditions and/or deadlocks. It might be saner to log
    each process to its own file.

    - --
    Phillip Gawlowski
    Twitter: twitter.com/cynicalryan
    Blog: http://justarubyist.blogspot.com

    Don't over-comment.
    ~ - The Elements of Programming Style (Kernighan & Plaugher)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.8 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iEYEARECAAYFAkg0tLAACgkQbtAgaoJTgL9zpACfR/jr2oNQSrRc34nr7a6/rTX1
    oGcAnj/BTPpEjGy9eZJz2NHYXBWPf5yk
    =zdqV
    -----END PGP SIGNATURE-----
     
    Phillip Gawlowski, May 22, 2008
    #2
    1. Advertisements

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. Abhishek Srivastava
    Replies:
    9
    Views:
    4,231
    Ashish
    Feb 3, 2004
  2. Stefan Siegl
    Replies:
    0
    Views:
    1,165
    Stefan Siegl
    Aug 27, 2003
  3. Smegly
    Replies:
    1
    Views:
    1,340
    Mitchell
    May 19, 2004
  4. Replies:
    4
    Views:
    1,238
    M.E.Farmer
    Feb 13, 2005
  5. Christoph Haas
    Replies:
    0
    Views:
    614
    Christoph Haas
    Jun 12, 2006
  6. Christoph Haas
    Replies:
    1
    Views:
    645
    Vinay Sajip
    Jun 14, 2006
  7. Replies:
    8
    Views:
    646
    The Ghost In The Machine
    Aug 1, 2006
  8. ela
    Replies:
    12
    Views:
    636
    Uri Guttman
    Apr 6, 2009
Loading...