Learned friends,

I am attempting to convert my Starman log to structured JSON to match the app logs, and to add a request ID to all the log entries starting with the Starman log.

I have both of these things working separately but they are not playing nicely with each other. I used Plack::Middleware::AccessLog::Structured for the log, and as advertised I get nice single-line JSON in my access_log. I also used Plack::Middleware::RequestId to generate the request ID, and this too is working, in the sense that in the application code, the value is present in the Plack env.

However, the initial log entry from Starman cannot read the request ID from the env, and I suspect it's because it's in the same builder block. I have tried pushing the value to an HTTP header and the regular ENV as well as the Plack env, and all attempts have the same outcome: the value is null in the log for the Starman entry and present for the app log entries. Here's my app.psgi:

use Meta::Portal; use Plack::Builder; use Plack::Middleware::AccessLog::Structured; use Plack::Middleware::RequestId; use Data::GUID; my $app = Meta::Portal->to_app; builder { enable 'RequestId', id_generator => sub { Data::GUID->new->as_string; }; enable 'AccessLog::Structured', callback => sub { my ($env, $message) = @_; #use Data::Dumper; #warn Dumper $env; $message->{date} = Time::Moment->from_epoch( delete $message-> +{epochtime} )->strftime('%F %T%6f'); $message->{tracking_id} //= $env{'psgix.request_id'}; return $message; }; $app; };

Thanks for any help!


The way forward always starts with a minimal test.

In reply to Plack middleware sets Plack env value but it's not consumable in the same builder by 1nickt

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.