在Spring Boot 中禁用其他异常日志记录为System.err

h5qlskok  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(121)

我尝试以json格式记录所有日志消息,但我的应用程序将所有异常记录为System.err.
我的log4j2配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <JsonTemplateLayout eventTemplateUri="classpath:gelf-layout.json" locationInfoEnabled="true"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root>
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

字符串
如果抛出异常,我会看到以下日志输出:

{"version":"1.1","time":"2024-01-04T14:37:06.086+0100","level":6,"short_message":"Error 404 occured ","stack_trace":".........}}

Jan. 04, 2024 2:37:06 PM org.apache.catalina.core.StandardWrapperValve invoke
ERROR: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is de.mycompany.exceptions.Server5xxCommunicationException: Fehler beim Erstellen einer Info.] with root cause
java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:208)
    at java.base/java.util.ImmutableCollections$MapN.<init>(ImmutableCollections.java:1186)
    at java.base/java.util.Map.of(Map.java:1395)


JSON格式看起来不错,但我想在json日志输出下面禁用额外的错误日志记录。
即使我完全禁用log4j2日志记录,除了控制台中的此错误消息外,也没有任何日志。

Jan. 04, 2024 2:37:06 PM org.apache.catalina.core.StandardWrapperValve invoke
ERROR: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is de.mycompany.exceptions.Server5xxCommunicationException: ...

huus2vyu

huus2vyu1#

经过一段时间的努力,这个堆栈跟踪,我能够禁用java.util.Logging为org.apache.*在SpringBootApplication。

public static void main(String[] args) {Logger geloggt werden.
   var logger = java.util.logging.Logger.getLogger("org.apache");
   logger.setLevel(Level.OFF);
   SpringApplication.run(Application.class, args);
}

字符串

相关问题