Search code examples
phploggingsymfony4monolog

Use LogLevels in Monolog (Symfony)


coming from Java development, I learned to appreciate LogLevel, how to set it in Logback or Log4j.

Monolog is used in my symphony 4.2 project.

I would like to see that from a certain controller/namespace the log entries with level info can also be seen in the production log file without all the other log entries with the info level filling the log file.

Unfortunately I didn't find any explanations.


Solution

  • After a little more research, I configured my monolog.yaml for dev and also prod this way

    monolog:
      channels: ['appinfo']
      handlers:
        custom:
          channels: ['appinfo']
          level: info
          max_files: 30
          path: "%kernel.logs_dir%/appinfo.log"
          type: rotating_file
    

    The important thing is the channel, appinfo in my case.

    The handler, custom in my case, can be named any way you like.

    Then in the services.yaml one has to define the "type" of the injected logger.

    App\Controller\DefaultController:
      arguments:
        $logger: '@monolog.logger.appinfo'
    

    This works with controllers as with services