Skip to content

Model/backend Log::Any log producer uses wrong "request-id context" #114

Description

@XSven

We have started to use Log::Any::Adapter::MojoLog in our Mojolicious application. In the startup() method we are calling

Log::Any::Adapter->set( 'MojoLog', logger => $app->log )

Now we wanted to show the request-id as additional context information. The MojoLog adapter isn't context aware: It doesn't implement the structured() method. Instead of that the Mojolicious community has invented another way to add context (mojolicious/mojo#1404). This kind of context creates another child logger for each request based on the parent application level logger $app->log. To use this child logger we have called the set() method in each Mojolicious controller action

sub ( $c ) {
  Log::Any::Adapter->set( 'MojoLog', logger => $c->log )
  ...
}

The swapping of adapters leads to problems in nonblocking model/backend code.

[2026-03-16 13:59:29.38149] [6194] [trace] [pXXpxMu3Rci-] POST "/api/deploy"
[2026-03-16 13:59:29.38183] [6194] [trace] [pXXpxMu3Rci-] Routing to a callback
[2026-03-16 13:59:29.38271] [6194] [debug] [pXXpxMu3Rci-] Log::Any: Start timer to simulate an async operation in model 'A'
[2026-03-16 13:59:29.38315] [6194] [trace] [ZShghqqC7kuD] POST "/api/deploy"
[2026-03-16 13:59:29.38393] [6194] [trace] [ZShghqqC7kuD] Routing to a callback
[2026-03-16 13:59:29.38426] [6194] [debug] [ZShghqqC7kuD] Log::Any: Start timer to simulate an async operation in model 'B'
[2026-03-16 13:59:31.38926] [6194] [debug] [ZShghqqC7kuD] Log::Any: Logging message from model 'B'
[2026-03-16 13:59:31.38986] [6194] [trace] [ZShghqqC7kuD] 201 Created (2.00667s, 0.498/s)
=> The request id in the following log line is wrong; it should be pXXpxMu3Rci- instead of ZShghqqC7kuD
[2026-03-16 13:59:34.38953] [6194] [debug] [ZShghqqC7kuD] Log::Any: Logging message from model 'A' 
[2026-03-16 13:59:34.39024] [6194] [trace] [pXXpxMu3Rci-] 201 Created (5.008702s, 0.200/s)

How could this problem be solved?

PS: If Log::Any cannot be used reliably in nonblocking code, maybe a warning should be added to its documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions