Traveler
Traveler

Reputation: 143

Disable creating logback log file

Is it possible to disable creating logback log files?

When I set level to root or logger elements to OFF, it will not log but the file specified in appender is still created.

Is there an option to disable creating this file? (other than deleting the config xml file)

Even if no logger (or root) links to the appender, the log file from appender is still created.

Thank you.

=========================================================================== Edit. I'm attaching my config:

<?xml version="1.0" encoding="UTF-8"?>

<configuration debug="false">

  <variable name="LOG_LEVEL" value="${mylevel:-TRACE}" /> 

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>${clogdir}/mylog.log</file>
    <append>true</append>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <pattern>%d %p %t %c - %m%n</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>${clogdir}/mylog.log.%d{yyyy-MM-dd}</fileNamePattern>
    </rollingPolicy>
  </appender>  

  <root level="${LOG_LEVEL}">
    <appender-ref ref="FILE" />
  </root>

  <appender name="log2" class="ch.qos.logback.core.FileAppender">
    <file>${clogdir}/log2.log</file>
    <append>true</append>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <pattern>%d %p %t %c - %m%n</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>${clogdir}/log2.log.%d{yyyy-MM-dd}</fileNamePattern>
    </rollingPolicy>
  </appender>

  <logger name="log2" level="TRACE">
   <appender-ref ref="log2" />
  </logger>

</configuration> 

Upvotes: 1

Views: 2407

Answers (1)

glytching
glytching

Reputation: 47895

As long as the file appender is declared in your configuration then Logback will create an instance of the appender class and invoke start() on it. You can see this in AppenderAction.end(). Calling start() on a FileAppender will cause the appender's configured file to be created, you can see this behaviour in FileAppender.start().

So, in order to fully disable the file output appender you must ensure that it is not declared in your Logback configuration.

Perhaps you are looking for some way to mark your file output appender as a no-op i.e. no events to be emitted to it and no file to be created. There is no way - with Logback's current implementation (and subclasses) of FileAppender - to mark the appender as a complete no-op. To implement this you would need to provide your own subclass of FileAppender and override start().

Upvotes: 1

Related Questions