I am using the following Class 'LoggerUtil' for logging to console and to a file 'logfile.log'. The logging to console is working fine. However, logging to 'logfile.log' file stops after few logs. Any suggestions in locating the bug would be welcome. I am attaching the code below:
public class LoggerUtil {
public static final String LOGGERNAME = "project.logging";
static {
try {
Logger.getLogger(LOGGERNAME).setUseParentHandlers(false);
Handler ch = new ConsoleHandler();
Handler fh = new FileHandler("logfile.log");
SimpleFormatter sf = new SimpleFormatter();
fh.setFormatter(sf);
Logger.getLogger(LOGGERNAME).addHandler(ch);
Logger.getLogger(LOGGERNAME).addHandler(fh);
setHandlersLevel(Level.ALL);
} catch (IOException | SecurityException ex) {
Logger.getLogger(LoggerUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void setHandlersLevel(Level level) {
Handler[] handlers = Logger.getLogger(LOGGERNAME).getHandlers();
for (Handler h : handlers) {
h.setLevel(level);
}
Logger.getLogger(LOGGERNAME).setLevel(level);
}
public static Logger getLogger() {
return Logger.getLogger(LOGGERNAME);
}
}
I call the following code from various places in my project to log:
LoggerUtil.getLogger().log(Level.INFO, "Message to be logged");
Kindly note, my project is multithreaded. Various threads use the same file for logging. Could this be a concurrency issue or is that just a red herring!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…