No logging in production environment when running Console commands

Viewed 2000

When running Console commands in prod mode, I need to log Doctrine related debug messages. Everything works fine in dev with the following configuration, so I assume I forgot something to set when in prod?

My system:

  • PHP 7.3
  • Symfony 4.4
  • Monolog
  • Doctrine

How do I run commands:

I run commands in prod as either

php bin/console app:scrape --env=prod

or

# set APP_ENV=prod in .env.local before
php bin/console app:scrape

Both result in no logs. I am sure, I run prod, because Symfony creates var/cache/prod every time.

Monolog configuration file: config/package/prod/monolog.yaml

This file configures Monolog in prod environment.

monolog:
    handlers:
        main:
            type: fingers_crossed
            action_level: debug
            handler: nested
            excluded_http_codes: [404, 405]
        nested:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
        deprecation:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.deprecations.log"
        deprecation_filter:
            type: filter
            handler: deprecation
            max_level: info
            channels: ["php"]
        doctrine:
            level: debug
            type: stream
            path: "%kernel.logs_dir%/doctrine/info.log"
            channels: ["doctrine"]

Output of APP_ENV=prod bin/console debug:config monolog:

https://gist.github.com/k00ni/419f62941e496a376be35a0d06e44131

1 Answers

Maybe you could have a main handler that is grouped so that it will pass messages with both handlers (your current main and doctrine):

# config/packages/monolog.yaml
monolog:
    handlers:
        main:
            type: group
            members: ["doctrine", "default"]
        doctrine:
            level: debug
            type: stream
            path: "%kernel.logs_dir%/doctrine/info.log"
            channels: ["doctrine"]
# config/package/prod/monolog.yaml
monolog:
    handlers:
        default: # formerly main
            type: fingers_crossed
            action_level: error
            handler: nested
            excluded_http_codes: [404, 405]
        nested:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
        deprecation:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.deprecations.log"
        deprecation_filter:
            type: filter
            handler: deprecation
            max_level: info
            channels: ["php"]
Related