I am fighting with some thing from yesterday.
I want to log a webapp (WAR) in java.
I create a log, append a FileHandler, and i send log messages. But, like all of us know, the file handler keep the lock over the new file until i call fush() and close(), so, when i redeploy the war (or if i run it in develop, and i continously deploy the app) it create one file per deploy (ugly).
So, I was trying to avoid this behavior, (I am not sure if it is done by a thing of performance this way, maybe open and close file everytime is expensive, i am not sure) but i want to atach the handler and flush/close each time the Logger log a message. (So i have only one file).
But i dont find any way to do this. I try to extends a java.util.Logger class and is so hard... (and -advised against- by some people).
Some one has any clue on how can i aproach this situation?
Thanks in advance!
public class LogUtil {
public static String name = "app%g.log";
public static Logger obtenerLogger(String eid, String path, Level levelToLog) {
Logger logger = Logger.getLogger(eid);
if (levelToLog != null) {
logger.setLevel(levelToLog);
}
//I create the folder if not exist
File dirFile = new File(path);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
//I remove the handlers and close after each logg call (doing this i can use the same file lof after log)
for (Handler handler : logger.getHandlers()) {
logger.removeHandler(handler);
handler.close();
handler.flush();
}
//I Create the FileHandler
FileHandler f;
try {
f = new FileHandler(
path + name, //pattern
10485760, //limit
3, // count
true); //append
//f.setFormatter(new SimpleFormatter());
f.setFormatter(new MyFormatter());
f.setLevel(levelToLog);
} catch (Exception ex) {
return null;
}
logger.addHandler(new ConsoleHandler());
logger.addHandler(f);
return logger;
}
PD1: But at redeploy of the war, i call the logger class, it create a new logger, but the last one keep unclose. And that is what is bothering me (i think)