Esteemed Monks and friends...

I'm just putting the finishing touches on a rather large and complex project and noticed an issue with my logging mechanism. Trying to simplify things for clarity, I share an object that contains a logging object that holds an internal file handle for writing. I then spin off an async event that runs at every X seconds. This event receives a copy of the object that contains the log object.

All parts of the application write to the log file just fine, except for the code that runs within the event. Here is a *very* dumbed down example of what I mean:

use warnings; use strict; use Async::Event::Interval; use Logging::Simple; my $log = Logging::Simple->new( file => 'test.log', write_mode => 'w' ); my $count = 1; my $e = Async::Event::Interval->new( 1, sub { print "running poll $count\n"; $log->_0('running poll $count') ; $count++; } ); $e->start; for (0..3){ $log->_0('logging in main'); sleep 1; } $e->stop;

What I need is for all $log->_0() entries to go into the specified file, but the log file ends up looking like this, with only the main log entries making it to the file:

[2018-01-23 07:39:04.604][lvl 0] logging in main [2018-01-23 07:39:05.605][lvl 0] logging in main [2018-01-23 07:39:06.605][lvl 0] logging in main [2018-01-23 07:39:07.605][lvl 0] logging in main

The Async::Event::Interval is a distribution that simply runs a specified subroutine every X seconds (in this case, 1). It's exceptionally basic, and I wrote it for a single purpose (well, that, and to learn). In the above case, nothing is passed in, the event simply uses the $log object from within the file scope itself.

Is there a way to share a handle like this? If not, can anyone recommend the proper way to do these things (I won't object to using a different async-event type distribution if necessary)?

Thanks,

-stevieb


In reply to Sharing a filehandle with an asynchronous event by stevieb

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.