Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
892 views
in Technique[技术] by (71.8m points)

logging - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...