Configuration Guide of Apache Log4J Core framework
Apache Log4j is a popular Java log record framework and is widely used in development and debug applications.LOG4J provides powerful log records and log management functions, allowing developers to flexibly control the level, format and output location of the log records.This article will introduce how to configure the Apache Log4J Core framework and how to use the Java code example to set and use the log recorder.
1. Introduce log4j core dependencies
First, you need to introduce the dependence of Apache Log4J Core in your project.You can implement this by adding the following code to your project's pom.xml file:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.0</version>
</dependency>
2. Create log4j2.xml configuration file
Create a log4j2.xml file in the resource directory of the project, and add the following example configuration to the file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<Property name="logFile">logs/application.log</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="FileAppender" fileName="${logFile}">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>
</Configuration>
In this example configuration, we define two APPENDERS -CONSOLEAPPENDER and FileAppender.ConsoleAppender outputs the log to the console, and FileAppender outputs the log into a file called "Logs/Application.log".We use PatternLayout to define the format of the log message.
3. Use a log recorder
Once you complete the creation of the configuration file, you can use log4j core log recorder to record the log message.Consider the following examples of Java:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ExampleClass {
private static final Logger logger = LogManager.getLogger(ExampleClass.class);
public static void main(String[] args) {
logger.debug("This is a debug message.");
logger.info("This is an info message.");
logger.warn("This is a warning message.");
logger.error("This is an error message.");
logger.fatal("This is a fatal message.");
}
}
In the above example, we used a logger class to get a log recorder called ExampleClass.We then record different levels of log messages using different logs (Debug, Info, Warn, ERROR, and FATAL).
4. Run the application and view the log output
You can now build and run your application and view log output.You will see that printed on the console via ConsoleAppender and write it to the specified log file through FileAppender.
Through the above steps, you have successfully configured the Apache Log4J Core framework and use the Java code example to set and use the log recorder.You can modify the parameters in the log4j2.xml configuration file according to your needs to meet specific logging needs.