i have a problem of rotation of log filename when using java.util.logging.logger in tomcat.
private Logger LOGGER = Logger.getLogger(MyClass.class.getName());
private FileHandler fh = null;
Log log = new Log();
private void writeLog(String terminalId, String date_time, String msg){
log.isExists();
fh = new FileHandler(log.fullPath(), true);
fh.setLevel(Level.INFO);
fh.setFormatter(new MyLogFormatter());
LOGGER.addHandler(fh);
LOGGER.setUseParentHandlers(false);
LOGGER.setLevel(Level.INFO);
LOGGER.info("Terminal: " + terminalId);
LOGGER.info("Time: " + date_time);
LOGGER.info("message: " + msg);
}
public test() {
writeLog(mapXML.get("terminalId"), mapXML.get("date_time"), "successful");
}
In the above code, the log.fullPath() is like "/usr/desktop/2015-05-13.log"
After i execute test() multiple times, multiple files such as
2015-05-13.log, 2015-05-13.log.1, 2015-05-13.log.2, 2015-05-13.log.3
are generated.
In the 2015-05-13.log, it contains the total messages, and the file with number only contains the message of that execution.
I know if restarting tomcat after each execution of test(), only 2015-05-13.log is created.
But i can not restart tomcat after each execution. So how to let it generate only one log file?
This is coming late but am still posting for those who might find this relevant.
The problem is because you didn't close FileHandler fh after writing. The first log file created remains locked and not available, as a result, a new file is then created and appended a number to avoid conflict.
Simply add fh.close(); after LOGGER.info("message: " + msg); to close the handler.
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