`
haiyupeter
  • 浏览: 418607 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java 日志管理

    博客分类:
  • Java
阅读更多
Logger.java

//Level.java  Log级别定义
引用
OFF      一个特殊的级别,它可以关闭日记记录
SEVERE   severe级别的的信息应该描述非常重要的可能阻止程序正常执行的事件,应该清楚的描述给最终用户或者系统管理员 [si'viə] 严重的,剧烈的 1000。
WARNING  warning级别的信息应该描述最终用户或者系统管理员感兴趣的信息,或者是指明潜在的错误 900。
INFO     info级别的信息用于记录常规信息,描述最终用户或者系统管理员感兴趣的信息 800。


日志输出到后台控制台
    再来看看LogManager.java类;
    可以看到它有一个static静态代码块,里面寻找java.util.logging.manager配置,如果有的话,则将该类作为LogManager并实例化它,否则,新建一个默认的java.util.logging.LogMangger实例;
    在getLogManager()这个方法中,负责去装载日志配置文件logging.properties中的配置,readConfiguration()。首先是从System.getProperty("java.util.logging.config.class");寻找Config的Class类,方便第三方日志框架进行重写。如果没有则通过System.getProperty("java.util.logging.config.file")寻找。还没有的话,那就使用默认的jre(java.home)目录下的lib目录下的logging.properties文件,里面会进行Logger.setLever(),日志级别可以在日志文件里面定义
   
    
    /** 
     * Reinitialize the logging properties and reread the logging configuration 
     * from the given stream, which should be in java.util.Properties format. 
     * A PropertyChangeEvent will be fired after the properties are read. 
     * <p> 
     * Any log level definitions in the new configuration file will be  
     * applied using Logger.setLevel(), if the target Logger exists. 
     *  
     * @param ins   stream to read properties from 
     * @exception  SecurityException  if a security manager exists and if 
     *             the caller does not have LoggingPermission("control"). 
     * @exception  IOException if there are problems reading from the stream. 
     */  
    public void readConfiguration(InputStream ins) throws IOException, SecurityException {  
        checkAccess();  
        reset();  
  
        // Load the properties  
        props.load(ins);  
        // Instantiate new configuration objects.  
        String names[] = parseClassNames("config");  
  
        for (int i = 0; i < names.length; i++) {  
            String word = names[i];  
            try {  
                Class clz = ClassLoader.getSystemClassLoader().loadClass(word);  
                clz.newInstance();  
            } catch (Exception ex) {  
                System.err.println("Can't load config class \"" + word + "\"");  
                System.err.println("" + ex);  
                // ex.printStackTrace();  
            }  
        }  
  
        // Set levels on any pre-existing loggers, based on the new properties.  
        setLevelsOnExistingLoggers();  
  
        // Notify any interested parties that our properties have changed.  
        changes.firePropertyChange(null, null, null);  
  
        // Note that we need to reinitialize global handles when   
        // they are first referenced.  
        synchronized (this) {  
            initializedGlobalHandlers = false;  
        }  
    }  

参考:http://dollyn.iteye.com/blog/539922
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics