1

File loggerAm processing files using java , there are 2 or 3 Java components to process files.My requirement is for file must have a log file and its processing details will be logged in relevant log file.

Problem is for single file it works well but with multiple file am getting problem. When multiple files are getting processed logger is logging log detail of file1.txt logs to file2.log instead of file1.log...

public Class FileProcessComponent1
{
public void process(String fileName)
{
  Logger Log =  Logg.getLogger1(fileName,this.getClass());
  log.info("file1 logging");
}
}

public Class FileProcessComponent2
{
public void process(String fileName)
{
  Logger Log =  Logg.getLogger1(fileName,this.getClass());
  log.info("file1 logging");
}
}


public Class Logg
{
      public static Logger getLogger1(String fileName,Class clazz) throws Exception
      {
          if(fileName == null || "".equals(fileName.trim()))
              throw new Exception("File Name or Map for File Name is Null");        

            fileName = "/home/logs/"+fileName+".log";
            Logger logger = Logger.getLogger(clazz.getCanonicalName()+":"+System.nanoTime());
            logger.setAdditivity(false);
            FileAppender appender = new DailyRollingFileAppender(new PatternLayout("%d{ISO8601}\t%p\t%c\t%m%n"),    fileName, "'.'yyyy-MM-dd");
            logger.addAppender(appender);
            logger.setLevel(Level.DEBUG);
            return logger; 
      }
}
5
  • Your two components both pass the same file name ("file1") to getLogger1(). Not surprising they both write to the same file. Commented Sep 28, 2013 at 8:10
  • yes both use the same file name Commented Sep 28, 2013 at 8:23
  • So, what's your question? You want them to write to diferent files, but you always pass the same file name. So obviously, they all write to the same file. Pass a different file name, to write to a different file. Commented Sep 28, 2013 at 8:29
  • Thanks for reply sorry for inconvenience even after passing dynamic file based on the file to processed it is writing file1.txt log to file2.log Commented Sep 28, 2013 at 8:37
  • yes i want different log files for different files to be processed. Commented Sep 28, 2013 at 8:39

1 Answer 1

1

I think you want to create a logger pointing to a unqiue file for each files comes for processing.

Try this,

a) At the point where you start processing a new file, Create a new Logger with file name as Loggername. b) Create the Appender with filename.log and assign the appender to the logger.

Now, when you try to get the logger, always use the filename in getLogger().

Once you are done with processing a file, remove the logger/appender. (this is very important)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help Could give me the way to remove the logger/appender.
remove all appenders from logger and close the appender.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.