views:

320

answers:

1

Hi All,

I have created java program which will process different file which coming to the particular folder.In my program, I need to create log file for each incoming file for logging exception for that file. I have used the below code for that. Problem i am facing is for 1st file it creates log file and logging exception. When second file file comes, it create separate log file and logs the 2nd file's exception and meantime it logs the 2nd file's exception along with first file's exception in first file's log file. I do not want like 2nd file's exception to be append in 1st file's log file. How to do that?

private Appender myAppender;
private Logger logger = Logger.getLogger(ConfigFileReader.class.getName());

//filename is dynamic based on the incoming file
myAppender = new FileAppender(new AppXMLLayout(),filename+".log",true);
logger.addAppender(myAppender);
+5  A: 

When you add the appender for new file you need to remove the appender for the earlier file using removeAppender. In short once your processing of first file finishes remove the appender.

Bhushan