我尝试将自定义日志级别“BSHARING_LOG”的消息写入单独的日志文件“bsharing.log”。启动应用程序后,出现空文件“bsharing.log”,但其中没有消息。“测试日志消息2”和“测试日志消息”都在控制台上。但不在文件“bsharing.log”中。标准级别的消息写入“application.log”。你怎么了?
我的pom.xml的一部分:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.6</version>
<relativePath />
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
<Property name="APP_LOG_ROOT">logs</Property>
</Properties>
<CustomLevels>
<CustomLevel name="BSHARING_LOG" intLevel="350" />
</CustomLevels>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</Console>
<RollingFile name="appLog"
fileName="${APP_LOG_ROOT}/application.log"
filePattern="${APP_LOG_ROOT}/application-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${LOG_PATTERN}" />
<Policies>
<SizeBasedTriggeringPolicy size="19500KB" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingFile>
<RollingFile name="bsharingLogAppender"
fileName="${APP_LOG_ROOT}/bsharing.log"
filePattern="${APP_LOG_ROOT}/bsharing-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${LOG_PATTERN}" />
<Policies>
<SizeBasedTriggeringPolicy size="19500KB" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="ru.bozza.Sharing" additivity="false">
<AppenderRef ref="appLog" />
<AppenderRef ref="Console" />
</Logger>
<Logger name="bsharinglogger" level="BSHARING_LOG" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="bsharingLogAppender" />
</Logger>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="appLog" />
</Root>
</Loggers>
</Configuration>
类别:
public class CustomSecurityFilter extends GenericFilterBean {
private final Logger log = LogManager.getLogger(this.getClass());
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
log.log(Level.forName("BSHARING_LOG", 350), "test log message2");
log.log(Level.getLevel("BSHARING_LOG"), "test log message");
filterChain.doFilter(servletRequest, servletResponse);
}
}
如果
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="appLog" />
<AppenderRef ref="bsharingLogAppender" />
</Root>
然后记录对这两个文件的写入。我不明白,为什么我的自定义日志消息只出现在控制台,而不是在文件'bsharing.log'。
1条答案
按热度按时间6yt4nkrj1#
我已经解决了。
文件log4j2.xml:
请参见LevelRangeFilter参数。