I think what you are after is a Queue. Your processes can write to the queue, and a seperate, single process can pop them off the queue and put them in the log file.

One place I worked used UNIX queues and two small C programs to accomplish this. I did not write, nor remember the code but as I recall it was fairly short.

There is at least one Perl Queue module (Queue::Base) though I have no experience with it or how it works.

There is also POE which might work for you.

As someone mentioned, if you have access already to a database like Oracle which has a queue implementation, or perhaps something like MQ then that would work. Obviously it is overkill if you need it only for a single logging queue, but if it is there already...

Depending on your processes and versions of Perl, perhaps you can do a single process for all of them and do something clever with threads if you are feeling adventurous.

Depending on your access and the temperment of your Sys Admins, you might be able to write to the system file (syslog) and then either use that as your log file or reap it with another single process. Depends on what you want this for and the volume of messages you expect, I guess.

As a last, low tech model, pick a directory and have every process write seperate files for each message to the directory. Use a nameing convention/time stamp or some such to determine file/message order. You can also use the naming convention to distinguish files in process (e.g. "dot" files are being written to. when finished the process can remname them to remove the leading '.'). Then, have another, again single, process reap that directory, transfer the messages to the log file and delete the temp files.


In reply to Re: Writing to a log file without colliding by KeighleHawk
in thread Writing to a log file without colliding by cgraf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.