Skip to content

Handler of type "service" doesn't respect configured level #503

@micter59

Description

@micter59

Hello,

My goal is to store logs with level "error" in a DB, just to facilitate their readings from inside my app. I've written a simple custom handler :

class MonologDoctrineHandler extends AbstractProcessingHandler
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
    )
    {
        parent::__construct();
    }


    /**
     * @inheritDoc
     */
    protected function write(LogRecord $record): void
    {
        $logEntry = new ApplicationLog();

        $logEntry
            ->setMessage($record['message'])
            ->setLevel($record['level'])
            ->setLevelName($record['level_name'])
            ->setExtra($record['extra'])
            ->setContexte($record['context'])
        ;

        $this->entityManager->persist($logEntry);
        $this->entityManager->flush();
    }
}

The service is declared in the "services.yaml" file, with the needed entitymanager as argument :

    monolog.doctrine_handler:
        class: App\Manager\Tools\MonologDoctrineHandler
        arguments: ['@doctrine.orm.default_entity_manager']

And I've added this handler in the configuration of the monolog bundle :

when@dev:
    monolog:
        handlers:
            main:
                type: rotating_file
                path: "%kernel.logs_dir%/%kernel.environment%_main.log"
                level: debug
                channels: [ "!security", "!audit" ]
                max_files: 30
            db:
                type: service
                id: monolog.doctrine_handler
                level : error
                channels: ["!security", "!audit", "!doctrine"]

Log entries are effectively written to the database, but the "level: error" in the monolog.yaml is not respected. All log levels are written (info, debug are written for example). I could filter this in my handler, but this would be more practical to be able to change this as needed in the yaml file. Is this a bug, or do I missed something in my configuration or in the documentation ?

Thank you.

Michaël

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions