How to use the Apache log4j framework in the Java library for log records

How to use the Apache log4j framework in the Java library for log records Overview: The log record is an indispensable part of software development.Apache Log4j is a powerful framework for logging. It provides rich functions and flexible configuration options, suitable for various types of Java applications.This article will guide you how to use the Apache Log4j framework in the Java library for log records. Step 1: Add log4j dependencies First, you need to add the log4j framework to your Java project.You can manually download the log4j library, or you can use the dependency management tool (such as Maven or Gradle) to add the following dependencies: Maven dependencies: <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.14.1</version> </dependency> Gradle dependencies: groovy implementation 'org.apache.logging.log4j:log4j-core:2.14.1' Step 2: Create log4j configuration file LOG4J uses the configuration file to specify the log record.Create a file called "log4j2.xml" in the project's resource directory (such as SRC/main/Resources) and use the following basic configuration: <?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration> This configuration file records the log to the console (System.out), and includes a timestamp, thread information, log level, class name and log message. Step 3: Use log4j in the Java class for log records Now you can use LOG4J in the Java library for log records.First, import the logger class in the class. import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; Then, use the following code to get the following code to obtain the logger instance: private static final Logger logger = LogManager.getLogger(YourClassName.class); Make sure that "YourclassName" is replaced with the name of the current class. In your code, you can use the following common LOG4J log level methods for log records: logger.debug("Debug message"); logger.info("Info message"); logger.warn("Warning message"); logger.error("Error message"); logger.fatal("Fatal error message"); You can use various LOG4J log level methods as needed. Step 4: Run and output When you run the application, log4j will record the log to the console according to the configuration file.According to the configuration, the log message will include timestamp, thread information, log levels, class names and log messages. Configure other APENDERS, Loggers and Layouts: You can also expand the LOG4J configuration according to needs.For example, you can record logs to files, databases or other goals.You can add and configure other APPENDERS and logers by modifying the <Loggers> and <ppenders> sections in the configuration file.You can also use different PatternLayout to define the format of the log message. Summarize: By following the above steps, you can easily use the Apache Log4j framework in the Java library for log records.This can help you better track and debug the code and make your application easier to check errors when there is a problem.