util.logger的使用


1 . 日志的组成:配置文件,写日志

log.cfg

############################################################
#      Default Logging Configuration File
#
# You can use a different file by specifying a filename
# with the java.util.logging.config.file system property.  
# For example java 
- Djava.util.logging.config.file = myfile
############################################################

############################################################
#      Global properties
############################################################

" handlers "  specifies a comma separated list of log Handler 
# classes.  These handlers will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By 
default  we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
handlers
=  java.util.logging.ConsoleHandler

# To also add the FileHandler, use the following line instead.
#handlers
=  java.util.logging.FileHandler, java.util.logging.ConsoleHandler

# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers.  For any given facility 
this  global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
.level
=  INFO

############################################################
# Handler specific properties.
# Describes specific configuration info 
for  Handlers.
############################################################

default  file output is in user ' s home directory.
#java.util.logging.FileHandler.level  =  INFO
java.util.logging.FileHandler.pattern 
=  myLog % u.log
java.util.logging.FileHandler.limit 
=   50000
java.util.logging.FileHandler.count 
=   1
java.util.logging.FileHandler.formatter 
=  java.util.logging.SimpleFormatter
java.util.logging.FileHandler.append 
=   true
# Limit the message that are printed on the console to INFO and above.

# modify by dwq 
java.util.logging.ConsoleHandler.level 
=  INFO
java.util.logging.ConsoleHandler.formatter 
=  java.util.logging.SimpleFormatter


############################################################
# Facility specific properties.
# Provides extra control 
for  each logger.
############################################################

# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
com.xyz.root.level 
=  INFO


2.  日志的使用理想要求:可以灵活开关,可以写控制台,可以写文件,可以对某个包或类使用日志。
 
  1)灵活开关,一级,.level = ?? ,可通过注意这里是Logger的level, 还有handler的可以分别在后面设计。二者级别取与才是日志输出级别。

  2) 写文件时,要注意文件的路径。当不使用任何前缀时是当前路径。还不错了。写文件时可以使用多个文件,且文件大小不会过大。
  3)使用控制台时,注意是继承了root的控制台。
   
  4)对某个包或类使用的问题,就要求得到日志同时传入类名才能做到。
       但是对日志的打开是只能往上关,不能加入例外。如能加入例外就好了,就是说可以配置其他全关了,但是对打开某个包下的到某个级别。
   目前的配置是比如有10个包,要求打开一个,关闭其他。需要一个一个去设置其他的.level为OFF, 然后只打开需要的那个。
   
   * 对包的配置是具体的,可以关闭父包而打开子包。这算是一种例外了。哈哈,原来是没有发现。

 3. 默认的util.log不好弄,不好弄,或者得到麻烦,我封闭了一个简单点的。可以配合前面配置文件使用

  LogUtil.java
  1 package  com.dwq.util;
  2
  3 import  java.io.FileNotFoundException;
  4 import  java.io.IOException;
  5 import  java.util.Properties;
  6 import  java.util.logging.LogManager;
  7 import  java.util.logging.Logger;
  8
  9 /** */ /**
 10 * 
 11 * Class: com.dwq.util.LogUtil
 12 * @version1.0    作者:dongweiqiang-ghq * 日期:2010-9-9 功能初建
 13 * 功能:做好的使用java.util.logging.Logger类。
 14 * 
 15 * 日志使用注意点,默认级别使用全在配置文件中设置,而不要用方法操作
 16 * 
 17 */

 18 public   class  LogUtil
 19 {
 20    private final static String        CONFIG_FILE    = "log.cfg";
 21
 22    private static LogManager    lm;
 23
 24    private static boolean isConfiged = false;
 25    
 26    private LogUtil()
 27    {
 28        
 29    }

 30    /** *//**
 31     * 得到日志,如果不指定属性配置文件,默认使用log.cfg
 32     * 
 33     * 配置文件需要在首次调用getLogger方法前调用,否则使用默认的log.cfg
 34     * @return
 35     */

 36    
 37    /** *//**
 38     * 简单情况,不需要对包或类级使用不同级别时,就用全局 的
 39     */

 40    public static Logger getLogger()
 41    {
 42        if(!isConfiged)
 43            configLog(CONFIG_FILE);
 44        Logger logger = lm.getLogger(Logger.GLOBAL_LOGGER_NAME);
 45        return logger;
 46    }

 47    
 48    /** *//**
 49     * 传入类名构建日志,并加入管理器
 50     * @param clsName 
 51     * @return
 52     */

 53    public static Logger getLogger(String clsName)
 54    {
 55        if(!isConfiged)
 56            configLog(CONFIG_FILE);
 57        lm.addLogger(Logger.getLogger(clsName));
 58        Logger logger = lm.getLogger(clsName);
 59        return logger;
 60    }

 61    
 62    /** *//**
 63     * 建议使用得到Logger的方法
 64     * 传入类构造,这个通常输入更少些可通过this.getClass()或类名.class得到
 65     * @param cls
 66     * @return
 67     */

 68    public static Logger getLogger(Class<?> cls)
 69    {
 70        if(!isConfiged)
 71            configLog(CONFIG_FILE);
 72        lm.addLogger(Logger.getLogger(cls.getName()));
 73        Logger logger = lm.getLogger(cls.getName());
 74        return logger;
 75    }

 76    /** *//**
 77     * 设置配置util.log的属性文件
 78     * @param file 属性配置文件,输入为包括后缀, 默认使用log.cfg文件
 79     */

 80    public static void configLog(String file)
 81    {
 82        try
 83        {
 84            Properties p = System.getProperties();
 85            p.put("java.util.logging.config.file", file);
 86            lm = LogManager.getLogManager();
 87            lm.readConfiguration();
 88        }
 catch (FileNotFoundException e)
 89        {
 90            e.printStackTrace();
 91        }
 catch (SecurityException e)
 92        {
 93            e.printStackTrace();
 94        }
 catch (IOException e)
 95        {
 96            e.printStackTrace();
 97        }

 98        isConfiged = true;
 99    }

100}

101


你可能感兴趣的:(util.logger的使用)