writing from 3 sources to 1 file

U

Une bévue

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

Robert Klemme

Une said:
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
 
U

Une bévue

Robert Klemme said:
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.

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

ara.t.howard

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 ) =3D> 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) #=3D> 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
--=20
happiness is not something ready-made. it comes from your own actions.
- h.h. the 14th dali lama
 
L

Logan Capaldo

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

-- =20
une b=E9vue

If you are just writing on a line by line basis, OS buffering should =20
mitigate the need to do anything special, as long as you don't mind =20
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
 
U

Une bévue

Logan Capaldo said:
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.
 
U

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top