How to use Apache Log4J Core framework in Java applications

How to use Apache Log4J Core framework in Java applications Overview: Apache Log4j is one of the most commonly used log record frameworks in Java.It provides a powerful log function that allows developers to record various types of events and messages in the application.In this article, we will introduce how to use the Apache Log4j Core framework in Java applications. Step 1: Import the core dependency item of log4j First, we need to add the dependency item of Apache Log4J Core to the project.If you use Maven to build your project, you can add the following dependencies to the pom.xml file: <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.17.1</version> </dependency> Step 2: Configure log4j attribute file Next, we need to create a log4j attribute file to configure the log recorder.Create a file called log4j2.xml and place it in the resource directory of your project.In this file, you can configure logging levels, log output formats and goals. The following is a simple LOG4J2.XML configuration example: <?xml version="1.0" encoding="UTF-8"?> <configuration status="INFO"> <appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" /> </Console> </appenders> <loggers> <root level="info"> <appender-ref ref="Console" /> </root> </loggers> </configuration> In this example, we are equipped with a log outputer called "Console", which outputs the log message to the console.We use a common log message format, showing the date, thread, log level, class name and message.We also set the logging level to "Info", which means that only records of the INFO level and higher level log messages. You can also add other log output targets as needed, such as files, databases, etc. Step 3: Create log4j logger Now, we can create the log4j logger object in the Java application and use it to record the log message. import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class MyApp { private static final Logger logger = LogManager.getLogger(MyApp.class); public static void main(String[] args) { logger.debug("Debug message"); logger.info("Info message"); logger.warn("Warning message"); logger.error("Error message"); logger.fatal("Fatal message"); } } In this example, we created a static logger object in the MyApp class.We use the logmanager.getLogger method to associate the logger with the MyApp class. Step 4: Run the application and view log output Finally, you can run the application and observe the log output.According to the target you configured in log4j2.xml, you will see the log message at the console or other specified output location. For example, if you use the above log4j2.xml example configuration and set the log level to "Info", you will only see the Info level and higher level log message on the console. shell 2023-01-01 09:23:01.123 [main] INFO com.example.MyApp - Info message 2023-01-01 09:23:01.124 [main] WARN com.example.MyApp - Warning message 2023-01-01 09:23:01.125 [main] ERROR com.example.MyApp - Error message 2023-01-01 09:23:01.126 [main] FATAL com.example.MyApp - Fatal message In this way, you have successfully used the Apache Log4J Core framework in the Java application. Summarize: In this article, we introduced how to use the Apache Log4j Core framework in Java applications.You need to import the core dependency item of log4j, configure the log4j attribute file, create a log4j logger object, and use it to record the log message.By using log4j, you can easily perform flexible and powerful log records.