Mojo logger inherits from Mojo::Emitter. On before_dispatch I added the subscriber which sets user before next default subscriber makes log. But it works like before, the logger does not know what is the current request.
Thanks, such helper is good idea. But the internal modules of Mojolicious would still use the app->log so the user may be mismatched in these logs. W dniu czwartek, 31 sierpnia 2017 17:50:40 UTC+2 użytkownik Dan Book napisał: > > A hook is not a suitable place to put request-specific data, because of > the async pattern you discovered. Requests may partially dispatch and then > another request may be handled by the event loop while waiting for another > part of the request. Some of the request may be initiated from outside the > dispatch chain entirely like your callback from the user agent request. I'm > not sure what solution is best for your use case but one option would be to > write a helper which sets this userid according to the current request and > logs all in one call, and only use that helper to log. > > On Thu, Aug 31, 2017 at 8:08 AM, njzt <[email protected] <javascript:>> > wrote: > >> Hi >> >> in my app logs there is userid: >> >> >> $self->hook( before_dispatch => sub { >> >> >> my ($c) = @_; >> >> >> Log::Log4perl::MDC->put('userid', $c->userid); >> >> >> >> >> In few cases app makes request to external webservice using non-blocking >> request. When external request for user A is pending then hypnotoad worker >> takes another request of another user B and sets his userid in logs. When >> external request for user A is completed then there is still userid of user >> B in logs. >> >> $r->get('/async')->to(cb => sub { >> >> >> my ($c) = @_; >> >> >> $c->app->log->debug("hello"); # user A in logs >> $c->ua->get("http://hello.world" => sub { >> >> >> my ($ua, $tx) = @_; >> >> >> $c->app->log->debug("hello"); # user B in logs for request of user A >> ... >> }); >> >> >> }); >> >> >> I know I can call "put(...)" once more in callback of ua->get but I have >> few places like this, maybe you have some other ideas instead of copy-paste >> it for each new non-blocking external request. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Mojolicious" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> To post to this group, send email to [email protected] >> <javascript:>. >> Visit this group at https://groups.google.com/group/mojolicious. >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "Mojolicious" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/mojolicious. For more options, visit https://groups.google.com/d/optout.
