背景说明
一般对于Java的日志处理,可能大家首先联想到的就是Log4J,Apache的这个项目确实广泛的应用于各个系统中,但是后来Sun在JDK中也添加了类似的日志功能(据说Sun当时是要把Log4J整合进来的,但是某些原因没有谈拢)。使用JDK自带的日志处理比Log4J特别好的一点就是不需要导入任何第三方Jar。这里我主要总结说明一下JDK自带日志的配置。
使用说明
首先,JDK自带日志类位于java.util.logging包中,同时在JDK安装目录的jre/lib下面有个默认的日志属性文件:logging.properties。
其次,JDK日志原理和Log4J原理类似
(1) LoggerManager类用于管理日志的配置,和Log4J的PropertyConfigurator类对应
(2) Logger类是日志信息记录的入口,和Log4J的Log类对应
(3) 日志输出位置:控制台ConsoleHandler,文件FileHandler,以及流输出StreamHandler等,和Log4J的Appender对应
(4) 日志输出格式:默认SimpleFormatter,XMLFormatter等,和Log4J的Pattern对应
最后,主要介绍如何在项目中使用这个日志系统
(1) 先贴上日志属性配置文件的内容
############################################################
# 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.pattern = %h/bugzilla-help/log%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.
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.foo.level = SEVERE
(2) 然后是使用Java代码读取属性文件
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class LoggerUtil {
public static void config() {
try {
LogManager.getLogManager().readConfiguration(LoggerUtil.class.getResourceAsStream("logging.properties"));
} catch (IOException ex) {
Logger.getLogger(LoggerUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(LoggerUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
注意:Java代码和配置文件位于同一个目录下面
(3) 要点说明
# 全局属性
handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler//这里表示同时使用控制台输出和文件输出
.level= INFO//这里表示默认值输出INFO级别以及别INFO更高级别的日志信息
# Handler相关属性
# 文件输出的相关设置
java.util.logging.FileHandler.pattern = %h/bugzilla-help/log%u.log//文件路径和文件名格式
java.util.logging.FileHandler.limit = 50000//日志文件最大允许50000字节
java.util.logging.FileHandler.count = 1//同时只允许存在1个日志文件
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter//日志输出格式
java.util.logging.FileHandler.append = true//日志内容以文件内容追加的方式写入
# 控制台输出的相关设置
java.util.logging.ConsoleHandler.level = INFO//控制台输出的级别设置
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter//控制台输出的日志格式
更具体的信息请翻看JDK文档。