Use Apache log4j framework to perform log records and log filtration

Use Apache log4j framework to perform log records and log filtration ## profile Apache Log4j is an open source log record tool for Java applications.It helps developers to record the running logs of the application so that it is easy to debug and maintain.LOG4J allows developers to configure parameters such as log level, output position and message format according to different needs, and can filter and classify log messages based on these parameters. This article will introduce how to use log4j for log records and log filters. ## prerequisite condition Before starting, you need to meet the following conditions: -Onk the basic knowledge of Java programming language. -The basic concept and usage of the Apache Log4J framework. ## Step ### 1. Add log4j dependencies First, add log4j dependencies to your Java project.You can add the following dependencies in the construction file of the project (such as maven): <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.17.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.17.2</version> </dependency> </dependencies> This will download the core library and API of log4j. ### 2. Configure log4j Next, you need to configure log4j to define log records.Create a file called `log4j2.xml` and put it in the resource folder of the project (such as` src/main/resources`).In this file, you can set the log output position, log level, log format, etc. The following is an example of the `log4j2.xml` configuration file: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="INFO"> <Appenders> <Console name="ConsoleAppender" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" /> </Console> <File name="FileAppender" fileName="logs/application.log" append="false"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" /> </File> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="ConsoleAppender" /> <AppenderRef ref="FileAppender" /> </Root> </Loggers> </Configuration> The above examples are configured with two APPENDERS, which are ConsoleAppender and FileAppender.ConsoleAppEnder outputs the log to the console, and FileAppender outputs the log to the `LOGS/Application. Log` file.You can adjust according to actual needs. ### 3. Record log After completing the configuration of log4j, you can start to record the log in the code.First, introduce the related class of log4j: import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; Then, instantiate a Logger object in your code: private static final Logger logger = LogManager.getLogger(YourClassName.class); Now you can use the Logger object to record logs.For example: logger.debug("This is a debug log message."); logger.info("This is an info log message."); logger.warn("This is a warning log message."); logger.error("This is an error log message."); According to the configuration of log4j, the log message will be output to the console and log file according to the specified format. ### 4. Filter log LOG4J allows you to filter log messages according to different conditions.For example, you can filter log messages based on log levels, log names. The following is a sample `log4j2.xml` configuration file, adding a filter called` ThresholdFilter`: <?xml version="1.0" encoding="UTF-8"?> <Configuration status="INFO"> <Appenders> <!-- ... --> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="ConsoleAppender"> <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL" /> </AppenderRef> <AppenderRef ref="FileAppender" /> </Root> </Loggers> </Configuration> The above example will associate ConsoleAppender with a ThresholdFilter, which will only accept logs in the log level of `ERROR` and above.Log messages at other levels will be filtered out. ## in conclusion This article introduces how to use the Apache Log4j framework for log records and log filters.You can add LOG4J's dependence, configure log4j and record logs in the code according to the above steps.Through reasonable configuration and use of log4j, you can better understand and debug your Java application.