Skip to content

Forward Log::withContext / Log::shareContext to FrankenPHP request-logs - #54

Open
LauJosefsen wants to merge 3 commits into
masterfrom
lejo/forward-log-context-to-frankenphp-request-logs
Open

Forward Log::withContext / Log::shareContext to FrankenPHP request-logs#54
LauJosefsen wants to merge 3 commits into
masterfrom
lejo/forward-log-context-to-frankenphp-request-logs

Conversation

@LauJosefsen

Copy link
Copy Markdown
Contributor

Summary

  • Adds ForwardingLogManager that extends Illuminate\Log\LogManager and forwards context attached via Log::withContext() and Log::shareContext() into the FrankenPHP request-logs extension (\Cego\RequestLogs\context()).
  • FilebeatLoggingServiceProvider::register() rebinds the log singleton to the forwarding manager.
  • When the FrankenPHP extension is absent (php-fpm, CLI, queue workers) the forward is a no-op — standard Laravel logging behavior is preserved.

Context

Companion to cego/frankenphp-image!264, which adds a Go-side per-request context store and the PHP function \Cego\RequestLogs\context(). This package is the idiomatic Laravel bridge: application code calls Log::withContext(['user' => ['id' => $id]]) from middleware, and the fields appear on both the Monolog app-log records AND the final access-log line emitted by the Caddy request-logs middleware.

Not forwarded

Log::channel('x')->withContext([...]) — per-channel scoping does not map cleanly to a cross-cutting request log entry. Documented in the README section.

@LauJosefsen LauJosefsen self-assigned this Apr 23, 2026
Without clearing the facade's resolved-instance cache, Log::shareContext
and Log::withContext can hit a stale LogManager that pre-dates our
rebind, so context never reaches the request-logs extension.
The FrankenPHP request-logs extension uses frankenphp.GoMap to convert
the PHP array to Go primitives, which silently drops the entire call
when it hits a PHP object (UuidInterface, Carbon, etc.). Round-trip
through json_encode/decode so objects flatten to their JSON scalar
representation and survive the conversion. The original array is
untouched, so parent::shareContext() / Monolog handlers still receive
the objects unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant