前言
logging.properties
配置文件用于原生的日志记录器进行配置,对该配置文件有一定了解可以更好的使用日志记录器。
文件路径
jre/lib/logging.properties
文件概览
############################################################
# 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/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
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
# Example to customize the SimpleFormatter output format
# to print one-line log message like this:
# <level>: <log message> [<date/time>]##
java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
############################################################
# 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
内容分析
# Default Logging Configuration File #
# You can use a different file by s filename
# with the java.util.logging.config.file system property.
# For example java -Djava.util.logging.config.file=myfile
java -Djava.util.logging.config.file=myfile
如果想要使用另一个配置文件,就要将java -Djava.util.logging.config.file
特性设置成配置文件的存储位置
除了以上方法,还可以利用原生API进行设置,在main中执行
System.setProperty("java -Djava.util.logging.config.file",myfile);
该方法会调用readConfiguration()
来重新初始化日志管理器
# "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
由注释可以得知,.level
用于全局设置日志处理器处理对象信息的级别的阈值,这里的设置的级别是INFO
。
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
java.util.logging.FileHandler.append = true
这里配置了日志文件的名字格式、文件格式
(这里是XML格式)等,
通过注释,我们可以得知日志文件的存储位置位于User
文件夹。
# 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
# Example to customize the SimpleFormatter output format
# to print one-line log message like this:
# <level>: <log message> [<date/time>]##
java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
这里配置的是输出到日志信息输出到控制台的最低级别
(这里是INFO,必须要注意该级别不能低于全局阈值
)、输出到控制台日志信息的格式
。
# For example, set the com.xyz.foo logger to only log SEVERE
# messages:com.xyz.foo.level = SEVERE
这里是对自定义的日志记录器配置,指定日志记录的记录级别
,低于该级别的日志信息不会被处理。