Example usage for java.util.logging Logger removeHandler

List of usage examples for java.util.logging Logger removeHandler

Introduction

In this page you can find the example usage for java.util.logging Logger removeHandler.

Prototype

public void removeHandler(Handler handler) throws SecurityException 

Source Link

Document

Remove a log Handler.

Usage

From source file:org.eclipse.ecr.common.logging.JavaUtilLoggingHelper.java

/**
 * Redirects {@code java.util.logging} to Apache Commons Logging do not log
 * below the threshold level.//from  w ww .  ja va2  s  .  c om
 *
 * @since 5.4.2
 */
public static synchronized void redirectToApacheCommons(Level threshold) {
    if (activeHandler != null) {
        return;
    }
    try {
        Logger rootLogger = LogManager.getLogManager().getLogger("");
        for (Handler handler : rootLogger.getHandlers()) {
            rootLogger.removeHandler(handler);
        }
        activeHandler = new LogHandler();
        activeHandler.setLevel(threshold);
        rootLogger.addHandler(activeHandler);
        rootLogger.setLevel(threshold);
        log.info("Redirecting java.util.logging to Apache Commons Logging, threshold is "
                + threshold.toString());
    } catch (Exception e) {
        log.error("Handler setup failed", e);
    }
}

From source file:org.freaknet.gtrends.client.GoogleTrendsClientFactory.java

private static void setLogLevel(CmdLineParser cmdLine) throws SecurityException, IllegalArgumentException {
    final Level level;
    if (cmdLine.getLogLevel() != null) {
        level = Level.parse(cmdLine.getLogLevel());
    } else {//from w w  w.ja  v  a2  s  .c o  m
        level = Level.parse(DEFAULT_LOGGING_LEVEL);
    }
    Logger log = LogManager.getLogManager().getLogger("");

    for (Handler h : log.getHandlers()) {
        log.removeHandler(h);
    }
    Handler handler = new ConsoleHandler();
    handler.setFormatter(new LogFormatter());
    handler.setLevel(level);
    log.setUseParentHandlers(false);

    Logger defaultLog = Logger.getLogger(GoogleConfigurator.getLoggerPrefix());
    defaultLog.addHandler(handler);
    defaultLog.setLevel(level);
    defaultLog.setFilter(new Filter() {
        @Override
        public boolean isLoggable(LogRecord record) {
            if (record.getSourceClassName().startsWith(GoogleConfigurator.getLoggerPrefix())) {
                return (record.getLevel().intValue() >= level.intValue());
            }
            return false;
        }
    });
}

From source file:org.nuxeo.common.logging.JavaUtilLoggingHelper.java

/**
 * Redirects {@code java.util.logging} to Apache Commons Logging do not log
 * below the threshold level./*from   ww w . j  a  v a  2s .c  o  m*/
 *
 * @since 5.4.2
 */
public static synchronized void redirectToApacheCommons(Level threshold) {
    if (activeHandler != null) {
        return;
    }
    try {
        Logger rootLogger = LogManager.getLogManager().getLogger("");
        for (Handler handler : rootLogger.getHandlers()) {
            rootLogger.removeHandler(handler);
        }
        activeHandler = new LogHandler();
        activeHandler.setLevel(threshold);
        rootLogger.addHandler(activeHandler);
        rootLogger.setLevel(threshold);
        log.info("Redirecting java.util.logging to Apache Commons Logging, threshold is "
                + threshold.toString());
    } catch (SecurityException e) {
        log.error("Handler setup failed", e);
    }
}

From source file:ductive.log.JDKToCommonsHandler.java

public static JDKToCommonsHandler rerouteJDKToCommons(Level level) {
    Logger root = Logger.getLogger("");

    {//from ww  w  .  jav a2s. com
        Handler[] handlers = root.getHandlers();
        if (!ArrayUtils.isEmpty(handlers))
            for (Handler h : handlers)
                root.removeHandler(h);
    }

    root.addHandler(DEFAULT);
    root.setLevel(level);

    return DEFAULT;
}

From source file:geva.Main.Run.java

protected static void initJavaLogging(java.util.logging.Level level) {
    // All of the library code uses the commons logging library for output.
    // If you want to change the log formats or do anything fancy it's strongly
    // recommended that you switch to the much more capable log4j library as
    // described at http://commons.apache.org/logging/guide.html

    // As a default we'll make the JDK logging behave like System.out.println 
    // so we dont introduce another dependency.

    java.util.logging.SimpleFormatter fmt = new java.util.logging.SimpleFormatter() {
        @Override/* ww w .j  a va  2 s  . c om*/
        public synchronized String format(LogRecord record) {
            return record.getMessage() + "\n";
        }
    };
    java.util.logging.Handler handler = new java.util.logging.StreamHandler(System.out, fmt);
    java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger("");
    for (java.util.logging.Handler h : rootLogger.getHandlers()) {
        rootLogger.removeHandler(h);
    }
    rootLogger.addHandler(handler);
    rootLogger.setLevel(level);

}

From source file:org.tros.utils.logging.Logging.java

public static void initLogging(BuildInfo binfo, Class init) {
    try {/*w w w  . jav  a  2s  .  c  o m*/
        //hack to get this logger to shut up
        Class<?> forName = Class.forName("org.reflections.Reflections");
        Field f = forName.getField("log");
        f.set(null, null);
    } catch (ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalArgumentException
            | IllegalAccessException ex) {
        Logger.getLogger(Logging.class.getName()).log(Level.FINEST,
                "org.reflections.Reflections not in CLASSPATH...");
    }

    //make logs directory
    getLogDirectory(binfo);

    //init logger
    String dir = getApplicationEtcDirectory(binfo) + "/logging.properties";
    File logProp = new File(dir);
    if (!logProp.exists()) {
        copyFile(binfo, init, logProp);
    }

    if (logProp.exists()) {
        loadFile(logProp);
    }

    LogManager lm = LogManager.getLogManager();
    String property = lm.getProperty("version");
    if (property == null || !property.equals(binfo.getVersion().replace("-SNAPSHOT", ""))) {
        //backup old file
        File backup = new File(logProp.getAbsolutePath() + "." + (property == null ? "old" : property));
        logProp.renameTo(backup);
        //copy new file
        copyFile(binfo, init, logProp);
        //re-load new file
        loadFile(logProp);
    }

    //Small hack to close SwingComponentHandler which should only be used by a GUI
    //however, if the logging.properties file is already set with this handler, remove
    //it and then the GUI will manually re-add it in the LogConsole constructor.
    Logger logger = Logger.getLogger("");
    try {
        Class<?> swingLogger = Class.forName("org.tros.utils.logging.SwingComponentHandler");
        for (Handler h : logger.getHandlers()) {
            if (swingLogger.isAssignableFrom(h.getClass())) {
                logger.removeHandler(h);
                h.close();
            }
        }
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Logging.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:brut.apktool.Main.java

private static void setupLogging(Verbosity verbosity) {
    Logger logger = Logger.getLogger("");
    for (Handler handler : logger.getHandlers()) {
        logger.removeHandler(handler);
    }/* w  ww.jav a  2  s. c  o  m*/
    LogManager.getLogManager().reset();

    if (verbosity == Verbosity.QUIET) {
        return;
    }

    Handler handler = new Handler() {
        @Override
        public void publish(LogRecord record) {
            if (getFormatter() == null) {
                setFormatter(new SimpleFormatter());
            }

            try {
                String message = getFormatter().format(record);
                if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
                    System.err.write(message.getBytes());
                } else {
                    System.out.write(message.getBytes());
                }
            } catch (Exception exception) {
                reportError(null, exception, ErrorManager.FORMAT_FAILURE);
            }
        }

        @Override
        public void close() throws SecurityException {
        }

        @Override
        public void flush() {
        }
    };

    logger.addHandler(handler);

    if (verbosity == Verbosity.VERBOSE) {
        handler.setLevel(Level.ALL);
        logger.setLevel(Level.ALL);
    } else {
        handler.setFormatter(new Formatter() {
            @Override
            public String format(LogRecord record) {
                return record.getLevel().toString().charAt(0) + ": " + record.getMessage()
                        + System.getProperty("line.separator");
            }
        });
    }
}

From source file:org.spoutcraft.launcher.entrypoint.SpoutcraftLauncher.java

protected static Logger setupLogger() {
    final Logger logger = Utils.getLogger();
    File logDirectory = new File(Utils.getLauncherDirectory(), "logs");
    if (!logDirectory.exists()) {
        logDirectory.mkdir();/*from ww w .j  a  v a  2 s .  c om*/
    }
    File logs = new File(logDirectory, "techniclauncher_%D.log");
    RotatingFileHandler fileHandler = new RotatingFileHandler(logs.getPath());

    fileHandler.setFormatter(new TechnicLogFormatter());

    for (Handler h : logger.getHandlers()) {
        logger.removeHandler(h);
    }
    logger.addHandler(fileHandler);

    SpoutcraftLauncher.handler = fileHandler;

    if (params != null && !params.isDebugMode()) {
        logger.setUseParentHandlers(false);

        System.setOut(new PrintStream(new LoggerOutputStream(console, Level.INFO, logger), true));
        System.setErr(new PrintStream(new LoggerOutputStream(console, Level.SEVERE, logger), true));
    }

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            logger.log(Level.SEVERE, "Unhandled Exception in " + t, e);

            if (errorDialog == null) {
                LauncherFrame frame = null;

                try {
                    frame = Launcher.getFrame();
                } catch (Exception ex) {
                    //This can happen if we have a very early crash- before Launcher initializes
                }

                errorDialog = new ErrorDialog(frame, e);
                errorDialog.setVisible(true);
            }
        }
    });

    return logger;
}

From source file:ffx.Main.java

/**
 * Replace the default console handler with our custom FFX handler.
 *///from   w  w  w .  j a  v a2  s . co m
private static void startLogging() {
    // Remove all log handlers from the default logger.
    try {
        Logger defaultLogger = LogManager.getLogManager().getLogger("");
        Handler defaultHandlers[] = defaultLogger.getHandlers();
        for (Handler h : defaultHandlers) {
            defaultLogger.removeHandler(h);
        }
    } catch (Exception e) {
        System.err.println(e.toString());
    }

    // Retrieve the log level from the ffx.log system property.
    String logLevel = System.getProperty("ffx.log", "info");
    Level tempLevel;
    try {
        tempLevel = Level.parse(logLevel.toUpperCase());
    } catch (Exception e) {
        tempLevel = Level.INFO;
    }

    level = tempLevel;
    logHandler = new LogHandler();
    logHandler.setLevel(level);
    Logger ffxLogger = Logger.getLogger("ffx");
    ffxLogger.addHandler(logHandler);
    ffxLogger.setLevel(level);
}

From source file:com.surfs.nas.log.LogFactory.java

/**
 *
 *
 * @param filename/*from  w w  w  .  j ava2  s . c o m*/
 * @param log
 */
public static void configJDKLog(String filename, Logger log) {
    try {
        File f = new File(filename);
        if (!f.exists()) {
            return;
        }
        java.util.logging.LogManager.getLogManager().readConfiguration(new FileInputStream(f));
        java.util.logging.Logger logger = java.util.logging.LogManager.getLogManager().getLogger("");
        java.util.logging.Handler[] handlers = logger.getHandlers();
        for (java.util.logging.Handler h : handlers) {
            logger.removeHandler(h);
        }
        JdkLogHandler handle = new JdkLogHandler(log);
        logger.addHandler(handle);

    } catch (Exception e) {
    }
}