The article directories
- 1. An overview of the
- 2. Three components
- 3. Log level
- 4. Configure the log4j.properties file
- 5. Log4j is used in the program
1. An overview of the
- Log4J is an open source project of Apache. By using Log4J in your project, you can control the output of log information to the console, files, GUI components, and even databases. You can control the output format of each log. By defining the output level of the log, you can control the output process of the log more flexibly. Convenient project debugging.
- website
logging.apache.org/log4j/2.x/
2. Three components
- Log4J is mainly composed of
Logger
(logger),Appender
(output) andLayout
(Log formatter). - Logger controls the output level of logs and whether logs are output.
- Appender specifies the log output method (ConsoleAppender console, FileAppender file, JDBCAppender, etc.);
- Layout controls the output format of log information (Simple format, HTML format, PatternLayout custom format).
3. Log level
- Log4J in
org.apache.log4j.Level
ClassOFF
,FATAL
,ERROR
,WARN
,INFO
,DEBUG
,TRACE
,ALL
There are eight log levels. - Generally, only four levels are used, the priority is from high to low
ERROR > WARN > INFO > DEBUG
.
4. Configure the log4j.properties file
- Just create it under the project path
log4j.properties
Configuration file, and configure information such as the output format of logs.Log4J
The framework automatically loads the configuration file and sets the configuration information toLogger
In the. - From the bottom up, the output level is DEBUG, and the output name is Console (you can customize it), but it must correspond to the output name above
5. Log4j is used in the program
- The log4j JAR package is introduced in the project
- Adding a Configuration File
log4j.properties
# Console output configure log4j. Appender. The Console = org.. Apache log4j. ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d [%t] %p [%c] - %m%n # Specifies the log output level and output log4j.rootLogger=DEBUG,ConsoleCopy the code
- Use in code
package com.xdr630.hdfs;
import org.apache.log4j.Logger;
/ * * *@author xdr630
* @version 1.0
* @date2021/4/10 16:15 * /
public class log4jTest {
public static void main(String[] args) {
Logger logger = Logger.getLogger(log4jTest.class);
logger.info("This is the info");
logger.warn("It's a warn");
logger.error("This is the error");
logger.fatal("It is fatal." "); }}Copy the code
- You can also export logs to a file
- Put the above
log4j.properties
Changed the level ofinfo
The output is as followsdebug
, meaning that the lowest level at this time isinfo
,info
All of the above can be output, and none of the following can be output, so nodebug
Output of logs.