Add log4j2 to the Web Project

To add log4j to the existing engineering code and record the log information without destroying the original code structure, we need to download the package on the official website first. I downloaded apache-log4j-2.12.0-bin, download address: https://logging.apache.org/log4j/2.x/download.html

1. First decompress apache-log4j-2.12.0-bin, we need log4j-api-2.12.0, log4j-core-2.12.0, copy toWEB-INFlib directory, then select the jar package right-click Build Path-Add to Build Path, add to the project.

2. Adding configuration code to Web.xml

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>class Path:log4j2.xml</param-value>
  </context-param>

3. Add the log4j2.xml file to the src directory. The contents of the file are as follows

<?xml version="1.0" encoding="UTF-8"?>
 <configuration status="WARN" monitorInterval="10">
    
     <appenders>
     
         <console name="Console" target="SYSTEM_OUT">
             <PatternLayout pattern="[%d{YYYY-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
         </console>
    
     <File name="log" fileName="D:/Code/logs/test.log" append="false">
        <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
     </File>

         <RollingFile name="InfoFile" fileName="D:/Code/logs/info.log"
                      filePattern="D:/Code/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">     
             <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
             <PatternLayout pattern="[%d{YYYY-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
             <Policies>
                 <TimeBasedTriggeringPolicy/>
                 <SizeBasedTriggeringPolicy size="100 MB"/>
             </Policies>
         </RollingFile>
         
         <RollingFile name="WarnFile" fileName="D:/Code/logs/warn.log"
                      filePattern="D:/Code/logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
             <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
             <PatternLayout pattern="[%d{YYYY-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
             <Policies>
                 <TimeBasedTriggeringPolicy/>
                 <SizeBasedTriggeringPolicy size="100 MB"/>
             </Policies>
             <DefaultRolloverStrategy max="20"/>
         </RollingFile>
         
         <RollingFile name="ErrorFile" fileName="D:/Code/logs/error.log"
                      filePattern="D:/Code/logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
             <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
             <PatternLayout pattern="[%d{YYYY-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
             <Policies>
                 <TimeBasedTriggeringPolicy/>
                 <SizeBasedTriggeringPolicy size="100 MB"/>
             </Policies>
         </RollingFile>
     </appenders>
     
     <loggers>
         <root level="all">
             <appender-ref ref="Console"/>
             <appender-ref ref="InfoFile"/>
             <appender-ref ref="WarnFile"/>
             <appender-ref ref="ErrorFile"/>
         </root>
     </loggers>
 </configuration>

4. Once the configuration is complete, we can start using it. Let's build a test class to test it.

package log4j2Test;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LogTest {

	private final static Logger logger = LogManager.getLogger(LogTest.class);

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		for (int i = 0; i < 10000; i++) {
			if (i % 2 == 0) {
				logger.info(i + "It's a multiple of 2.");
			}

			if (i % 3 == 0) {
				logger.warn(i + "It's a multiple of three.");
			}

			if (i % 7 == 0) {
				logger.error(i + "It's a multiple of 7.");
			}
		}
	}

}

The output is as follows

Console:

Output file:

 

Info files will hold information with a level greater than or equal to info, where info, warn, error are saved

Warning.log saves more than or equal to warn information

error.log holds information greater than or equal to error

 

 

 

Finally, if this article helps you and saves you valuable time, I hope you can give bloggers a bit of encouragement to sweep Alipay's red packets, or reward them. Thank you.

Keywords: log4j Apache xml encoding

Added by cljones81 on Wed, 31 Jul 2019 09:39:34 +0300