I’m logging in a JSF and ejb enviroment and every time I redeploy my app, the .lck files don’t get deleted and a new log (and lock) file is created (logfilename.log.2, logfilename.log.3 …)
This is how I get the log and add a filehandler in a JSF managed bean:
static {
// (...)
logger = Logger.getLogger("registrations");
FileHandler fh;
try {
// (...)
fh = new FileHandler(registerLogPath, true);
fh.setFormatter(new Formatter() {
@Override
public String format(LogRecord record) {
return ...
}
});
logger.addHandler(fh);
} catch (IOException | SecurityException ex) {
logger.log(Level.SEVERE, null, ex);
}
}
I tried to remove the handlers in the contextDestroyed method of ServletContextListener, but it seems that it is too late since Logger.getLogger("registrations").getHandlers() returns an empty array, so nothing to remove.
Can you help me how to make the logger use the file it used before redeploying?
I finally managed to solve this. Removing a handler is not enough, you have to call
close()on the handler itself. Also,logger.getHandlers()works in acontextDestroyedmethod: