Example code of log4j2 project log component
In the process of project operation, it is often necessary to debug functions and track and record user behavior. Some people are used to using system Out, but this is not recommended. It is only easy to use but not easy to maintain and has no scalability. Compared with log4j, log4j can control the delivery destination, output format and level of log information, so that we can more carefully control the log generation process.
Log4j2 is an upgrade of log4j1, with significant improvements in performance and functions, including throughput enhancement in multithreading, placeholder support, automatic reload of configuration files, etc
1、 Introduction
1. Download jar package
pox. xml
2. Configuration file
Log4j contains four configuration factory implementations: JSON, yaml, properties and XML. This article introduces the common XML methods.
Log4j has the ability to automatically configure itself during initialization. When log4j is started, it will locate all files matching the name under the classpath. The priority order is: log4j2 test properties > log4j2-test. xml > log4j2. properties > log4j2. xml
3. A simple example
XML configuration:
Java code:
Console information
22:17:47.146 [main] INFO MyApp - Hello World!
Two, module introduction
Log4j allows log requests to be printed to multiple destinations. In the log4j language, the output destination is called an appender. Currently, Appenders exist in consoles, files, remote socket servers, Apache flume, JMS, remote UNIX syslog daemons, and various database APIs. The following describes several commonly used Appenders. For more information, you can check them on the official website.
1、ConsoleAppender
Output to console, < console >
2、FileAppender
Output to file, < File >
3、JDBCAppender
JDBC appender uses standard JDBC to write log events to relational database tables. It can be configured to obtain JDBC connections using JNDI data sources or custom factory methods. Either way, it must be supported by the connection pool.
Otherwise, logging performance will be greatly affected.
If the configured jdbc driver supports batch statements and the buffer size is configured to a positive number, the log events will be batch processed.
(1)
(2) Use < datasource > to obtain JDBC connection. Only JNDI is listed here:
(3) Use < column > to specify which columns in the table to write to and how to write to them. It has no SQL injection vulnerability.
example:
(1) Date,% D /% date
Of course, you can also customize the format, such as%d{yyyy MM DD HH: mm: SS}
(2) Logger,% C /% logger
{?} - ? If it is a positive integer, it means to take n parts from the right, and a negative integer means to remove n parts from the left. I don't know why%c{-10} is a complete name. Please leave a message
(3) Log message,% m /% MSG /% message
(4) Log level,% level
Log4j2 comes with a variety of filters for direct use. We can also define filters ourselves:
MyFilter. java
log4j2. xml
Supplement:
In practical applications, it is sometimes necessary to record the user's access information, such as request parameters, user ID and so on. In log4j1, we will use MDC and NDC to store the context information of the application, while log4j2 uses threadcontext to realize the functions of both MDC and NDC.
(1) NDC uses a stack like mechanism to store context information, which is thread independent.
Use% X to output in patternlayout. Note that x is lowercase.
example:
Test. java
log4j2. xml
(2) MDC uses a map like mechanism to store information, which is thread independent.
Use% X {userid} in patternlayout to output. Note that x is uppercase.
example:
Test. java
log4j2. xml
Note that clearAll () is used to remove context maps and stack after use.
api: http://logging.apache.org/log4j/2.x/javadoc.html
Official website address: https://logging.apache.org/log4j/2.x/index.html