The best practice of the NLOG4J framework in the development of the Java class library
The best practice of the NLOG4J framework in the development of the Java class library
introduction:
NLOG4J is a log tool that is widely used in the development of the Java library.It provides a simple and powerful way to record the running log in the application.This article will introduce the best practice of using NLOG4J in the development of Java libraries, and provide some Java code examples to help readers better understand.
1. Add NLOG4J dependence
First, we need to add NLOG4J to the project.You can add the following dependencies in Maven or Gradle configuration files:
Maven:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
Gradle:
groovy
implementation 'org.apache.logging.log4j:log4j-core:2.14.1'
Second, configuration log file
Create a log configuration file (such as log4j2.xml) in the project, and specify the format, level, and storage location of the log output.The following is a simple example configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="FileAppender" fileName="logs/app.log">
<PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>
</Configuration>
In the above configuration file, we record the log into a file called "Logs/App.log" and use a custom log format.
3. Use NLOG4J in the Java class
1. Import the nlog4j library
In the Java class that needs to be used to use NLOG4J, first of all, you need to import the NLOG4J library:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2. Create a logger instance
Create a static logger instance in the class to process the log record:
private static final Logger logger = LogManager.getLogger(YourClassName.class);
Please note that "YourclassName" is replaced with your class name.
3. Logging
Where to record logs, use a logger instance to record the log.NLOG4J provides multiple log levels, including Trace, Debug, Info, Warn, ERROR and FATAL.Here are some examples:
logger.trace("This is a trace message.");
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.");
Select the right log level according to the actual situation.
Fourth, use log tool
Sometimes, we may need to contain some detailed information of variables or objects in the log.NLOG4J provides a variety of ways to achieve this.
1. Use parameterized log message
String name = "John Doe";
int age = 30;
logger.info("User '{}' is {} years old.", name, age);
2. Use abnormal information
try {
// Some operations that may throw abnormal abnormalities
} catch (Exception e) {
logger.error("An error occurred.", e);
}
By passing the abnormal object to the log method, the abnormal information can be included in the log.
5. Summary
This article introduces the best practice of using NLOG4J in the development of the Java library.First, we need to add NLOG4J dependence.Then, configure the log file to define the format and storage position of the log output.Next, we used NLOG4J to record logs in the Java class and demonstrated some commonly used log record operations.Finally, we provide some techniques to include variables and abnormal information in the log.
Using NLOG4J can easily record the running log in the application and provide flexible configuration options.It is one of the indispensable tools in the development of the Java library.I hope this article provides some help for everyone to use nlog4j for log records.