Search code examples
symfonyloggingconsole-applicationsymfony-3.4

Symfony 3 Console exception logging only displayed on console


In our Symfony 3 app all logging from console commands (e.g. when there's an exception) are nicely formatted and displayed to the screen... which when dealing with cron jobs in production mode is completely useless.

All exception logging from HTTP requests are routed through log files on the server and reports sent to us via Swifmail (as per https://symfony.com/doc/3.4/logging/monolog_email.html)

I need to get logs from console commands treated the same as logs from HTTP requests but the Symfony documentation seems to ignore this scenario?

our config:

monolog:
    handlers:
        main:
            type: fingers_crossed
            action_level: critical
            handler: grouped
        grouped:
            type: group
            members: [streamed, deduplicated]
        streamed:
            type: stream
            path: '%kernel.logs_dir%/%kernel.environment%.log'
            level: debug
        deduplicated:
            type: deduplication
            handler: swift
        swift:
            type: swift_mailer
            level: debug
            formatter: monolog.formatter.html
            content_type: text/html
        console:
            type: console
            process_psr_3_messages: false


Solution

  • Symfony 3 logged console exceptions at Monolog's error(400) level.

    In our app, we had monolog.handlers.main.action_level set to critical(500).

    To fix this the options were

    a) patch Symfony\Component\Console\EventListener\ErrorListener to call $this->logger->addCritical instead of $this->logger->error

    or

    b) take the config level down from critical to error and put up with getting error reports for all the HTTP 4?? errors too.