Log4j2
配置无法使用Spring 5
. 它在控制台上显示日志,但没有任何内容写入文件。但是文件正在生成,但它是空的。
pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
src/main/resources/log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="basePath">/apps/istore/logs</Property>
</Properties>
<Appenders>
<RollingFile name="fileLogger" fileName="${basePath}/istore.log" filePattern="${basePath}/istore-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="istore-logger" level="info" additivity="true">
<appender-ref ref="fileLogger" level="info"/>
</Logger>
<Root level="info" additivity="false">
<appender-ref ref="console"/>
</Root>
</Loggers>
</Configuration>
MyFilter.java
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MyFilter extends OncePerRequestFilter {
private static final Logger logger = LogManager.getLogger(MyFilter.class);
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
logger.info("@@@@@@@@@@@@@@doFilterInternal");
...
...
}
控制台日志:
[INFO ] 2020-06-09 12:30:49.381 [http-nio-8080-exec-2] MyFilter - @@@@@@@@@@@@@@doFilterInternal