I am working on a webapplication and i have a requirment to generate log files at run time for my impex process.here is the use case i am validating an XML file and validation error is being handled by custom Error handler.This error hanlde will be passed to the underlying validator (Jaxb 2.x validator),so i have to create the log file when the instance of this error hanlder is being created. we are using log4j as logging API
here is the code to create log file at run time
XMLErrorHandler<Object> errorHandler=new XMLErrorHandler<Object>(logFileLocation);
logFileName=errorHandler.getLogFileName();
validator.setErrorHandler(errorHandler);
validator.validate(source);
i am creating the log file inside XMLErrorHandler constructor since this is the only point i have control here is the code for the log file creation
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 is working fine and file is being created correctly as well being written by the logger to the respective place. but if i restart my server real problem starts and logger start appending the log content not only to its current log file but also for all file being created for this process while server is running. Here is the details lets suppose i have 3 log (A,B,C) files already at the location with 3 lines in each log file and C was the latest file created in the process. so when i restart my server(By resrarting i mean i stopped tomcat from the console) it some how appending data in to previos all log files in this fashin C has still 3 lines B has now 6 lines A has now 9 lines
it seems that the appender i hace created is still open or have refrence, not sure what exactly going on. any help in this regard will be helpfull.
Servlet Engine Apache Tomcat x, 9.0. x, 10.0. x and 10.1. x) have no dependency on any version of log4j.
Each server instance contains its own Catalina.This file is located in the logs directory below the Tomcat root directory. This log is the system's output log, which also consists of standard error messages. These files are saved daily (MM-DD-YYYY) with the date appended to the name of the data.
As of Tomcat 5.5, Apache's Java Commons Logging (JCL) technology is used throughout Tomcat. JCL is a lightweight API for Java applications that allows hierarchical logging to be supported across all log levels, independent of logging implementation.
Apache Tomcat Logs Location in Windows By default, Apache Tomcat logs are stored in the install_dir/logs where the install_dir refers to the Apache Tomcat installation directory. The log files are stored in the logs directory.
Problem solved.. Issue was with the appender i was using.i was of impression that log4j handle closing of appender by itself but it was not doing this (i saw FileAppender code as it was trying to close appender). so i closed the appender myself and it solved the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With