开发者

Log4j FileAppender issue in Tomcat server

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.


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.


Need more information. When you say "restart my server", does that mean the whole JVM is restarted or just threads within the JVM? I had a problem similar to this before with a custom daemon. When I thought the server was restarted it did not actually kill the previous instances of the daemon and the JVM, so I had multiple instances running, updating, and conflicting with each other.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜