Learn how to configure and optimize the Apache log4j ™ framework and its Extras ™ extension in the Java library
Configure and optimize the Apache Log4j ™ framework and its Extras ™ extension in the Java library
Overview:
Apache Log4j ™ is a powerful framework for recording the application log.It provides a logging option that is easy to use and flexible, which can help developers comprehensively track and debug the operation of the application.In addition, LOG4J ™ also provides EXTRAS ™ extensions to provide more enhanced functions, such as mail notifications, database log records and graphics interfaces.
Step 1: Introduce log4j ™ dependencies
First, you need to introduce LOG4J ™ dependencies in the construction configuration file of the project.If you are using Maven to build a 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>
Step 2: Create a log4j ™ configuration file
Next, you need to create a log4j ™ configuration file to define the logging behavior.Create a file called log4j2.xml and place it in the SRC/main/Resources directory.The following is a simple LOG4J ™ configuration example:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console-Appender" />
</Root>
</Loggers>
</Configuration>
In this example, we define an additional additional device called Console-APPENDER, which outputs the log to the console.We also define a root recorder named ROOT, set its level to INFO, and attach Console-APPENDER to the root recorder.
Step 3: Use log4j ™ in the Java class
Now you can use the log4j ™ record log in your Java class.First, you need to import the related class of log4j ™:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Then you can create a logger object to record the log:
public class MyClass {
private static final Logger logger = LogManager.getLogger(MyClass.class);
public static void main(String[] args) {
logger.info ("This is a information log");
logger.error ("This is a wrong log");
}
}
In this example, we created a Logger object called Logger, and used the info () and error () methods to record different levels of logs.You can use different logs as you need, such as Debug, Warn, and FATAL.
Step 4: Configure and optimize LOG4J ™ Extras ™ extension
In addition to the LOG4J ™ core framework, Extras ™ expansion also provides some useful features.Here are some common LOG4J ™ Extras ™ extensions:
1. SMTP APPENDER (for sending logs through emails)
If you want to receive a log notification via email, you can use SMTP APPENDER.To use it, you need to add the following in the log4j ™ configuration file:
<Appenders>
<SMTP name="mailAppender" subject="应用程序日志" to="your-email@example.com" from="app@example.com" smtpHost="smtp.example.com" smtpPort="587" smtpUsername="username" smtpPassword="password">
<PatternLayout pattern="[%d{ISO8601}] %-5p %c{1}:%L - %m%n" />
</SMTP>
</Appenders>
Make sure the actual values of to.
2. JDBC APPENDER (used to record logs to the database)
If you want to record the log into the database, you can use JDBC APPENDER.To this end, you need to add the following in the log4j ™ configuration file:
<Appenders>
<JDBC name="databaseAppender" tableName="logs">
<Column name="timestamp" literal="true" value="%d{yyyy-MM-dd HH:mm:ss}" />
<Column name="level" pattern="%level" />
<Column name="logger" pattern="%logger" />
<Column name="message" pattern="%message" />
</JDBC>
</Appenders>
In this example, we define a JDBC APPENDER called DataBaseappender, and specify the mapping relationship of the table name and columns to be used.
3. LOG4J ™ Web (for record logs in Jee environment)
If your application is running in the Java Enterprise Environment (Jee), you can use the log4j ™ Web for log records.To use it, you need to add the following in the web.xml file:
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>WEB-INF/log4j2.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
In this example, we designated the path of the Log4j ™ configuration file and registered the ServiceTContextListener of LOG4J ™.
in conclusion:
By configured and optimized the Apache Log4j ™ framework and its Extras ™ extension, you can easily manage and record your application log.According to your needs, you can choose to use different APPENDERS output targets to define logs. You can also use log4j ™ Extras ™ extensions to increase more functions.I hope this article will help you use the log4j ™ log record.