title: Log4j to create dynamic files
body: <p>I have a file processing JAVA Code, While processing files, I want to log all the file processing details like file has this no of lines and it has some "Hello" Strings in a Seperate Log file for Each file am processing.</p>
  
  <p>The code I tried here works fine, but if I have 2 file like file1.txt and file2.txt.The logger logs file1.txt details in log_file1.log then while log the file2.txt in log_file2.log it retains the log_file1.log contents also in log_file2.log .</p>
  
  <p><strong>File1.txt</strong></p>
  
  <pre><code>Hello How are you
  Hello How are you
  </code></pre>
  
  <p><strong>log_file1.log</strong></p>
  
  <pre><code>it has 2 lines and 2 Hello Strings
  </code></pre>
  
  <p><strong>File2.txt</strong></p>
  
  <pre><code>Hello How are you
  Hello How are you
  Hello How are you
  </code></pre>
  
  <p><strong>log_file2.log</strong></p>
  
  <pre><code>it has 2 lines and 2 Hello Strings
  it has 3 lines and 3 Hello Strings
  </code></pre>
  
  <p><strong>Code I tried :</strong></p>
  
  <pre><code>public class DummyLog
  {
      private static PatternLayout patternLayout = new PatternLayout("%d{ISO8601}\t%p\t%c\t%m%n");
  
      public static Logger getLogger(String fileName, Class clazz) throws Exception
          {
              fileName = "D:/New folder/log-properties/" + fileName + ".log";
              Logger logger = Logger.getLogger(clazz);
              FileAppender appender = new DailyRollingFileAppender(patternLayout,
                              fileName, "'.'yyyy-MM-dd");
              logger.addAppender(appender);
              logger.setLevel(Level.DEBUG);
              return logger;
          }
  }
  </code></pre>
  
  <p><strong>Main Class :</strong></p>
  
  <pre><code>public class TestDummyLog
  {
      public static void main(String args[]) throws Exception
      {
          for(int i = 2;i &gt; 0;i--)
          {           
                   String loggerName = "Log1_"+new SimpleDateFormat("yyyyddMMHHmmssSSS").format(new Date());
                   Logger logger = DummyLog.getLogger(loggerName, TestDummyLog.class);
                   logger.info("Joseph Micheal TestLogger:" +new SimpleDateFormat("yyyyddMMHHmmssSSS").format(new Date()));
                   new TestLoggerChild().getChileStuff(loggerName);
                   Thread.sleep(6000);
          }
  
      }
  }
  </code></pre>
  
  <p><strong>Child Class :</strong></p>
  
  <pre><code>public class TestLoggerChild
  {
  public static Logger logger = null;
  public void getChileStuff(String fileName) throws Exception
  {
      logger = DummyLog.getLogger(fileName,TestLoggerChild.class);
      logger.info("Child Sample info message"+new SimpleDateFormat("yyyyddMMHHmmssSSS").format(new Date()));
      logger.info("Class :::::: TestLoggerChild.class");
      logger.debug("fileName @@@@@@@@@@@@@:" + fileName);
      logger.info("Sample info message");
      logger.warn("Sample warn message");
      logger.error("Sample error message");
      logger.info("Sample info message ");
      logger.info("Class :::::: TestLoggerChild.class");
  }
  }
  </code></pre>