Use Apache log4j API to roll and archive
Use Apache log4j API to roll and archive
Overview:
Apache Log4j is a powerful and functional logging tool that helps us to achieve flexible log records and management in the application.LOG4J provides a lot of features, one of which is to easily roll and archive the log.This article will introduce how to use Apache Log4j API to implement these functions.
1. Add log4j dependencies:
First, you need to add LOG4J dependency items to your project.In the Maven project, you can add the following dependencies to the POM.XML file:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
2. Configure log4j settings:
Next, you need to configure LOG4J settings, including logging and archiving related parameters.You can modify these parameters in the sample code to meet your own needs.
properties
# log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<RollingFile name="RollingFileAppender" fileName="logs/log.txt"
filePattern="logs/log-%i.txt">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="RollingFileAppender" />
</Root>
</Loggers>
</Configuration>
The above configuration rolls the log file and archives the directory named "LOGS", and the maximum size of each file is 10MB.Keep up to 10 rolling log files.
3. Use log4j in the Java code for log records:
Now, you can use the log4j API in your Java code for log records, using the functions of rolling and archiving.
First, you need to obtain the logger object by calling the getlogger method of the Logmanager class.Pass your class name or identifier in the parameter to identify the source of the log.
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MyClass {
private static final Logger logger = LogManager.getLogger(MyClass.class);
public static void main(String[] args) {
Logger.info ("This is a log message."););
logger.error ("This is an error message.");
}
}
In the above example, we obtain the Logger object by calling the logmanager.getLogger method and save it in a static variable called Logger.In the main method, we used the Logger object to record a Info -level log message and an error -level error message.
4. Run application:
Finally, you need to run your application so that log4j records the log into the designated log file.After running the application, you will see one or more rolling log files in the "LOGS" directory, and the file name uses a specified mode.
Summarize:
It is very simple to use Apache Log4j API for log scrolling and archiving.By adding log4j dependencies, configuration log4j settings, and using related APIs in the Java code, you can easily implement the function of log scrolling and archiving.This will help you better manage and process log information generated by the application.
I hope this article can help you understand how to use Apache Log4J API for log scroll and archiving.If you need to understand a deeper understanding, you can check the official documentation of Apache Log4J.
(Note: The example of this article uses the log4j 2.x version, but the principle and method of use are basically the same in the log4j 1.x version.)