java.util.logging

时间:2023-03-09 22:31:30
java.util.logging

  我们目前记录日志用的最多的就是Apache的log4j,其实java.util本身也提供日志记录功能,即java.util.logging,值得关注的就是它的等级与log4j的等级有所不同:

  首先我们可以在D:\Program Files\Java\jdk1.7.0\jre\lib下找到logging.properties,这里面就是jdk自带的日志配置文件,Level 类定义了一组可用来控制日志输出的标准日志级别。日志 Level 对象是有序的,并且是通过有序的整数来指定。在给定的级别上启用日志记录也就启用了所有较高级别的日志记录。(这一点倒是与log4j一致)

  客户机一般应该使用预定义的 Level 常量(如 Level.SEVERE)。

  各级别按降序排列如下:

  • SEVERE(最高值)
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST(最低值)

  此外,还有一个级别 OFF,可用来关闭日志记录,使用级别 ALL 启用所有消息的日志记录。

  而我们在logging.properties中可以看到,其默认的输出级别是INFO,源码如下:

  

# 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