Example usage for java.util.logging Logger getHandlers

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

Introduction

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

Prototype

public Handler[] getHandlers() 

Source Link

Document

Get the Handlers associated with this logger.

Usage

From source file:jshm.logging.Log.java

public static Logger removeHandlers(final Logger logger) {
    for (Handler h : logger.getHandlers())
        logger.removeHandler(h);/*from   w w w  .  j av a2  s. c  o  m*/
    return logger;
}

From source file:ductive.log.JDKToCommonsHandler.java

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

    {//from   w ww. jav a  2 s .c  o  m
        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:asl.seedscan.DQAWeb.java

public static void findConsoleHandler() {
    // Locate the global logger's ConsoleHandler if it exists
    Logger globalLogger = Logger.getLogger("");
    for (Handler handler : globalLogger.getHandlers()) {
        if (handler instanceof ConsoleHandler) {
            consoleHandler = handler;//from   ww  w  .ja  va 2 s  . c  o  m
            break;
        }
    }

    // Ensure the global logger has an attached ConsoleHandler
    // creating one for it if necessary
    if (consoleHandler == null) {
        consoleHandler = new ConsoleHandler();
        globalLogger.addHandler(consoleHandler);
    }
}

From source file:org.openqa.selenium.server.log.LoggingManager.java

protected static void saveOriginalHandlersFormattersAndLevels(Logger logger) {
    originalHandlers = logger.getHandlers();
    originalFormatters = new HashMap<Handler, Formatter>();
    originalLogLevels = new HashMap<Handler, Level>();
    for (Handler handler : originalHandlers) {
        originalFormatters.put(handler, handler.getFormatter());
        originalLogLevels.put(handler, handler.getLevel());
    }/* w ww.  j av  a 2s .c o m*/
}

From source file:org.stanwood.nwn2.gui.logging.JavaLoggingToCommonLoggingRedirector.java

/**
 * Activates this feature.//  w w w .j a  va2s. c  om
 */
public static void activate() {
    try {
        Logger rootLogger = LogManager.getLogManager().getLogger("");
        // remove old handlers
        for (Handler handler : rootLogger.getHandlers()) {
            rootLogger.removeHandler(handler);
        }
        // add our own
        activeHandler = new JDKLogHandler();
        activeHandler.setLevel(Level.ALL);
        rootLogger.addHandler(activeHandler);
        rootLogger.setLevel(Level.ALL);
    } catch (Exception exc) {
        LogFactory.getLog(JavaLoggingToCommonLoggingRedirector.class).error("activation failed", exc);
    }
}

From source file:org.openqa.selenium.server.log.LoggingManager.java

protected static void restoreOriginalHandlersFormattersAndLevels(Logger logger) {
    for (Handler handler : logger.getHandlers()) {
        logger.removeHandler(handler);// w w w. j  a  v  a2 s  .c o m
    }
    for (Handler handler : originalHandlers) {
        logger.addHandler(handler);
        // jbevan: java.util.logging.RestishHandler.setFormatter(null) throws an NPE
        if (originalFormatters.get(handler) != null) {
            handler.setFormatter(originalFormatters.get(handler));
        }
        handler.setLevel(originalLogLevels.get(handler));
    }
}

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.//  ww  w  .jav a  2 s  . 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 (Exception e) {
        log.error("Handler setup failed", e);
    }
}

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   w w w .jav a2 s .  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: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 {//  w  w  w  .j  av a  2 s  .  c  om
        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.openqa.selenium.server.log.LoggingManager.java

public static void overrideSimpleFormatterWithTerseOneForConsoleHandler(Logger logger, boolean debugMode) {
    for (Handler handler : logger.getHandlers()) {
        if (handler instanceof ConsoleHandler) {
            final Formatter formatter;

            formatter = handler.getFormatter();
            if (formatter instanceof SimpleFormatter) {
                final StdOutHandler stdOutHandler;
                final Level originalLevel;

                /*//from  ww w  .  ja  v  a  2 s  .  co  m
                 * DGF - Nobody likes the SimpleFormatter; surely they wanted our terse formatter instead.
                 */
                originalLevel = handler.getLevel();
                handler.setFormatter(new TerseFormatter(false));
                handler.setLevel(Level.WARNING);

                /*
                 * Furthermore, we all want DEBUG/INFO on stdout and WARN/ERROR on stderr
                 */
                stdOutHandler = new StdOutHandler();
                stdOutHandler.setFormatter(new TerseFormatter(false));
                stdOutHandler.setFilter(new MaxLevelFilter(Level.INFO));
                stdOutHandler.setLevel(originalLevel);
                logger.addHandler(stdOutHandler);
                if (debugMode) {
                    if (originalLevel.intValue() > Level.FINE.intValue()) {
                        stdOutHandler.setLevel(Level.FINE);
                    }
                }
            }
        }
    }
}