Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I disable an appender in logback?

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!

like image 216
Rys Avatar asked Nov 11 '13 18:11

Rys


People also ask

What is Appender in Logback?

The Logback architecture is comprised of three classes: Logger, Appender, and Layout. A Logger is a context for log messages. This is the class that applications interact with to create log messages. Appenders place log messages in their final destinations. A Logger can have more than one Appender.

How do I disable Logback log?

Logback does not allow logging to be disabled from the command line. However, if the configuration file allows it, you can set the level of loggers on the command line via a Java system property.

What is async Appender in Logback?

AsyncAppender. Logs the log events asynchronously. It acts solely as an event dispatcher and must reference another appender. AsyncAppender acts as a dispatcher to another appender.

Does Logback require slf4j?

Note that logback-classic transitively includes the slf4j-api and logback-core , so only having the logback-classic is enough to setup logback with slf4j. When using Logback with SLF4j, a console appender with DEBUG log level is configured automatically.


1 Answers

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

like image 115
Kaushal Avatar answered Sep 22 '22 16:09

Kaushal