Apache Log4j Core Framework Introduction and User Guide
Apache Log4j Core Framework Introduction and Use Guide
Overview:
Apache Log4J Core is a powerful and flexible log record framework, which is widely used in Java applications.It provides scalable configuration options and rich log records, enabling developers to achieve efficient log records and management in the application.This article will introduce the concept of the Apache Log4J Core framework, and provide specific guidelines and Java code examples.
1. Introduce log4j core:
To use the LOG4J Core framework, you need to introduce related dependence in the Java project.In the Maven project, the following dependencies can be added to the POM.XML file:
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
</dependencies>
2. Configure log4j core:
The log4j core configuration file uses XML or Properties format to define the configuration of the log recorder.The following is an example of using XML file configuration LOG4J Core:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="File-Appender" fileName="logs/mylogfile.log"
filePattern="logs/mylogfile-%d{MM-dd-yyyy}.log.gz">
<PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.example.myapp" level="debug" additivity="false">
<AppenderRef ref="Console-Appender" />
<AppenderRef ref="File-Appender" />
</Logger>
<Root level="error">
<AppenderRef ref="Console-Appender" />
</Root>
</Loggers>
</Configuration>
In the above configuration, we define two APPENDERS, which are Console and File.Console Appender outputs the log to the console, and File Appender outputs the log into the file.We also define two loggers, one of which is the logger of com.example.myApp. It will output the logs above the Debug level to the Console and File Appender.The other is the ROOT LOGER, which will output the logs above the ERROR level to the Console Appender.
3. Use log4j core:
It is very simple to use Log4J Core in the Java application.First of all, you need to add related introduction statements to the beginning of the class:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
You can then obtain the Logger object through Logmanager:
private static final Logger logger = LogManager.getLogger(YourClass.class);
Where you need to record the log, you can use different methods of the Logger object to record different levels of logs.Here are some commonly used methods:
logger.debug("Debug level log message");
logger.info("Info level log message");
logger.warn("Warn level log message");
logger.error("Error level log message");
4. Example code:
Here are a complete example of Java code. Demonstrate how to use log4j core record logs:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4jExample {
private static final Logger logger = LogManager.getLogger(Log4jExample.class);
public static void main(String[] args) {
logger.debug("Debug level log message");
logger.info("Info level log message");
logger.warn("Warn level log message");
logger.error("Error level log message");
}
}
After executing the above code, you can see the output log message in the console or log file.
Summarize:
Apache Log4J Core provides a simple and powerful method to record and manage the log of Java applications.This article provides a profile and usage guide for the LOG4J Core framework, covering its configuration and usage method, and given a specific Java code example.By learning and using LOG4J Core, developers can better manage the logs of the application, thereby improving the stability and maintenance of the application.