天天看點

Java日志體系學習1--JUL

        最近在工作中遇到了一個有關于日志引起的性能問題,平日裡隻是簡單實用@slj4j @log4j2 等注解來記錄日志,但是對于日志記錄沒有全面學習總結一下,是以系統的學習一下java日志。

1.java日志架構

現有的java日志架構

JUL(java util logging)、logback、log4j、log4j2

JCL(Jakarta Commons Logging)、slf4j( Simple Logging Facade for Java)

日志門面 JCL、slf4j

日志實作 JUL、logback、log4j、log4j2

日志門面簡單來說就是接口,具體的日志實作有JUL、logback、log4j、log4j2 目前主流使用的是log4j2。

日志架構出現的曆史順序: log4j -->JUL-->JCL--> slf4j --> logback --> log4j2

Java日志體系學習1--JUL

2.JUL

JUL全稱Java util Logging是java原生的日志架構.

Java日志體系學習1--JUL

 其大緻原理如下:

Loggers:被稱為記錄器,應用程式通過擷取Logger對象,調用其API來來釋出日志資訊。Logger 通常時應用程式通路日志系統的入口程式。 Appenders:也被稱為Handlers,每個Logger都會關聯一組Handlers,Logger會将日志交給關聯 Handlers處理,由Handlers負責将日志做記錄。Handlers在此是一個抽象,其具體的實作決定了 日志記錄的位置可以是控制台、檔案、網絡上的其他日志服務或作業系統日志等。

Layouts:也被稱為Formatters,它負責對日志事件中的資料進行轉換和格式化。Layouts決定了 資料在一條日志記錄中的最終形式。

Level:每條日志消息都有一個關聯的日志級别。該級别粗略指導了日志消息的重要性和緊迫,我 可以将Level和Loggers,Appenders做關聯以便于我們過濾消息。

Filters:過濾器,根據需要定制哪些資訊會被記錄,哪些資訊會被放過。

基本使用如下所示:

Java日志體系學習1--JUL

日志級别可以參考Level類

Java日志體系學習1--JUL

日志的配置檔案

預設配置檔案路徑$JAVAHOME\jre\lib\logging.properties

看一下JDK中的配置檔案

############################################################
#  	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

# 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
           

 JUL原理:

Java日志體系學習1--JUL

1. 初始化LogManager 1. LogManager加載logging.properties配置 2. 添加Logger到LogManager

2. 從單例LogManager擷取Logger

3. 設定級别Level,并指定日志記錄LogRecord

4. Filter提供了日志級别之外更細粒度的控制

5. Handler是用來處理日志輸出位置

6. Formatter是用來格式化LogRecord的

JDK中自帶的formatter和filter如下 具體原理可以參考源碼

Java日志體系學習1--JUL
Java日志體系學習1--JUL

log4j現在已經基本不怎麼使用了,直接跳過。

參考資料:

黑馬程式員java日志架構教程,全面深入學習多種java日志架構_哔哩哔哩_bilibili