1
votes

I have two appenders: console and Kibana appender. When running on dev environment I want to have possibility to disable Kibana appender.

I've tried multiple way to achieve it, but nothing works:

  1. How to remove appender from logger in log4j2 programmatically? - even if I remove appender it still sends logs

  2. How do I conditionally add log4j2 appender depending on java system property? <AppenderRef ref="elasticsearchAsyncBatch" level="${sys:logLevel:-all}"/> - appender ignore variables passed from command line via: export LOG_LEVEL=off and always use default value, all in this example. Script solutions has the same problem

  3. <ThresholdFilter level="${sys:logLevel}" onMatch="ACCEPT" onMismatch="DENY"/> - level attribute doesn't accept dynamic values and throws error that it cannot cast "${sys:logLevel}" to Level enum

Log4j ver 2.11.2

1

1 Answers

1
votes

The issue with option 1 is unclear from the information you have provided. How did you remove the appender? You cannot just remove it from the configuration as all the configured loggers will still be pointing to it. You would have to modify all the configured loggers appender references to point to a different appender first.

For solution 2 you have specified ${sys:loglevel:-all} but then tried to set its value via an Environment variable. You need to start your application with -Dloglevel=DEBUG for that to work. Alternatively you can specify ${env:LOG_LEVEL:-all} and use the environment variable you have set.

I expect solution 3 is the same problem as solution 2. Since you didn't specify a default value when it doesn't find the loglevel system property it just returns itself as the string, which obviously is an invalid value.