Recently, I encountered a performance problem caused by logs in my work. On weekdays, I only use simple and practical @slj4j@log4j2 and other annotations to record logs, but I didn't comprehensively learn and summarize log records, so I systematically studied java logs.
1.java log framework
Existing java logging framework
JUL(java util logging),logback,log4j,log4j2
JCL(Jakarta Commons Logging),slf4j( Simple Logging Facade for Java)
Log facade JCL, slf4j
Log implementation JUL, logback, log4j, log4j2
The log facade is simply an interface. The specific log implementations include JUL, logback, log4j and log4j2. At present, log4j2 is the mainstream.
Historical sequence of log frames: log4j -- > Jul -- > JCL -- > slf4j -- > logback -- > log4j2
2.JUL
The full name of JUL is Java util Logging, which is the native logging framework of Java
The general principle is as follows:
Loggers: called loggers, applications publish log information by obtaining the logger object and calling its API. Logger is usually the entry program for applications to access the logging system. Appenders: also known as handlers. Each logger is associated with a group of handlers. The logger will send the log to the associated handlers for processing, and the handlers are responsible for recording the log. Handlers is an abstraction here. Its specific implementation determines the location of logging, which can be console, file, other logging services on the network or operating system logs.
Layouts: also known as Formatters, it is responsible for converting and formatting the data in log events. Layouts determine the final form of data in a log record.
Level: each log message has an associated log level. This level roughly guides the importance and urgency of log messages. I can associate level with Loggers and Appenders so that we can filter messages.
Filters: filters, which can be customized according to needs, which information will be recorded and which information will be let go.
The basic usage is as follows:
The log Level can refer to the Level class
Log profile
Default configuration file path $JAVAHOME\jre\lib\logging.properties
Take a look at the configuration file in the 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 principle:
1. Initialize LogManager 1. Load logging.properties configuration for LogManager 2. Add Logger to LogManager
2. Obtain the Logger from the singleton LogManager
3. Set the Level level and specify the log record LogRecord
4. Filter provides finer grained control beyond the log level
5. The handler is used to process the log output location
6. Formatter is used to format LogRecord
The formatter and filter provided in JDK are as follows. For specific principles, please refer to the source code
log4j is basically not used now. Skip it directly.
reference material: