Skip to content

Add LogContext processor for automatic log context - #12

Open
dcolt wants to merge 1 commit into
stefna:developfrom
dcolt:develop
Open

dcolt wants to merge 1 commit into
stefna:developfrom
dcolt:develop

Conversation

@dcolt

@dcolt dcolt commented Aug 24, 2026

Copy link
Copy Markdown

Add LogContext and LogContextProcessor to attach tracked context to every log record. Each key holds a single value managed via set()/unset()/clear(), and the processor merges them into the record context unless overridden explicitly. Includes tests and README docs.

Add LogContext and LogContextProcessor to attach tracked context to
every log record. Each key holds a single value managed via
set()/unset()/clear(), and the processor merges them into the record
context unless overridden explicitly. Includes tests and README docs.
@sunkan

sunkan commented Aug 24, 2026

Copy link
Copy Markdown
Member

Not really what I had in mind

I was more thinking of like this

class DoSomethingHandler {
    public function __construct(
        private RequestLogContext $logContecxt,
    ) {}

    public function __invoke($command) {
        $this->logContext->add('plate', $command->plate);
        .....
    }
}

class DoSomethingCronHandler {
    public function __construct(
        private RequestLogContext $logContecxt,
    ) {}

    public function __invoke($command) {
        $this->logContext->add('job', 'processing xxx');
        .....
        $this->logContext->remove('job', 'processing xxx');
    }
}

class AuthMiddleware {
    public function __construct(
        private RequestLogContext $logContecxt,
    ) {}

    public function handle(Request $request) {
        if (authenticated()) {
            $this->logContext->add('account', $account->id);
        }
....
    }
}

And there should probably be a log context contract that can be implemented so that we can create scoped log contexts and so forth.

@sunkan

sunkan commented Aug 24, 2026

Copy link
Copy Markdown
Member

An other option would be

class DoSomethingHandler {
    public function __construct(
        #[LogContextScope('request')]
        private LogContext $logContecxt,
    ) {}

    public function __invoke($command) {
        $this->logContext->add('plate', $command->plate);
        .....
    }
}

class DoSomethingCronHandler {
    public function __construct(
        #[LogContextScope('cron')]
        private LogContext $logContecxt,
    ) {}

    public function __invoke($command) {
        $this->logContext->add('job', 'processing xxx');
        .....
        $this->logContext->remove('job', 'processing xxx');
    }
}

class AuthMiddleware {
    public function __construct(
        #[LogContextScope('request')]
        private LogContext $logContecxt,
    ) {}

    public function handle(Request $request) {
        if (authenticated()) {
            $this->logContext->add('account', $account->id);
        }
....
    }
}

@dcolt

dcolt commented Aug 24, 2026

Copy link
Copy Markdown
Author

Of the two suggestions, I absolutely prefer the attribute variant.

Seems both more robust and flexible, but also cleaner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants