[GH-ISSUE #1961] Handler of type "service" doesn't respect configured level #846

Closed
opened 2026-03-04 03:01:23 +03:00 by kerem · 8 comments
Owner

Originally created by @micter59 on GitHub (Apr 1, 2025).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1961

Hello,

I posted an issue here, but with no response. I try my chance here.

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 ?

PHP 8.2
Symfony 7.2.3
Monolog 3.10.0

Thank you.

Michaël

Originally created by @micter59 on GitHub (Apr 1, 2025). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1961 Hello, I posted an issue [here](https://github.com/symfony/monolog-bundle/issues/503), but with no response. I try my chance here. 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 : ```php 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 : ```yaml 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 : ```yaml 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 ? PHP 8.2 Symfony 7.2.3 Monolog 3.10.0 Thank you. Michaël
kerem 2026-03-04 03:01:23 +03:00
  • closed this issue
  • added the
    Bug
    label
Author
Owner

@stof commented on GitHub (Apr 1, 2025):

When using the type service, you are responsible for configuring the service definition of your service (as MonologBundle has no idea how your class looks like). So the extra config settings are no-op (channels is not a no-op as this is about configure where the handler is injected, not about configuring the handler itself).

You need to pass the level in the parent::__construct() call in your class (as you omit the argument, the default level will be used, and it is not error but debug)

<!-- gh-comment-id:2769349269 --> @stof commented on GitHub (Apr 1, 2025): When using the type `service`, you are responsible for configuring the service definition of your service (as MonologBundle has no idea how your class looks like). So the extra config settings are no-op (`channels` is not a no-op as this is about configure where the handler is injected, not about configuring the handler itself). You need to pass the level in the `parent::__construct()` call in your class (as you omit the argument, the default level will be used, and it is not `error` but `debug`)
Author
Owner

@micter59 commented on GitHub (Apr 1, 2025):

Thanks for your quick answer. I think I understand what you mean.

I now have to find how to pass log level to my class. This will be in the "services.yaml", I am right ?
Do you have any doc elsewhere to describe how to do please ?

<!-- gh-comment-id:2769415866 --> @micter59 commented on GitHub (Apr 1, 2025): Thanks for your quick answer. I think I understand what you mean. I now have to find how to pass log level to my class. This will be in the "services.yaml", I am right ? Do you have any doc elsewhere to describe how to do please ?
Author
Owner

@micter59 commented on GitHub (Apr 1, 2025):

Just to be sure, here's the modified constructor of my class :

    public function __construct(
        private readonly EntityManagerInterface $entityManager,
        int|string|Level $level = Level::Debug,
        $bubble = true,
    )
    {
        parent::__construct($level, $bubble);
    }

But log level is not respected. That's why I'm looking in the services.yaml.

<!-- gh-comment-id:2769496468 --> @micter59 commented on GitHub (Apr 1, 2025): Just to be sure, here's the modified constructor of my class : ``` public function __construct( private readonly EntityManagerInterface $entityManager, int|string|Level $level = Level::Debug, $bubble = true, ) { parent::__construct($level, $bubble); } ``` But log level is not respected. That's why I'm looking in the services.yaml.
Author
Owner

@micter59 commented on GitHub (Apr 3, 2025):

Hello. Even, with the constructor modified as I mentioned it before, the log level is not correctly passed to my class.
I've found nothing about add something in the services.yaml.
Googling a bit more about that, i've only found this issue which seems pretty similar to what I face : https://github.com/symfony/monolog-bundle/issues/322.

<!-- gh-comment-id:2775513233 --> @micter59 commented on GitHub (Apr 3, 2025): Hello. Even, with the constructor modified as I mentioned it before, the log level is not correctly passed to my class. I've found nothing about add something in the services.yaml. Googling a bit more about that, i've only found this issue which seems pretty similar to what I face : https://github.com/symfony/monolog-bundle/issues/322.
Author
Owner

@stof commented on GitHub (Apr 3, 2025):

if you make it configurable in your own constructor, you need to configure it when configuring your service (which is not specific to monolog-bundle at all)

<!-- gh-comment-id:2775517087 --> @stof commented on GitHub (Apr 3, 2025): if you make it configurable in your own constructor, you need to configure it when configuring your service (which is not specific to monolog-bundle at all)
Author
Owner

@micter59 commented on GitHub (Apr 3, 2025):

I'm not sure to understand you well, when you say "So the extra config settings are no-op". Sorry, english is not my first language. Is the information "level" coming from the monolog.yaml file, passed to my class at any moment ? If yes, I know how to check this in my class. I could overwritte the "handle" method herited from the AbstractHandler. But for now, I can't get the information coming from the yaml file, so I hard coded it. I hope what I write is understandable...

<!-- gh-comment-id:2775660821 --> @micter59 commented on GitHub (Apr 3, 2025): I'm not sure to understand you well, when you say "So the extra config settings are no-op". Sorry, english is not my first language. Is the information "level" coming from the monolog.yaml file, passed to my class at any moment ? If yes, I know how to check this in my class. I could overwritte the "handle" method herited from the AbstractHandler. But for now, I can't get the information coming from the yaml file, so I hard coded it. I hope what I write is understandable...
Author
Owner

@stof commented on GitHub (Apr 3, 2025):

When using type: service, any configuration of the handler has to be done when configuring the service (for which you give the id in the monolog-bundle configuration). It cannot be done through the monolog-bundle semantic configuration as monolog-bundle is not the one defining the service definition for the handler in that case.

<!-- gh-comment-id:2775770878 --> @stof commented on GitHub (Apr 3, 2025): When using `type: service`, any configuration of the handler has to be done when configuring the service (for which you give the id in the monolog-bundle configuration). It cannot be done through the monolog-bundle semantic configuration as monolog-bundle is not the one defining the service definition for the handler in that case.
Author
Owner

@micter59 commented on GitHub (Apr 3, 2025):

OK. So I understand I have to put it directly in the "arguments" section of the "services.yaml", and won't be able to get it from "monolog.yaml". Thank you for your patience with me.

<!-- gh-comment-id:2775783358 --> @micter59 commented on GitHub (Apr 3, 2025): OK. So I understand I have to put it directly in the "arguments" section of the "services.yaml", and won't be able to get it from "monolog.yaml". Thank you for your patience with me.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#846
No description provided.