1. Preface
Log4j is a reliable, fast, and flexible log Framework (API) written in Java that is published under the license of Apache software. LOG4J has been ported to languages such as C,c++,c#,perl,python and Ruby.
Log4j are highly configurable and can be configured at run time by external files. It is based on the priority level of the record and provides a mechanism to indicate logging information to many destinations, such as databases, files, consoles, Unix system logs, and so on.
There are three main components in the log4j:
Loggers: Responsible for capturing record information.
Appenders: Responsible for publishing log information to different preferred destinations.
Layouts: Responsible for formatting different style of log information.
Note: This article is based on the log4j 2.X and above version.
2, installation
Log4j-core-xx.jar
Log4j-api-xx.jar
Log4j-web-xx.jar (required reference for a Web project)
3, configuration
The class that prepares the logs adds the following reference:
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 will not error, default to console the form of output.
Log4j2 configuration file with Log4 (1. The x version is very different, and can only be in the. xml,. json, or. jsn format, as follows ( ${web:rootDir}
representing the Web root directory):
<?xml version= "1.0" encoding= "UTF-8"?> <configuration status= "error" > <!--first define all appender--> <
Appenders> <!--The 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> <!--files will print out all information, this log is automatically emptied each time the program is run, and is determined by the Append attribute, suitable for temporary Test with--> <file name= "Error" filename= "${web:rootdir}/logs/error.log" append= "false" > <!-- The file records only level and above levels of information (Onmatch), other direct rejections (Onmismatch)--> <thresholdfilter level= "error" onmatch= "ACCEPT" Onmismatch= "DENY"/> <patternlayout charset= "UTF-8" pattern=. SSS}%-5level%class{36}%l%m-%msg%xex%n "/> </File> <!--This will print out all the information, the size of the log will automatically be saved by year-month, each time it is larger than sizes --> <rollingfile name= "Rollingfile" Filename= "${web:rootdir}/logs/history.log" as archived under the folder and compressed
"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 logger, Only the Appender,appender defined logger and introduced will take effect--> <loggers> <!--establish a default root logger--> <root level= "Trace" &
Gt <appender-ref ref= "Error"/> <appender-ref ref= "Rollingfile"/> <appender-ref ref= "Console"/> < /root> </loggers> </configuration>
4. General projects and Web projects
For ordinary projects, the above configuration is done to normal use, for Web projects, is not generated log files. You need to <web-app>
add the following configuration under the root node 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 use of log4j log in Java method, I hope this article content for everyone's study or work can bring some help, if you have questions you can message exchange. Thank you for your support to the cloud-dwelling community.