Dearest Monks

I built a small mod_perl handler that logs a message in a custom log file depending on the presence and the value of an HTTP header. It is named Tiscali::LogLoadBalancer and uses the Apache::LogFile module this way in httpd.conf:

PerlModule Apache::LogFile PerlModule Apache::LogFile::Config PerlLogFile "logs/loadbalancer.log" Tiscali::LoadBalancerLogFile <Location /> PerlFixuphandler Tiscali::LogLoadBalancer </Location>

The handler works almost well: header's values are correctly logged into the logs/loadbalancer.log file. E.g.:

Mon Jul  9 16:53:56 2007 [25102] X-Loadbalancer: 0 (OK)

Unfortunately, when the server is heavily stressed (like launching three parallel instances of the ab utility with three different values for that header) it happens quite often that two (or more!) different log messages are merged togehter, e.g.:

Mon Jul  9 16:53:56 2007 [25101] X-Loadbalancer: 2 (INVALID)Mon Jul  9 16:53:56 2007 [25108] X-Loadbalancer: 1 (OK)

or

Mon Jul 9 16:53:56 2007 [25101] X-Loadbalancer: 2 (INVALID)Mon Jul 9 + 16:53:56 2007 [25108] X-Loadbalancer: 1 (OK)Mon Jul 9 16:53:56 2007 + [25102] X-Loadbalancer: 0 (OK) Mon Jul 9 16:53:56 2007 [25101] X-Loadbalancer: 2 (INVALID)

Is there any way to avoid that side effect? When I opted for an Apache::LogFile-based solution I did it also because I believed that this kind of issue was already taken care of by the author, who has a well respected name in the community. I guess I am probably doing something wrong, but I can't tell what... any help?

Module's code follows:

package Tiscali::LogLoadBalancer ; use strict ; use warnings ; use Apache::Constants qw{:common} ; sub handler { my ($self,$r) = @_ ; $r ||= Apache->request() ; my $status ; my $lb = $r->header_in( q{X-Loadbalancer} ) ; if (defined $lb) { $status = q{INVALID} ; $status = q{OK} if $lb == 0 ; $status = q{OK} if $lb == 1 ; } else { $lb = q{UNDEF} ; $status = q{OK} ; } _logger($lb,$status) ; return DECLINED ; } sub _logger { my ($lb_value,$status) = @_ ; my $now = localtime ; my $pid = $$ ; my $message = qq{$now [$pid] X-Loadbalancer: $lb_value ($status)} ; print Tiscali::LoadBalancerLogFile $message ; } 1 ;

Ciao!
--bronto


In theory, there is no difference between theory and practice. In practice, there is.

In reply to Merged lines with Apache::LogFile-based log handler by bronto

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.