1. Preface
log4j is a reliable, fast and flexible logging framework (API) written in Java, released under the Apache Software License. Log4j has been ported to languages such as C, C++, C#, Perl, Python and Ruby.
Log4j is highly configurable and can be configured via external files at runtime. It depends on the priority level of the record and provides mechanisms to indicate the logging information to many destinations, such as: databases, files, consoles, UNIX system logs, etc.
There are three main components in Log4j:
loggers: Responsible for capturing record information.
appenders: Responsible for publishing log information to different preferred destinations.
layouts: Responsible for formatting log information of different styles.
Note: This article is based on log4j 2.X and above.
2. Installation
log4j-core-xx.jar
log4j-api-xx.jar
log4j-web-xx.jar (reference required for web projects)
3. Configuration
Prepare some logs to add the following references:
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger;
static Logger logger = LogManager.getLogger(Test.class.getName());
The configuration file location is located in: src root directory. Even if there is no configuration file, there will be no error. By default, it is output in the form of console .
The log4j2 configuration file is very different from log4 (the 1.X version is very different), and can only use .xml, .json or .jsn formats. The specific configuration is as follows ( ${web:rootDir} represents the web root directory):
<?xml version="1.0" encoding="UTF-8"?><configuration status="error"> <!--Define all appenders--> <appenders> <!--Configuration of this output console--> <Console name="Console" target="SYSTEM_OUT"> <!--This is the format of the output log--> <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> </Console> <!--The file will print out all information. This log will be automatically cleared every time it runs, which is determined by the append attribute, suitable for temporary testing--> <File name="Error" fileName="${web:rootDir}/logs/error.log" append="false"> <!--The file only records information at level and above (onMatch), and the others are directly rejected (onMismatch) --> <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout charset="UTF-8" pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> </File> <!--This will print out all the information. Each time the size exceeds the size, the log of this size will be automatically stored in the folder created by year-month and compressed as an archive--> <RollingFile name="RollingFile" fileName="${web:rootDir}/logs/history.log" filePattern="log/$${date:yyyy-MM}/history-%d{MM-dd-yyyy}-%i.log.gz"> <PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/> <SizeBasedTriggeringPolicy size="50MB"/> </RollingFile> </appenders> <!--Then define the logger. Appender will only take effect if the logger is defined and introduced. --> <loggers> <!--Create a default root logger--> <root level="trace"> <appender-ref ref="Error"/> <appender-ref ref="RollingFile"/> <appender-ref ref="Console"/> </root> </loggers></configuration>4. Ordinary projects and web projects
For ordinary projects, the above configuration can be used normally after completing the above configuration, and for web projects, log files will not be generated. You need to add the following configuration under the root node of <web-app> in web.xml:
<!-- log4j2.x start --><listener> <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class></listener><filter> <filter-name>log4jServletFilter</filter-name> <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class></filter><filter-mapping> <filter-name>log4jServletFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping><!-- log4j2.x end -->
Summarize
The above is the method of using log4j to record logs in Java. I hope the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.