Example usage for java.util.logging Logger addHandler

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

Introduction

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

Prototype

public void addHandler(Handler handler) throws SecurityException 

Source Link

Document

Add a log Handler to receive logging messages.

Usage

From source file:jshm.logging.Log.java

public static void reloadConfig() throws Exception {
    // all logging
    Handler consoleHandler = new ConsoleHandler();
    consoleHandler.setLevel(DEBUG ? Level.ALL : Level.WARNING);
    consoleHandler.setFormatter(new OneLineFormatter());

    Logger cur = Logger.getLogger("");
    removeHandlers(cur);//from ww w.ja  v  a 2s.  c  o m

    cur.addHandler(consoleHandler);

    // jshm logging
    Formatter fileFormatter = new FileFormatter();
    Handler jshmHandler = new FileHandler("data/logs/JSHManager.txt");
    jshmHandler.setLevel(Level.ALL);
    jshmHandler.setFormatter(fileFormatter);

    cur = Logger.getLogger("jshm");
    cur.addHandler(jshmHandler);
    cur.setLevel(DEBUG ? Level.ALL : Level.INFO);

    // hibernate logging
    Handler hibernateHandler = new FileHandler("data/logs/Hibernate.txt");
    hibernateHandler.setLevel(Level.ALL);
    hibernateHandler.setFormatter(fileFormatter);

    cur = Logger.getLogger("org.hibernate");
    removeHandlers(cur);

    cur.addHandler(hibernateHandler);
    cur.setLevel(DEBUG ? Level.INFO : Level.WARNING);

    // HttpClient logging
    Handler httpClientHandler = new FileHandler("data/logs/HttpClient.txt");
    httpClientHandler.setLevel(Level.ALL);
    httpClientHandler.setFormatter(fileFormatter);

    //      cur = Logger.getLogger("httpclient.wire");
    cur = Logger.getLogger("httpclient.wire.header");
    removeHandlers(cur);

    cur.addHandler(httpClientHandler);
    cur.setLevel(DEBUG ? Level.ALL : Level.INFO);

    cur = Logger.getLogger("org.apache.commons.httpclient");
    removeHandlers(cur);

    cur.addHandler(httpClientHandler);
    cur.setLevel(DEBUG ? Level.FINER : Level.INFO);

    // HtmlParser logging
    Handler htmlParserHandler = new FileHandler("data/logs/HtmlParser.txt");
    htmlParserHandler.setLevel(Level.ALL);
    htmlParserHandler.setFormatter(fileFormatter);

    cur = Logger.getLogger("org.htmlparser");
    removeHandlers(cur);

    cur.addHandler(htmlParserHandler);
    cur.setLevel(DEBUG ? Level.ALL : Level.INFO);

    // SwingX logging
    Handler swingxHandler = new FileHandler("data/logs/SwingX.txt");
    swingxHandler.setLevel(Level.ALL);
    swingxHandler.setFormatter(fileFormatter);

    cur = Logger.getLogger("org.jdesktop.swingx");
    removeHandlers(cur);

    cur.addHandler(swingxHandler);
    cur.setLevel(DEBUG ? Level.ALL : Level.INFO);
}

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  a  v  a 2  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:com.mobius.software.mqtt.performance.controller.ControllerRunner.java

private static void configureConsoleLogger() {
    Logger l = Logger.getLogger("org.glassfish.grizzly.http.server.HttpHandler");
    l.setLevel(Level.INFO);//from   w  ww  . ja  v  a2s  .  c  om
    l.setUseParentHandlers(false);
    ConsoleHandler ch = new ConsoleHandler();
    ch.setLevel(Level.INFO);
    l.addHandler(ch);
}

From source file:havocx42.Program.java

private static void initRootLogger() throws SecurityException, IOException {

    FileHandler fileHandler;/*from www  .j  a  v  a 2 s.co m*/
    fileHandler = new FileHandler("PRWeaponStats.%u.%g.txt", 1024 * 1024 * 8, 3, false);
    fileHandler.setLevel(Level.FINEST);
    fileHandler.setFormatter(new SimpleFormatter());
    Logger rootLogger = Logger.getLogger("");
    Handler[] handlers = rootLogger.getHandlers();
    for (Handler handler : handlers) {
        handler.setLevel(Level.INFO);
    }
    rootLogger.setLevel(Level.FINEST);
    rootLogger.addHandler(fileHandler);
}

From source file:velo.patterns.FactoryLogger.java

/**
 * Factory a logger by a log file name/*from www .j  av a 2  s  .co m*/
 * @param logName The log file name to factory a logger object for
 * @return A logger object for the specified log file name
 */
public static Logger loggerFactory(String logName) {
    Logger logger = Logger.getLogger(logName);
    //Create a new handler that uses the simple formatter
    try {
        FileHandler fh = new FileHandler(getLogDir() + logName + ".txt");
        fh.setFormatter(new SimpleFormatter());
        logger.addHandler(fh);
    } catch (IOException e) {
    }

    return logger;
}

From source file:ductive.log.JDKToCommonsHandler.java

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

    {//from   ww  w .  ja va2 s.  c  om
        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:nl.minvenj.pef.stream.LiveCapture.java

/**
 * Initializes a logger.//w w  w  .  j  ava  2  s  .c  o m
 *
 * @param logPath The location and name of the log file
 * @throws SecurityException on opening the log file
 * @throws IOException on opening the log file
 */
private static void initLogger(final String logPath)
        throws SecurityException, IOException, IllegalArgumentException {
    logFileHandler = new FileHandler(logPath, true);
    Logger initLogger = Logger.getLogger("");
    logFileHandler.setFormatter(new SimpleFormatter());
    initLogger.addHandler(logFileHandler);
    initLogger.setLevel(Level.CONFIG);
}

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 a  2s .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.jamwiki.utils.WikiLogger.java

/**
 *
 *///from  www. j ava 2  s  .  co  m
private static void initializeLogParams() {
    FileInputStream stream = null;
    try {
        File propertyFile = WikiLogger.loadProperties();
        stream = new FileInputStream(propertyFile);
        Properties properties = new Properties();
        properties.load(stream);
        String pattern = properties.getProperty("org.jamwiki.pattern");
        int limit = new Integer(properties.getProperty("org.jamwiki.limit")).intValue();
        int count = new Integer(properties.getProperty("org.jamwiki.count")).intValue();
        boolean append = Boolean.valueOf(properties.getProperty("org.jamwiki.append")).booleanValue();
        String datePattern = properties.getProperty("org.jamwiki.timestamp");
        DEFAULT_LOG_LEVEL = Level.parse(properties.getProperty("org.jamwiki.level"));
        WikiLogger.DEFAULT_LOG_HANDLER = new FileHandler(pattern, limit, count, append);
        DEFAULT_LOG_HANDLER.setFormatter(new WikiLogFormatter(datePattern));
        DEFAULT_LOG_HANDLER.setLevel(DEFAULT_LOG_LEVEL);
        // test the logger to verify permissions are OK
        Logger logger = Logger.getLogger(WikiLogger.class.getName());
        logger.addHandler(WikiLogger.DEFAULT_LOG_HANDLER);
        logger.setLevel(DEFAULT_LOG_LEVEL);
        logger.config("JAMWiki log initialized from " + propertyFile.getPath() + " with pattern " + pattern);
    } catch (Exception e) {
        System.out.println("WARNING: Unable to load custom JAMWiki logging configuration, using system default "
                + e.getMessage());
        WikiLogger.DEFAULT_LOG_HANDLER = null;
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ex) {
            }
        }
    }
}

From source file:org.jamwiki.utils.WikiLogger.java

/**
 * Retrieve a named <code>WikiLogger</code> object.
 *
 * @param name The name of the log object to retrieve or create.
 * @return A logger instance for the given name.
 *//*  w w w. j ava2s.co m*/
public static WikiLogger getLogger(String name) {
    Logger logger = Logger.getLogger(name);
    if (WikiLogger.DEFAULT_LOG_HANDLER != null) {
        logger.addHandler(WikiLogger.DEFAULT_LOG_HANDLER);
        logger.setLevel(DEFAULT_LOG_LEVEL);
    }
    return new WikiLogger(logger);
}