I try write messages of my custom log level "BSHARING_LOG" to separate log file "bsharing.log". After start app, empty file 'bsharing.log' appears, but no messages in it. Both "test log message2" and "test log message" are on console. But not in file 'bsharing.log'. Messages for standart levels writes to 'application.log'. What's wrong?
我尝试将我的自定义日志级别“BSHARING_LOG”的消息写入单独的日志文件“bSharing.log”。启动应用程序后,会出现空文件‘bSharing.log’,但其中没有消息。“测试日志消息2”和“测试日志消息”都在控制台上。但不在文件‘bSharing.log’中。将标准级别的消息写入‘Applation.log’。怎么了?
Part of my pom.xml:
我的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:
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>
class:
班级:
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);
}
}
If
如果
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="appLog" />
<AppenderRef ref="bsharingLogAppender" />
</Root>
then logs writes to both files.
I can't understand, why my custom log messages appears only in console, but not in file 'bsharing.log'.
然后记录对这两个文件的写入。我不明白,为什么我的自定义日志消息只出现在控制台中,而不出现在‘bSharing.log’文件中。
更多回答
Instead of changing the title, can you write your solution as an answer and mark it as accepted?
你能把你的解决方案写为答案,并标记为接受,而不是改变标题吗?
If you are looking to log a specific aspect of your application to a separate log file, that is what markers are for (see Markers documentation). There is also a filter for markers.
如果您希望将应用程序的某个特定方面记录到单独的日志文件中,这就是标记的用途(请参阅标记文档)。还有一个用于标记的过滤器。
Hello. It is great that you want to share your solution with others! But please note that on this site solutions are meant to be posted as "answers" which are separated from problem description (the "question" section). This allows us to vote and comment on each answer separately from question. If you don't see area/form to post an answer (question authors usually don't need it so it may be hidden), you may need too click option like "answer your question" somewhere below your question. Good luck!
你好。您想要与他人分享您的解决方案,这很棒!但请注意,在本网站上,解决方案是以“答案”的形式发布的,与问题描述(“问题”部分)分开。这允许我们对每个答案进行投票和评论,而不是问题。如果你没有看到区域/表格来张贴答案(问题作者通常不需要它,所以它可能会被隐藏),你可能需要在问题下方的某个地方单击选项,比如“回答你的问题”。祝好运!
(Posting an answer on behalf of the question author to move it to the answer space).
(代表问题作者张贴答案以将其移动到答案空间)。
I have solved it.
我已经解决了这个问题。
File log4j2.xml:
文件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}" />
<LevelRangeFilter minLevel="BSHARING_LOG" maxLevel="BSHARING_LOG" onMatch="DENY" onMismatch="ACCEPT"/>
<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}" />
<LevelRangeFilter minLevel="BSHARING_LOG" maxLevel="BSHARING_LOG" onMatch="ACCEPT" onMismatch="DENY"/>
<Policies>
<SizeBasedTriggeringPolicy size="19500KB" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="appLog" />
<AppenderRef ref="bsharingLogAppender" />
</Root>
</Loggers>
</Configuration>
See LevelRangeFilter params.
请参见LevelRangeFilter参数。
更多回答
我是一名优秀的程序员,十分优秀!