Example usage for java.util.logging LogManager getProperty

List of usage examples for java.util.logging LogManager getProperty

Introduction

In this page you can find the example usage for java.util.logging LogManager getProperty.

Prototype

public String getProperty(String name) 

Source Link

Document

Get the value of a logging property.

Usage

From source file:com.archivas.logging.FileHandler.java

private static String getPattern() {
    LogManager manager = LogManager.getLogManager();

    String cname = FileHandler.class.getName();

    String pattern = manager.getProperty(cname + ".pattern");
    if (pattern == null) {
        pattern = "%h/java%u.log";
    }/*from   ww w  .  j a va  2  s .c o  m*/

    return pattern;
}

From source file:com.archivas.logging.FileHandler.java

private static int getLimit() {
    LogManager manager = LogManager.getLogManager();
    String cname = FileHandler.class.getName();

    String limitStr = manager.getProperty(cname + ".limit");
    int limit = 10000000;
    if (limitStr != null) {
        limit = Integer.valueOf(limitStr);
    }//  w w w  .jav a  2 s  . c  o m

    return limit;
}

From source file:com.archivas.logging.FileHandler.java

private static int getCount() {
    LogManager manager = LogManager.getLogManager();
    String cname = FileHandler.class.getName();

    String countStr = manager.getProperty(cname + ".count");
    int count = 9;
    if (countStr != null) {
        count = Integer.valueOf(countStr);
    }/*  w w w  . j a  va2  s  .  co m*/

    return count;
}

From source file:com.archivas.logging.FileHandler.java

private static boolean getAppend() {
    LogManager manager = LogManager.getLogManager();
    String cname = FileHandler.class.getName();

    String appendStr = manager.getProperty(cname + ".append");
    boolean append = true;
    if (appendStr != null) {
        append = Boolean.valueOf(appendStr);
    }//from   w  w w .  j a v a  2  s.  c  o m

    return append;
}

From source file:com.izforge.izpack.util.LogUtils.java

public static void loadConfiguration(final Properties configuration) throws IOException {
    if (OVERRIDE) {
        LogManager manager = LogManager.getLogManager();

        // Merge global logging properties
        InputStream baseResourceStream = null;
        try {/*w ww  .  j a  v a2s.  c  o m*/
            baseResourceStream = LogUtils.class.getResourceAsStream(LOGGING_BASE_CONFIGURATION);
            final Properties baseProps = new Properties();
            baseProps.load(baseResourceStream);
            mergeLoggingConfiguration(configuration, baseProps);
        } finally {
            IOUtils.closeQuietly(baseResourceStream);
        }

        boolean mkdirs = false;
        String pattern = null;
        if (configuration.getProperty("handlers") != null
                && configuration.getProperty("handlers").contains(FILEHANDLER_CLASSNAME)
                && manager.getProperty("handlers").contains(FILEHANDLER_CLASSNAME)) {
            // IzPack maintains just one log file, don't override the existing handler type of it.
            // Special use case: Command line argument -logfile "wins" over the <log-file> tag.
            // Assumption at the moment for optimization: Just FileHandler is used for configurations from install.xml.
            return;
        }
        for (String key : configuration.stringPropertyNames()) {
            if (key.equals(FILEHANDLER_CLASSNAME + ".pattern")) {
                // Workaround for not normalized file paths, for example ${INSTALL_PATH}/../install_log/name.log
                // to get them working before creating ${INSTALL_PATH} in the
                // com.izforge.izpack.installer.unpacker.UnpackerBase.preUnpack phase
                // otherwise the FileHandler will fail when opening files already in constructor and not recover from that.
                pattern = FilenameUtils.normalize(configuration.getProperty(key));
                configuration.setProperty(key, pattern);
            } else if (key.equals(FILEHANDLER_CLASSNAME + ".mkdirs")) {
                // This key goes beyond the capabilities of java.util.logging.FileHandler
                mkdirs = Boolean.parseBoolean(configuration.getProperty(key));
                configuration.remove(key);
            }
        }
        if (mkdirs && pattern != null) {
            FileUtils.forceMkdirParent(new File(pattern));
        }

        // Merge user settings compiled in
        final Properties userProps = new Properties();
        InputStream userPropsStream = LogUtils.class
                .getResourceAsStream(ResourceManager.getInstallLoggingConfigurationResourceName());
        try {
            if (userPropsStream != null) {
                userProps.load(userPropsStream);
                for (String userPropName : userProps.stringPropertyNames()) {
                    if (userPropName.endsWith(".level") && !userPropName.startsWith(FILEHANDLER_CLASSNAME)) {
                        String level = userProps.getProperty(userPropName);
                        if (level != null) {
                            configuration.setProperty(userPropName, level);
                        }
                    }
                }
            }
        } finally {
            IOUtils.closeQuietly(userPropsStream);
        }

        InputStream defaultResourceStream = null;
        try {
            defaultResourceStream = LogUtils.class.getResourceAsStream(LOGGING_CONFIGURATION);
            final Properties defaultProps = new Properties();
            defaultProps.load(defaultResourceStream);
            mergeLoggingConfiguration(configuration, defaultProps);
        } finally {
            IOUtils.closeQuietly(defaultResourceStream);
        }

        if (Debug.isDEBUG()) {
            configuration.setProperty(FILEHANDLER_CLASSNAME + ".level", Level.FINE.toString());
            configuration.setProperty(ConsoleHandler.class.getName() + ".level", Level.FINE.toString());
        }

        // Set general log level which acts as filter in front of all handlers
        String fileLevelName = configuration.getProperty(FILEHANDLER_CLASSNAME + ".level",
                Level.ALL.toString());
        Level fileLevel = Level.ALL;
        if (fileLevelName != null) {
            fileLevel = Level.parse(fileLevelName);
        }

        String consoleLevelName = configuration.getProperty(CONSOLEHANDLER_CLASSNAME + ".level",
                Level.INFO.toString());
        Level consoleLevel = Level.INFO;
        if (consoleLevelName != null) {
            consoleLevel = Level.parse(consoleLevelName);
        }

        configuration.setProperty(".level",
                (fileLevel.intValue() < consoleLevel.intValue()) ? fileLevelName : consoleLevelName);

        final PipedOutputStream out = new PipedOutputStream();
        final PipedInputStream in = new PipedInputStream(out);
        try {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        configuration.store(out, null);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        IOUtils.closeQuietly(out);
                    }
                }
            }).start();

            manager.readConfiguration(in);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
}

From source file:LogWindow.java

private WindowHandler() {
    LogManager manager = LogManager.getLogManager();
    String className = this.getClass().getName();
    String level = manager.getProperty(className + ".level");
    setLevel(level != null ? Level.parse(level) : Level.INFO);
    if (window == null)
        window = new LogWindow();
}

From source file:MailHandlerDemo.java

/**
 * Used debug problems with the logging.properties. The system property
 * java.security.debug=access,stack can be used to trace access to the
 * LogManager reset.//  ww  w  .  j  ava2 s . c o m
 *
 * @param prefix a string to prefix the output.
 * @param err any PrintStream or null for System.out.
 */
@SuppressWarnings("UseOfSystemOutOrSystemErr")
private static void checkConfig(String prefix, PrintStream err) {
    if (prefix == null || prefix.trim().length() == 0) {
        prefix = "DEBUG";
    }

    if (err == null) {
        err = System.out;
    }

    try {
        err.println(prefix + ": java.version=" + System.getProperty("java.version"));
        err.println(prefix + ": LOGGER=" + LOGGER.getLevel());
        err.println(prefix + ": JVM id " + ManagementFactory.getRuntimeMXBean().getName());
        err.println(prefix + ": java.security.debug=" + System.getProperty("java.security.debug"));
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            err.println(prefix + ": SecurityManager.class=" + sm.getClass().getName());
            err.println(prefix + ": SecurityManager classLoader=" + toString(sm.getClass().getClassLoader()));
            err.println(prefix + ": SecurityManager.toString=" + sm);
        } else {
            err.println(prefix + ": SecurityManager.class=null");
            err.println(prefix + ": SecurityManager.toString=null");
            err.println(prefix + ": SecurityManager classLoader=null");
        }

        String policy = System.getProperty("java.security.policy");
        if (policy != null) {
            File f = new File(policy);
            err.println(prefix + ": AbsolutePath=" + f.getAbsolutePath());
            err.println(prefix + ": CanonicalPath=" + f.getCanonicalPath());
            err.println(prefix + ": length=" + f.length());
            err.println(prefix + ": canRead=" + f.canRead());
            err.println(prefix + ": lastModified=" + new java.util.Date(f.lastModified()));
        }

        LogManager manager = LogManager.getLogManager();
        String key = "java.util.logging.config.file";
        String cfg = System.getProperty(key);
        if (cfg != null) {
            err.println(prefix + ": " + cfg);
            File f = new File(cfg);
            err.println(prefix + ": AbsolutePath=" + f.getAbsolutePath());
            err.println(prefix + ": CanonicalPath=" + f.getCanonicalPath());
            err.println(prefix + ": length=" + f.length());
            err.println(prefix + ": canRead=" + f.canRead());
            err.println(prefix + ": lastModified=" + new java.util.Date(f.lastModified()));
        } else {
            err.println(prefix + ": " + key + " is not set as a system property.");
        }
        err.println(prefix + ": LogManager.class=" + manager.getClass().getName());
        err.println(prefix + ": LogManager classLoader=" + toString(manager.getClass().getClassLoader()));
        err.println(prefix + ": LogManager.toString=" + manager);
        err.println(prefix + ": MailHandler classLoader=" + toString(MailHandler.class.getClassLoader()));
        err.println(
                prefix + ": Context ClassLoader=" + toString(Thread.currentThread().getContextClassLoader()));
        err.println(prefix + ": Session ClassLoader=" + toString(Session.class.getClassLoader()));
        err.println(prefix + ": DataHandler ClassLoader=" + toString(DataHandler.class.getClassLoader()));

        final String p = MailHandler.class.getName();
        key = p.concat(".mail.to");
        String to = manager.getProperty(key);
        err.println(prefix + ": TO=" + to);
        if (to != null) {
            err.println(prefix + ": TO=" + Arrays.toString(InternetAddress.parse(to, true)));
        }

        key = p.concat(".mail.from");
        String from = manager.getProperty(key);
        if (from == null || from.length() == 0) {
            Session session = Session.getInstance(new Properties());
            InternetAddress local = InternetAddress.getLocalAddress(session);
            err.println(prefix + ": FROM=" + local);
        } else {
            err.println(prefix + ": FROM=" + Arrays.asList(InternetAddress.parse(from, false)));
            err.println(prefix + ": FROM=" + Arrays.asList(InternetAddress.parse(from, true)));
        }

        synchronized (manager) {
            final Enumeration<String> e = manager.getLoggerNames();
            while (e.hasMoreElements()) {
                final Logger l = manager.getLogger(e.nextElement());
                if (l != null) {
                    final Handler[] handlers = l.getHandlers();
                    if (handlers.length > 0) {
                        err.println(prefix + ": " + l.getClass().getName() + ", " + l.getName());
                        for (Handler h : handlers) {
                            err.println(prefix + ":\t" + toString(prefix, err, h));
                        }
                    }
                }
            }
        }
    } catch (Throwable error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }
    err.flush();
}

From source file:org.apache.jena.atlas.logging.java.ConsoleHandlerStream.java

private static String getProperty(LogManager manager, String cname, String pname) {
    return manager.getProperty(cname + "." + pname);
}

From source file:org.dstadler.commons.logging.jdk.PatternFormatter.java

public PatternFormatter() {
    LogManager manager = LogManager.getLogManager();
    String cName = getClass().getName();

    timeFormat = manager.getProperty(cName + ".timeFormat");

    if (timeFormat == null) {
        timeFormat = "dd-MMM-yyy; HH:mm:ss";
    }//from   w  w w  .ja  v a2s  .c o m
    setTimeFormat(timeFormat);

    logPattern = manager.getProperty(cName + ".logPattern");
    if (logPattern == null) {
        logPattern = "[{0} - {1}] {2}: {3} \n";
    }
    setLogPattern(logPattern);

    exceptionPattern = manager.getProperty(cName + ".exceptionPattern");
    if (exceptionPattern == null) {
        exceptionPattern = "[{0} - {1}] {2} {3} \nException in {4}: {6} \n{7} ";
    }
    setExceptionPattern(exceptionPattern);

    logMessageFormat = new MessageFormat(logPattern, Locale.ROOT);
    exceptionMessageFormat = new MessageFormat(exceptionPattern, Locale.ROOT);

    dateFormat = FastDateFormat.getInstance(timeFormat);
}

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

public static void initLogging(BuildInfo binfo, Class init) {
    try {/*from   w  ww .  j a v  a2 s . 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);
    }
}