springboot配置log4j2(新手向)
导入maven依赖
<!-- 引入log4j2依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
log4j2配置文件
配置文件名字最好为log4j2-spring.xml,不然就要在application.yml文件上声明
logging:
config: xxxx.xml
配置文件详情
<Configuration status="INFO" monitorInterval="30">
<Properties>
<!-- 输出路径 -->
<Property name="logpath">./log</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
</Console>
<RollingFile name="debug" fileName="${logpath}/debug/debug.log"
filePattern="${logpath}/debug/debug_%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="24" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>\
</Policies>
<DefaultRolloverStrategy max="30">
<Delete basePath="${logpath}/debug" maxDepth="1">
<IfFileName glob="debug_*.log"/>
<IfLastModified age="15d"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="info" fileName="${logpath}/info/info.log"
filePattern="${logpath}/info/info_%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="24" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>\
</Policies>
<DefaultRolloverStrategy max="30">
<Delete basePath="${logpath}/info" maxDepth="1">
<IfFileName glob="info_*.log"/>
<IfLastModified age="15d"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="warn" fileName="${logpath}/warn/warn.log"
filePattern="${logpath}/warn/warn_%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="24" modulate="true"/>
<SizeBasedTriggeringPolicy size="50 MB"/>\
</Policies>
<DefaultRolloverStrategy max="30">
<Delete basePath="${logpath}/warn" maxDepth="1">
<IfFileName glob="warn_*.log"/>
<IfLastModified age="15d"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="error" fileName="${logpath}/error/error.log"
filePattern="${logpath}/error/error_%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="24" modulate="true"/>
<!-- 每个文件最大50M -->
<SizeBasedTriggeringPolicy size="50 MB"/>\
</Policies>
<DefaultRolloverStrategy max="30">
<Delete basePath="${logpath}/error" maxDepth="1">
<IfFileName glob="error_*.log"/>
<!-- 设置最大保存时间为15天-->
<IfLastModified age="15d"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<!--切换输出级别-->
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="debug"/>
<AppenderRef ref="info"/>
<AppenderRef ref="warn"/>
<AppenderRef ref="error"/>
</Root>
</Loggers>
</Configuration>
如何使用(实例)
使用lombok注解
@Log4j2
@SpringBootTest
class Tests {
@Test
void contextLoads() {
log.info("this is info log");
log.error("this is error log");
log.debug("this is debug log");
log.warn("this is warn log");
}
}
遇到的问题
依赖冲突
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/response/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/response/org/apache/logging/log4j/log4j-slf4j-impl/2.12.1/log4j-slf4j-impl-2.12.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]