private int logValue(Level l) { if (l.equals(Level.FINEST)) { return 0; } else if (l.equals(Level.FINE)) { return 1; } else if (l.equals(Level.INFO)) { return 2; } else if (l.equals(Level.WARNING)) { return 3; } else if (l.equals(Level.SEVERE)) { return 4; } return -1; }
private void initLogger() { ConsoleHandler consoleHandler = null; Logger rootLogger = LogManager.getLogManager().getLogger(""); Handler[] handlers = rootLogger.getHandlers(); for (Handler handler : handlers) { if (handler instanceof ConsoleHandler) { consoleHandler = (ConsoleHandler) handler; rootLogger.removeHandler(handler); } } logger = Logger.getLogger(contextId); logger.setLevel(logLevel); if (!logLevel.equals(Level.OFF)) { LogFormatter formatter = new LogFormatter(); if (consoleLog) { if (consoleHandler == null) { consoleHandler = new ConsoleHandler(); } consoleHandler.setFormatter(formatter); consoleHandler.setLevel(logLevel); logger.addHandler(consoleHandler); } String userHomePath = getProperty("user.home", "."); File logDir = new File(userHomePath, '.' + contextId + "/log"); logDir.mkdirs(); String logFilePattern = new File(logDir, contextId + "-%g.log").getPath(); try { FileHandler fileHandler = new FileHandler(logFilePattern); fileHandler.setFormatter(formatter); fileHandler.setLevel(logLevel); logger.addHandler(fileHandler); } catch (IOException e) { System.err.println("Error: Failed to create log file: " + logFilePattern); } } }