monolog-bundle icon indicating copy to clipboard operation
monolog-bundle copied to clipboard

enable or disable handlers with env vars

Open mathroc opened this issue 7 years ago • 7 comments

Hi,

we deploy a docker image in production, this image contains the source code, cache and assets (in short, we run composer install in the Dockerfile)

the problem is that logs are pushed to logstash with the gelf handler and this can't work in the image build process (we don't want to log to logstash at that point anyway)

I would like to enable a StreamHandler when in the docker build and the GelfHandler when the image is deployed, ideally the config would look something like this:

parameters:
  env(LOG_TO_LOGSTASH): true
  env(LOG_TO_STDERR): false

monolog:
    handlers:
        logstash:
            type: gelf
            level: warning
            ...
            enabled: '%env(bool:LOG_TO_LOGSTASH)%'
        main:
            type:  stream
            level: warning
            path:  "php://stderr"
            enabled: '%env(bool:LOG_TO_STDERR)%'

and in the Dockerfile I could do:

...

RUN LOG_TO_LOGSTASH=false LOG_TO_STDERR=true composer install

mathroc avatar Jul 25 '18 16:07 mathroc

env variables are available only at runtime, not at build time. So supporting that would mean that we would still need to instantiate the handler and put it in the logger (as we cannot change the object graph generated by the container at that point), and have a setting in the handler to make it a no-op (based on the runtime config). But handlers don't have such capability.

stof avatar Jul 25 '18 16:07 stof

yeah that's what I starting to fear while reading the code in this repository. At first I was thinking this could be added to the handler but as it would probably need to be added to Monolog\Handler\AbstractHandler I doubt it would be accepted as an addition to monolog.

My second idea (and last for now) would be to wrap the handler into a ToggleableHandler. if it can be detected whether enabled is specified, it could be ignored at compile time if not needed

mathroc avatar Jul 25 '18 16:07 mathroc

so I solved it in my project by manually creating the handlers and decorating them with a ToggleableHandler

would adding a similar mechanism into the bundle be a welcome addition ? if so I can work on a PR, otherwise feel free to close this issue

thx for the quick feedback @stof

mathroc avatar Jul 31 '18 09:07 mathroc

Maybe this could be achieved by combining two changes that don't introduce too specific behavior:

  1. Add a log level OFF in the configuration only, so that a logger with this log level doesn't log anything (Java's log4j does this). I didn't check the implications though.
  2. Enable log level configuration by environment variables (I proposed a PR some time ago: https://github.com/symfony/monolog-bundle/pull/245).

umulmrum avatar Oct 31 '18 16:10 umulmrum

Technically passing 999 as log level would mean most handlers would be disabled. AFAIK this wouldn't cause any issues.

Seldaek avatar Dec 26 '18 22:12 Seldaek

Hi, I encountered a similar problem. Setting 999 is not a solution as it always try to reach graylog (via gelf handler). monologBundle is now in version 3.10. Will it be implemented?

ppodgorskicrido avatar Dec 11 '23 08:12 ppodgorskicrido