Writing from 3 sources to 1 file

i've an app with 2 daemons, i'd like to write all the log messages to
the same file.

then i suppose i need to lock the file when one source is writing and
wait the file is unlocked for the other ?

how this could be done in ruby, or is it automagic ))

···

--
une bévue

Une bévue wrote:

i've an app with 2 daemons, i'd like to write all the log messages to
the same file.

then i suppose i need to lock the file when one source is writing and
wait the file is unlocked for the other ?

how this could be done in ruby, or is it automagic ))

There is some locking support but watch out for NFS. There is Ara's solution at http://raa.ruby-lang.org/project/lockfile/

An alternative approach is to have a centralized logger daemon which is the only process writing the file. Your sources then could connect to it via DRB. This might even be more efficient because the logger daemon can keep the file open and even queue a number of log messages to write in order to decouple file writing and your sources activities.

Kind regards

  robert

mussel:~ > ri 'File#flock'
------------------------------------------------------------- File#flock
      file.flock (locking_constant ) => 0 or false

···

On Sat, 5 Aug 2006, [ISO-8859-1] Une bévue wrote:

i've an app with 2 daemons, i'd like to write all the log messages to
the same file.

then i suppose i need to lock the file when one source is writing and
wait the file is unlocked for the other ?

how this could be done in ruby, or is it automagic ))

------------------------------------------------------------------------
      Locks or unlocks a file according to _locking_constant_ (a logical
      _or_ of the values in the table below). Returns +false+ if
      +File::LOCK_NB+ is specified and the operation would otherwise have
      blocked. Not available on all platforms.

      Locking constants (in class File):

         LOCK_EX | Exclusive lock. Only one process may hold an
                   > exclusive lock for a given file at a time.
         ----------+------------------------------------------------
         LOCK_NB | Don't block when locking. May be combined
                   > with other lock options using logical or.
         ----------+------------------------------------------------
         LOCK_SH | Shared lock. Multiple processes may each hold a
                   > shared lock for a given file at the same time.
         ----------+------------------------------------------------
         LOCK_UN | Unlock.

      Example:

         File.new("testfile").flock(File::LOCK_UN) #=> 0

you also need to make sure that buffers are flushed after each write or the
msgs will be interleaved.

if you're on windows you'll need one on these

   http://rubyforge.org/projects/win32utils/

but i forget which one provides locking - probably 'win32-file'

regards.

-a
--
happiness is not something ready-made. it comes from your own actions.
- h.h. the 14th dali lama

If you are just writing on a line by line basis, OS buffering should mitigate the need to do anything special, as long as you don't mind have lines interleaved from different daemons.

e.g.

a whole line from daemon1
a whole line from daemon2
a whole line from daemon1

···

On Aug 5, 2006, at 1:00 AM, Une bévue wrote:

i've an app with 2 daemons, i'd like to write all the log messages to
the same file.

then i suppose i need to lock the file when one source is writing and
wait the file is unlocked for the other ?

how this could be done in ruby, or is it automagic ))

-- une bévue

ok thanx, in fact two of the tree sources are allready daemons ))

···

Robert Klemme <shortcutter@googlemail.com> wrote:

There is some locking support but watch out for NFS. There is Ara's
solution at http://raa.ruby-lang.org/project/lockfile/

An alternative approach is to have a centralized logger daemon which is
the only process writing the file. Your sources then could connect to
it via DRB. This might even be more efficient because the logger daemon
can keep the file open and even queue a number of log messages to write
in order to decouple file writing and your sources activities.

--
une bévue

i don't want interleave, i have 2 different daemons and one app as
sources, the trio run asynchronously. I don't report in a line by line
basis too.

i think i'll have a look upon Log4r.
i think (may be) with Log4r i might be able to write log msg thru socket
then be able to debugg thru de net.

···

Logan Capaldo <logancapaldo@gmail.com> wrote:

If you are just writing on a line by line basis, OS buffering should
mitigate the need to do anything special, as long as you don't mind
have lines interleaved from different daemons.

--
une bévue

you also need to make sure that buffers are flushed after each write or the
msgs will be interleaved.

thanx for your long a vlear answer !

if you're on windows you'll need one on these

unfortunately i forgot to mention i'm unning on MacOS X...

···

<ara.t.howard@noaa.gov> wrote:

--
une bévue