21

Can I disable an appender in logback on the xml config? I have my configuration and I want to put two appenders, one for database and other for text logs, but only one must be activated. thanks!

Rys
  • 4,934
  • 8
  • 21
  • 37

2 Answers2

15

Not sure why you want to deactivate an appender, what are you trying to achieve by disabling.

There are some ways to achieve it

  1. Add the appender in logback.xml and keep it commented. When you like to enable it then uncomment the appender and reload logback configuration (http://logback.qos.ch/manual/configuration.html#autoScan)
  2. Add a logger like the one given below and use appropriate logger for logging
    <configuration>
      <appender name="stdoutappender" />
      <appender name="dbappender" />
      <logger name="stdoutlogger" level="DEBUG">
        <appender-ref ref="stdoutappender" />
      </logger>

      <logger name="dblogger" level="OFF">
        <appender-ref ref="dbappender" />
      </logger>
    </configuration>   

In this case also you have to reload the configuration when you modify logback configuration (logback.xml)

  1. If you know the conditions (to activate/deactivate) beforehand then use if else block to enable/disable

On top of above 3 options you can create logback configurations progamatically

Radek Postołowicz
  • 4,506
  • 2
  • 30
  • 47
Kaushal
  • 665
  • 2
  • 9
  • 21
  • Thanks!! At the end, I put my dblogger level="OFF" and as you describe, I make a little change on my config adding the autoscan every 10 minutes, its a great idea! – Rys Nov 26 '13 at 13:01
  • I'm here like a decade later :) But one reason why someone might want to disable an appender is if they're creating a server that users can configure via some config file. It might be nice to allow users to say, "Look, it's nice that you want to offer me file logging, but space is precious and I don't need it, just turn it off." But there's no harm in leaving console logging on in that case. – dherman Apr 13 '23 at 23:15
14

The easy way to choose a logging level for an appender is to use a ThresholdFilter, e.g.:

<appender name="ap.Console" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>${logging.appender.console.level:-OFF}</level>
    </filter>
    <encoder>
        <pattern>${pattern}</pattern>
    </encoder>
</appender>

When you want to activate a particular appender you should run your jvm with an appropriate -D option. For the appender defined above it would be:

java -Dlogging.appender.console.level=DEBUG

Of course if you activate automatic configuration reloading (http://logback.qos.ch/manual/configuration.html#autoScan) you can change filter level while the app is running.

The approach I've proposed is convenient when you don't want to change your logback config file every time you start the app (with different logging levels). You simply have to set properties used in logback config by running jvm with corresponfing -D options.

tomek
  • 771
  • 1
  • 7
  • 19