title: How to configure log4j to enable/disable logging to a file?
body: <p>I've written up a small console utility.  Throughout the application, I have used slf4j/log4j to log debug, trace, info statements to the console as I was developing it.  Now that I am done development and want to release it, I would like to disable all output to the console except for a couple of specific loggers and send all the remaining loggers to a text file.</p>
  
  <p>In theory, this should be fairly easy.  I have created 2 appenders (one ConsoleAppender and one FileAppender).  I have assocaited the ConsoleAppender with the specific loggers want on the Console and the FileAppender at the Root level.  This works fine.</p>
  
  <p>My issue now, is if I want to completely disable the FileAppender, I cannot figure out how to do it.  If I add a DenyAllFilter to the FileAppender, it will prevent anything from being written to the file, but the file is still created.  If I remove the FileAppender from the ROOT logger, I get Log4J writting to StdErr:</p>
  
  <pre><code>log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
  log4j:WARN Please initialize the log4j system properly.
  log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
  </code></pre>
  
  <p>If I set the ROOT level to OFF, the individual loggers that I have set custom debug levels to still output.</p>
  
  <p>If I set the LoggerRepository Level to OFF, then all loggers (those I want to go to STDOUT and the File) are all shut off.</p>
  
  <p>Is there any way to do this easily?  Ideally, I want to be able to disable the file appender altogether, however provide a command line switch to enable it if desired.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"&gt;
  &lt;log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'&gt;
  
      &lt;appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"&gt;
          &lt;layout class="org.apache.log4j.PatternLayout"&gt;
              &lt;param name="ConversionPattern" value="%d{ISO8601} %-5p %c{5} - %X{messageId} - %m%n" /&gt;
  &lt;!--            &lt;param name="ConversionPattern" value="%m%n" /&gt; --&gt;
          &lt;/layout&gt;
      &lt;/appender&gt;
  
      &lt;appender name="LOGTXT" class="org.apache.log4j.FileAppender"&gt;
          &lt;param name="file" value="lss-client.log" /&gt;
          &lt;param name="append" value="false" /&gt;
          &lt;layout class="org.apache.log4j.PatternLayout"&gt;
              &lt;param name="ConversionPattern" value="%d{ISO8601} %-5p %c{5} - %X{messageId} - %m%n" /&gt;
          &lt;/layout&gt;
  &lt;!--        &lt;filter class="org.apache.log4j.varia.DenyAllFilter"/&gt; --&gt;
      &lt;/appender&gt;
  
      &lt;!-- Spring Loggers --&gt;
      &lt;logger name="org.springframework"&gt;
          &lt;level value="INFO" /&gt;
      &lt;/logger&gt;
  
      &lt;logger name="org.springframework.ws.client.MessageTracing.sent"&gt;
          &lt;level value="TRACE" /&gt;
      &lt;/logger&gt;   
  
      &lt;logger name="org.springframework.ws.client.MessageTracing.received"&gt;
          &lt;level value="TRACE" /&gt;
      &lt;/logger&gt;   
  
      &lt;logger name="com.cws.cs.Client" &gt;
          &lt;level value="INFO" /&gt;
          &lt;appender-ref ref="STDOUT" /&gt;
      &lt;/logger&gt;
  
      &lt;root&gt;
          &lt;level value="INFO" /&gt;
          &lt;appender-ref ref="LOGTXT" /&gt; 
      &lt;/root&gt;
  &lt;/log4j:configuration&gt;
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>Eric</p>