Table of Contents
Log4J
Simple use of Log4j
Log level
Log4j components
Loggers
Appenders
Layout
Layout format
Setting configuration file loading
Configuration file analysis
Log4J
It is an open source logging framework under Apache.
Simple use of Log4j
@Test public void testLog4J(){ Logger logger = Logger.getLogger(Log4jTest.class); logger.info("hello log4j"); }
log4j:WARN No appenders could be found for logger (com.zmt.Log4jTest).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See Apache log4j 1.2 – Frequently Asked Technical Questions for more info.
It does not directly output the log as we imagined, but outputs a warning message. This means that an initial configuration information is required.
Just add a BasicConfigurator.configure().
Log level
- fatal: serious error, usually causing the system to crash and stop running.
- error: error message, will not affect system operation
- warn: warning message, problems may occur
- info: running information, database connection, network connection, IO and other information
- debug: information debugging, generally used in development (default level)
- trace: trace information, record all process information of the program
Log4j components
Log4j consists of Loggers (log recorder), Appenders (output terminal) and Layout (log formatter). Among them, Loggers controls the output level of the log and whether the log is output; Appenders specifies the output method of the log (output to the console, file, etc.); Layout controls the output format of the log information.
Loggers
It has the same parent-child relationship as the Logger of JUL (JUL-CSDN Blog of Java Logging System). There is a default root Logger object, and the parent-child relationship is also judged through the full path. If no configuration is set, the default configuration of the root Logger is used.
Appenders
Specify log output location
- ConsoleAppender: Output logs to the console
- FileAppender: Output logs to a file
- DailyRollingFileAppender: Output the log to a log file and output it to a new file every day
- RollingFileAppenger: Output the log information to a log file and specify the file size. When the file size reaches the specified size, the file will be automatically renamed and a new file will be generated.
- DBCAppender: Save log information to the database
Layout
- HTMLLayout: Format log output into HTML table format
- SimpleLayout: Simple log output formatting, the printed log format is (info-message)
- PatternLayout: The most powerful formatting period, which can output logs according to a custom format. If no conversion format is specified, the default conversion format is used.
Layout format
Only valid when Appender is PatternLayout
- %m outputs the log information specified in the code
- %p output priority, DEBUG, INFO, etc.
- %n newline character
- %r Outputs the number of milliseconds it takes from application startup to outputting the log information.
- %c outputs the full name of the class to which the print statement belongs
- %t Output the full name of the thread that generated the log
- %d outputs the current time of the server, the default is IS08601, you can also specify the format, such as:%d{yyyy-MM-dd HH:mm:ss]
- %l Outputs the location where the log time occurred, including the class name, thread, and line number in the code. Such as: Test.main{Test.java:10}
- %F The name of the file where the output log message is generated
- %L outputs the line number in the code
- %% outputs a “%” character
Test the log output in the following format
log4j.appender.console.layout.conversionPattern = Thread:%t [%5p] %d{yyyy-MM-dd HH:mm:ss}: %m%n
Set configuration file loading
Log4j supports several configuration file formats:
The path for LomManager to load configuration files is loaded through the class loader.
The loading method is as shown below
Configure files
Configuration file analysis
log4j.rootLogger=debug,console,file log4j.appender.console =org.apache.log4j.ConsoleAppender #Specify message format type log4j.appender.console.layout =org.apache.log4j.PatternLayout #Specify the conversion format content log4j.appender.console.layout.conversionPattern = Thread:%t [%5p] %d{yyyy-MM-dd HH:mm:ss}: %m%n log4j.appender.file =org.apache.log4j.FileAppender log4j.appender.file.layout =org.apache.log4j.PatternLayout log4j.appender.file.layout.conversionPattern = Thread:%t [%5p] %d{yyyy-MM-dd HH:mm:ss}: %m%n #Specify file path log4j.appender.file.file=/logs/log4j.log #Specify file encoding format log4j.appender.file.encoding=UTF-8 log4j.appender.rollingFile =org.apache.log4j.RollingFileAppender log4j.appender.rollingFile.layout =org.apache.log4j.PatternLayout log4j.appender.rollingFile.layout.conversionPattern = Thread:%t [%5p] %d{yyyy-MM-dd HH:mm:ss}: %m%n #Specify file path log4j.appender.rollingFile.file=/logs/log4j.log #Specify file encoding format log4j.appender.rollingFile.encoding=UTF-8 #Split the file when it reaches 1MB log4j.appender.rollingFile.maxFileSize=1MB #Specify the number of splits. If the splits are not enough, they will be overwritten according to the time. log4j.appender.rollingFile.maxBackupIndex= 10 log4j.appender.dailyFile =org.apache.log4j.DailyRollingFileAppender log4j.appender.dailyFile.layout =org.apache.log4j.PatternLayout log4j.appender.dailyFile.layout.conversionPattern = Thread:%t [%5p] %d{yyyy-MM-dd HH:mm:ss}: %m%n #Specify file path log4j.appender.dailyFile.file=/logs/log4j.log #Specify file encoding format log4j.appender.dailyFile.encoding=UTF-8 #Specify time splitting rules. The default unit is days. log4j.appender.dailyFile.datePattern='.'yyyy-MM-dd