The function and characteristics of the Apache Log4J Core framework in the Java class library
Apache Log4J Core framework is a powerful logging library that is widely used in the Java library.This article will introduce the functions and characteristics of the LOG4J Core framework and give the relevant Java code example.
1. Flexible configuration: The LOG4J CORE frame has a flexible configuration function. You can set the rules and behaviors of logs by configure files or programming methods.You can modify the configuration of the log record without re -compiling the code.
Example code:
First, add the dependencies of LOG4J Core to the POM.XML file:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
Then, create a configuration file called log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Finally, use log4j in the Java code for log records:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MyClass {
private static Logger logger = LogManager.getLogger(MyClass.class);
public static void main(String[] args) {
logger.debug("Debug message");
logger.info("Info message");
logger.error("Error message");
}
}
2. Various log levels: LOG4J Core provides a wealth of log levels, including DEBUG, Info, WARN, ERROR and other levels.You can choose the appropriate log level according to different needs in order to output different degrees of information.
Example code:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MyClass {
private static Logger logger = LogManager.getLogger(MyClass.class);
public static void main(String[] args) {
Logger.debug ("Debug Message"); // Debug information for detailed information during the execution of the code
Logger.info ("Info Message"); // General information is used to display important application status
Logger.warn ("Warn Message"); // Warning information, used to prompt potential questions or abnormal situations
Logger.error ("ERROR Message"); // Error information is used to indicate serious abnormal conditions
}
}
3. Various output methods: LOG4J Core supports output log information to the console, files, databases and other goals.You can choose the right output target according to actual needs.
Example code:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="File" fileName="logs/application.log">
<PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
4. Asynchronous log records: LOG4J Core supports asynchronous record logs that can improve the performance of the application.The processing and output of the log message can be performed in another thread, thereby reducing the impact on the main thread.
Example code:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MyClass {
private static Logger logger = LogManager.getLogger(MyClass.class);
public static void main(String[] args) {
logger.debug("Debug message");
logger.info("Info message");
logger.error("Error message");
LogManager.shutdown (); // Close log4j
}
}
The above is the functions and characteristics of the Apache Log4J Core framework in the Java library.Through flexible configuration, various log levels, multiple output methods, and asynchronous log records, LOG4J Core provides a powerful log record solution to facilitate developers to debug and fail to investigate applications.By using LOG4J Core reasonably, the maintenance and stability of the application can be improved.