Log4j FileAppender error on Tomcat server

I am working on a web application and I have a requirement to create runtime log files for my impex process. Here is an example of use. I check the XML file, and the check error is handled by a special error handler. This hanlde error will be passed to the base validator (Jaxb 2.x validator), so I need to create a log file when an instance of this hanlder error is created. we use log4j as a logging API

here is the code to create the log file at runtime

XMLErrorHandler<Object> errorHandler=new XMLErrorHandler<Object>(logFileLocation); logFileName=errorHandler.getLogFileName(); validator.setErrorHandler(errorHandler); validator.validate(source); 

I am creating a log file inside the XMLErrorHandler constructor, since this is the only point I have control at, is the code to create the log file.

 FileAppender appender; try { appender = new FileAppender(layout, sb.toString(), false); log.addAppender(appender); log.setAdditivity(false); log.setLevel(Level.WARN); } catch (IOException e) { e.printStackTrace(); } 

everything works fine, and the file is created correctly and recorded by the registrar in the appropriate place. but if I restart my server, the real problem will start, and the registrar will begin to add the contents of the log not only to the current log file, but to the entire file created for this process during server operation. Here are the details, which suggests that I have 3 log files (A, B, C) already in place with 3 lines in each log file, and C is the last file created in this process. so when I restart my server (by renaming, I mean that I stopped tomcat from the console), some of them, like adding data to all previous log files in this fashin C has 3 more lines B has now 6 lines A has now 9 lines

It seems that the appender I created is still open or has a refrence, not sure what exactly is happening. any help in this regard would be helpful.

+6
java logging tomcat6 log4j
source share
3 answers

The problem is resolved. The problem was with the application that I used. There was an impression that log4j handles closing the appender by itself, but it didnโ€™t (I saw the FileAppender code as it was trying to close the appender). so I closed the application myself and solved the problem.

+1
source share

Additional information required. When you say โ€œreboot my serverโ€, does this mean that the entire JVM is restarted or just threads in the JVM? I used to have a problem like this with a special daemon. When I thought that the server was restarted, it did not actually kill previous instances of the daemon and JVM, so I had several instances that were working, updating, and conflicting with each other.

0
source share

before log.addAppender(appender); you can write log.removeAllAppenders();

0
source share

All Articles