public static void disableLogging() { LogManager.getLogManager().reset(); setGlobalLogLevel(Level.OFF); while (Logger.getLogger("").getHandlers().length > 0) { Logger.getLogger("").removeHandler(Logger.getLogger("").getHandlers()[0]); } } }
public synchronized static void init() { if (loggingInitialized.get()) { return; } // Turn off unwanted loggers (evil libraries and such) disableUnwantedLoggers(); // Load logging.properties try { // Use file if exists, else use file embedded in JAR InputStream logConfigInputStream = Config.class.getResourceAsStream(LOG_PROPERTIES_JAR_RESOURCE); if (logConfigInputStream != null) { LogManager.getLogManager().readConfiguration(logConfigInputStream); } loggingInitialized.set(true); } catch (Exception e) { Logger.getAnonymousLogger().severe("Could not load logging.properties file from resource "+LOG_PROPERTIES_JAR_RESOURCE); Logger.getAnonymousLogger().severe(e.getMessage()); e.printStackTrace(); } }
private void initLogHandlers(OptionSet options, OptionSpec<String> optionLog, OptionSpec<Void> optionLogPrint, OptionSpec<Void> optionDebug) throws SecurityException, IOException { // --log=<file> String logFilePattern = null; if (options.has(optionLog)) { if (!"-".equals(options.valueOf(optionLog))) { logFilePattern = options.valueOf(optionLog); } } else if (config != null && config.getLogDir().exists()) { logFilePattern = config.getLogDir() + File.separator + LOG_FILE_PATTERN; } else { logFilePattern = UserConfig.getUserLogDir() + File.separator + LOG_FILE_PATTERN; } if (logFilePattern != null) { Handler fileLogHandler = new FileHandler(logFilePattern, LOG_FILE_LIMIT, LOG_FILE_COUNT, true); fileLogHandler.setFormatter(new LogFormatter()); Logging.addGlobalHandler(fileLogHandler); } // --debug, add console handler if (options.has(optionDebug) || options.has(optionLogPrint) || (options.has(optionLog) && "-".equals(options.valueOf(optionLog)))) { Handler consoleLogHandler = new ConsoleHandler(); consoleLogHandler.setFormatter(new LogFormatter()); Logging.addGlobalHandler(consoleLogHandler); } }
private void initLogLevel(OptionSet options, OptionSpec<Void> optionDebug, OptionSpec<String> optionLogLevel) { Level newLogLevel = null; // --debug if (options.has(optionDebug)) { newLogLevel = Level.ALL; } // --loglevel=<level> else if (options.has(optionLogLevel)) { String newLogLevelStr = options.valueOf(optionLogLevel); try { newLogLevel = Level.parse(newLogLevelStr); } catch (IllegalArgumentException e) { showErrorAndExit("Invalid log level given " + newLogLevelStr + "'"); } } else { newLogLevel = Level.INFO; } // Add handler to existing loggers, and future ones Logging.setGlobalLogLevel(newLogLevel); // Debug output if (options.has(optionDebug)) { out.println("debug"); out.println(String.format("Application version: %s", Client.getApplicationVersionFull())); logger.log(Level.INFO, "Application version: {0}", Client.getApplicationVersionFull()); } }
public static void setGlobalLogLevel(Level targetLogLevel) { for (Enumeration<String> loggerNames = LogManager.getLogManager().getLoggerNames(); loggerNames.hasMoreElements(); ) { String loggerName = loggerNames.nextElement(); Logger logger = LogManager.getLogManager().getLogger(loggerName); if (logger != null) { logger.setLevel(targetLogLevel); } } for (Handler handler : Logger.getLogger("").getHandlers()) { handler.setLevel(targetLogLevel); } Logger.getLogger("").setLevel(targetLogLevel); // Make sure the unwanted loggers stay quiet disableUnwantedLoggers(); }