原生Logger的logging.properties配置文件簡單分析

前言

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

這裏是對自定義的日誌記錄器配置,指定日誌記錄的記錄級別,低於該級別的日誌信息不會被處理。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章