log4j2配置
1.maven引用
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zzc</groupId>
<artifactId>Demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>log4j2</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<log4j.version>2.21.1</log4j.version>
<slf4j.version>1.7.36</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</project>
2.log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration monitorInterval="1800" status="INFO">
<Properties>
<property name="filePath">D:\logs\demo\</property>
<property name="filePattern">${filePath}/info.log.%d{yyyy-MM-dd}-%i</property>
<property name="fileName">${filePath}/info.log</property>
<property name="logPattern">[%d{yyyy-MM-dd HH:mm:ss.SSS}] %t [%level] %c{1}#%M(%L)%msg%n</property>
<property name="everyFileSize">3G</property>
<property name="outputLogLevel">INFO</property>
<property name="rollingFileName">${fileName}</property>
<property name="rollingFilePattern">${filePattern}</property>
<property name="rollingMax">100</property>
</Properties>
<appenders>
<console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${logPattern}"/>
<ThresholdFilter level="${outputLogLevel}" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
</console>
<RollingFile name="RollingFile" fileName="${rollingFileName}" filePattern="${rollingFilePattern}">
<PatternLayout pattern="${logPattern}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="${everyFileSize}"/>
</Policies>
<DefaultRolloverStrategy max="${rollingMax}"/>
<Filters>
<ThresholdFilter level="${outputLogLevel}" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
</Filters>
</RollingFile>
</appenders>
<loggers>
<logger name="java.sql" level="INFO" additivity="false">
<AppenderRef ref="RollingFile"/>
</logger>
<logger name="io.netty" level="INFO" additivity="false">
<AppenderRef ref="RollingFile"/>
</logger>
<logger name="org.mybatis" level="INFO" additivity="false">
<AppenderRef ref="RollingFile"/>
</logger>
<Logger name="org.springframework" level="INFO" additivity="false">
<AppenderRef ref="RollingFile"/>
</Logger>
<logger name="org.quartz" level="INFO" additivity="false">
<AppenderRef ref="RollingFile"/>
</logger>
<logger name="org.redisson" level="INFO" additivity="false">
<AppenderRef ref="RollingFile"/>
</logger>
<logger name="httpclient" level="INFO" additivity="false">
<AppenderRef ref="RollingFile"/>
</logger>
<root level="${outputLogLevel}" includeLocation="true">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFile"/>
</root>
</loggers>
</configuration>
3.log4j2.xml部分标签详细介绍
- PatternLayout:[%d{yyyy-MM-dd HH:mm:ss.SSS}] %t [%level] %c{1}#%M(%L)%msg%n 打印出时间、线程名称、方法名称、代码行数、日志信息;
- Filters-ThresholdFilter: 如果LogEvent中的级别与配置的级别相同或更具体,则此筛选器返回onMatch结果,否则返回onMismatch值。例如,如果ThresholdFilter配置了级别ERROR,并且LogEvent包含级别DEBUG,则将返回onMismatch值,因为ERROR事件比DEBUG更明确。
附录
参考资料