I need to add trackability information for all logs.

E.g. logged in user and route parameters (entity ID)

Right now I have this code:

use Monolog\Processor\ProcessorInterface;

class RequestProcessor impelements ProcessorInterface {
    public function __construct(
        private RequestStack $requestStack,
    ) {
    }

    public function __invoke(LogRecord $record): LogRecord {
        $request = $this->requestStrack->getCurrentRequest();

        $record = $record->with(
            context: [
                'route' => $request->attributes->get('_route'),
            ],
        );

        return $record;
    }
}

Problem is that some logs (e.g. deprecations) are logged before `Kernel::request` event. That means there is no "current request" in strack.

Same with logged in user information.

---

Tried adding Bulk Handler, but it seem no processor were executed after flush, as processors are applied before passing log to handler

monolog:
    handlers:
        info_buffer:
            type: buffer
            handler: info_only

        info_only:
            type: filter
            handler: info_log
            channels: ['!database', '!elasticsearch_client', '!security']
            accepted_levels:
                - info

        info_log:
            type: stream
            level: debug
            bubble: false
            channels: ['!database']
            path: "%kernel.logs_dir%/info.jsonl.log"
            formatter: app.log.formatter.json

3 Replies 3

Can you, please, explain what is the problem with your solution?

Does it throw an error when you hit deprecation logs?

If so have you tried to add condition to your invoke function? Eg. only process logs when current request is present?

Do you care about deprecation logs? If so then you need to use something else than

$this->requestStrack->getCurrentRequest()

But if you really can ignore deprecation logs (eg. do not track the user etc) you can simply ignore those eg. do this:

use Monolog\Processor\ProcessorInterface;
// you also are missing use for RequestStack and LogRecord here but I guess you've just forgotten to copy them to your question

class RequestProcessor implements ProcessorInterface {
    public function __construct(
        private RequestStack $requestStack,
    ) {
    }

    public function __invoke(LogRecord $record): LogRecord {
        $request = $this->requestStack->getCurrentRequest();

        // Check if the current request exists
        if ($request === null) {
            return null; // Return null if there is no current request or do whatever you want
        }

        $record = $record->with(
            context: [
                'route' => $request->attributes->get('_route'),
            ],
        );

        return $record;
    }
}

Does that make sense to you? If this is not sufficient, please, let me know so we can try to find another solution together.

Problem is that some logs (e.g. deprecations) are logged before Kernel::request event. That means there is no "current request" in strack.

Sure, but as that will ever be you need something else to learn about the relation.

You could for example create an object for a universally unique ID and configure the dependency injection to provide it as singleton.

You then can make your constructor require it and use it as a correlation-ID in your logging.

You then can identify all diagnostic messages that belong to the same relation and you therefore have undefined dependencies that you are currently think you have and all those that you will have in the future.

This is such a general concept in logging, that perhaps the functionality already exists for Monolog: https://packagist.org/packages/mdavid-dev/symfony-correlation-id-bundle (I didn't know so just felt lucky).

What I ended up doing:

In YAML configuration, enabled buffered logs. Then created new Event Listener that on LoginSuccessEvent and LoginFailureEvent would inject proper information into custom logs formatter.

This way processors are still processing log-record specific data, but formatter is appending more of generic "global" information.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.