title: How to set additivity to false when the Appender is added programatically
body: <p>I am new to log4j. My requirement is to create a new log file during every iteration. Basically during every iteration, below is what I need to attain:</p>
  
  <ol>
  <li>Create a folder with current timestamp as the folder's name</li>
  <li>Create a log file with name log.txt</li>
  <li>Log the transactions of that iteration in log.txt</li>
  </ol>
  
  <p>I have given a mock up of the implementation i need.</p>
  
  <p><strong>log4j.properties</strong></p>
  
  <pre><code>log4j.rootLogger=INFO
  
  log4j.appender.tracerLog=org.apache.log4j.FileAppender
  log4j.appender.tracerLog.File=logs/${log4j.test.start.timestamp}/tracer.log
  log4j.appender.tracerLog.layout=org.apache.log4j.PatternLayout
  log4j.appender.tracerLog.layout.ConversionPattern=%d %-5p - %m%n
  
  log4j.category.traceLogger=TRACE, tracerLog
  log4j.additivity.traceLogger=false
  </code></pre>
  
  <p>java code</p>
  
  <pre><code>package app.testscript;
  
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  import org.apache.log4j.FileAppender;
  import org.apache.log4j.LogManager;
  import org.apache.log4j.Logger;
  import org.apache.log4j.PatternLayout;
  
  public class Log4jTest {
      static{
          SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_hh_mm_ss_S");
          System.setProperty("log4j.test.start.timestamp", dateFormat.format(new Date()));
      }
      Logger traceLogger = Logger.getLogger("traceLogger");
      Logger rootLogger = Logger.getRootLogger();
  
      Logger testScriptLogger = null;
  
      public void doSomething() {
  
          try {
              SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_hh_mm_ss_S");
              PatternLayout patterLayout = new PatternLayout("%m%n");
              String folderNameToCreate = dateFormat.format(new Date());
  
              FileAppender fileAppender = new FileAppender(patterLayout, "C:/MyTestLog/" + folderNameToCreate + "/log.txt", true);
              fileAppender.setName("A" + folderNameToCreate);
              fileAppender.activateOptions();
              rootLogger.addAppender(fileAppender);           
  
              try {
                  testScriptLogger = LogManager.getLogger("A" + folderNameToCreate);
              }catch(Exception e1) {
                  System.out.println("Exception e: ");
                  e1.printStackTrace();
              }
              //testScriptLogger.setAdditivity(false);
              testScriptLogger.info("Message to capture in log - " + folderNameToCreate);
          }catch(Exception e) {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          Log4jTest obj = new Log4jTest();
          for (int i=0; i&lt;2; i++) {
              obj.doSomething();
              try {
                  Thread.sleep(2000);
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
  
      }
  
  }
  </code></pre>
  
  <p><strong>Result of the above Code</strong>:</p>
  
  <ol>
  <li>I can see there are 2 folders created (folder_1 and folder_2)</li>
  <li>Both the folders have the log.txt in it</li>
  <li>log.txt file created (inside folder_1) during iteration 1 have 2 lines in it (log related to iteration 1 and iteration 2)</li>
  <li>log.txt file created (inside folder_2) during iteration 2 have one line it (log related to iteration 2)</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/OpuHN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OpuHN.jpg" alt="enter image description here"></a></p>
  
  <p><strong>Expected Result</strong> is to have 1 line in both the log.txt files (created during iteration 1 and 2)</p>
  
  <p><a href="https://i.stack.imgur.com/UDnB8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UDnB8.jpg" alt="enter image description here"></a></p>
  
  <p>When using <code>testScriptLogger.setAdditivity(false);</code> , I get the below WARNING message and nothing is written to the file. </p>
  
  <pre><code>log4j:WARN No appenders could be found for logger (A20170518_03_49_10_802).
  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>Please help me with a solution. Thanks</p>