Example usage for java.util.logging Logger setLevel

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

Introduction

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

Prototype

public void setLevel(Level newLevel) throws SecurityException 

Source Link

Document

Set the log level specifying which message levels will be logged by this logger.

Usage

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

/**
 *
 */// ww  w  .j a  va 2  s .c  o  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.
 *///ww  w  .  j  a v a 2 s. c  o  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);
}

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//  w  w  w . j  ava2  s. co  m
        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.eclipse.ecr.common.logging.JavaUtilLoggingHelper.java

/**
 * Redirects {@code java.util.logging} to Apache Commons Logging do not log
 * below the threshold level.//from ww w .  ja v a 2s .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.nuxeo.common.logging.JavaUtilLoggingHelper.java

/**
 * Redirects {@code java.util.logging} to Apache Commons Logging do not log
 * below the threshold level.//from   w ww  .j  a  v 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 (SecurityException e) {
        log.error("Handler setup failed", e);
    }
}

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

/**
 * Activates this feature./*from  w ww.  j a v a  2s.co  m*/
 */
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:ffx.Main.java

/**
 * Replace the default console handler with our custom FFX handler.
 *///from  w  w w .  j av a  2s . c  o  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:dk.hippogrif.prettyxml.PrettyPrint.java

static String loadConfiguration(String resource) {
    try {//from  www  . j av  a2s.  com
        Properties prop = loadPropertiesResource(resource);

        // logging
        String loggingLevel = prop.getProperty("logging.Level");
        if (loggingLevel != null) {
            Level level = Level.parse(loggingLevel);
            Logger l = Logger.getLogger("dk.hippogrif.prettyxml");
            l.setLevel(level);
            if ("ConsoleHandler".equals(prop.getProperty("logging.Handler"))) {
                ConsoleHandler h = new ConsoleHandler();
                h.setLevel(level);
                l.addHandler(h);
            } else if ("FileHandler".equals(prop.getProperty("logging.Handler"))) {
                FileHandler h = new FileHandler(System.getProperty("user.home") + "/prettyxml.log");
                h.setLevel(level);
                l.addHandler(h);
            }
            logger.config("logging.Level=" + loggingLevel);
        }

        // version
        version = prop.getProperty("version", "");
        logger.config("version=" + version);

        // wellknown encodings
        String s = prop.getProperty("encodings");
        if (s == null) {
            throw new Exception("encodings missing in prettyxml.properties");
        }
        encodings = s.split(";");

        // wellknown property settings
        s = prop.getProperty("settings");
        if (s == null) {
            throw new Exception("settings missing in prettyxml.properties");
        }
        settings = s.split(";");
        setting = new HashMap();
        for (int i = 0; i < settings.length; i++) {
            String name = settings[i];
            Properties props = loadPropertiesResource(name + ".properties");
            checkProperties(props, false);
            setting.put(name, props);
        }

        // wellknown transformations
        s = prop.getProperty("transformations");
        if (s == null) {
            throw new Exception("transformations missing in prettyxml.properties");
        }
        transformations = s.split(";");
        transformation = new HashMap();
        for (int i = 0; i < transformations.length; i++) {
            String name = transformations[i];
            transformation.put(name, mkTransformerResource(name + ".xslt"));
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return version;
}

From source file:org.noroomattheinn.utils.Utils.java

public static void setupLogger(File where, String basename, Logger logger, Level level) {
    rotateLogs(where, basename, 3);//from www .j a v a 2s.  co  m

    FileHandler fileHandler;
    try {
        logger.setLevel(level);
        fileHandler = new FileHandler((new File(where, basename + "-00.log")).getAbsolutePath());
        fileHandler.setFormatter(new SimpleFormatter());
        fileHandler.setLevel(level);
        logger.addHandler(fileHandler);

        for (Handler handler : Logger.getLogger("").getHandlers()) {
            if (handler instanceof ConsoleHandler) {
                handler.setLevel(level);
            }
        }
    } catch (IOException | SecurityException ex) {
        logger.severe("Unable to establish log file: " + ex);
    }
}

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 w  ww .ja v  a 2s.  co 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);
}