# Writing from 3 sources to 1 file

**URL:** <https://www.ruby-forum.com/t/writing-from-3-sources-to-1-file/101477>\
**Category:** Ruby\
**Created:** [August 5, 2006, 7:02am UTC](https://www.ruby-forum.com/t/writing-from-3-sources-to-1-file/101477 "2006-08-05T07:02:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 5, 2006, 7:02am UTC](https://www.ruby-forum.com/t/writing-from-3-sources-to-1-file/101477/1 "2006-08-05T07:02:46Z")

</div>

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 ))

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 5, 2006, 10:56am UTC](https://www.ruby-forum.com/t/writing-from-3-sources-to-1-file/101477/2 "2006-08-05T10:56:55Z")

</div>

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/](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

```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 5, 2006, 11:35am UTC](https://www.ruby-forum.com/t/writing-from-3-sources-to-1-file/101477/3 "2006-08-05T11:35:57Z")

</div>

Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There is some locking support but watch out for NFS. There is Ara’s  
> solution at [http://raa.ruby-lang.org/project/lockfile/](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.

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 5, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/writing-from-3-sources-to-1-file/101477/4 "2006-08-05T16:27:09Z")

</div>

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 ))

## mussel:~ \> ri ‘File#flock’ ------------------------------------------------------------- File#flock file.flock (locking\_constant ) =\> 0 or false

```
  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/](http://rubyforge.org/projects/win32utils/)

but i forget which one provides locking - probably ‘win32-file’

regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 5, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/writing-from-3-sources-to-1-file/101477/5 "2006-08-05T19:33:22Z")

</div>

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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…

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 5, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/writing-from-3-sources-to-1-file/101477/6 "2006-08-05T19:33:22Z")

</div>

Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 5, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/writing-from-3-sources-to-1-file/101477/7 "2006-08-05T16:33:10Z")

</div>

On Aug 5, 2006, at 1:00 AM, Une bévue 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.

e.g.

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