【问题标题】:How to not-abbreviate the source class name in spriing-boot's loggger name?如何在 spring-boot 记录器名称中不缩写源类名称?
【发布时间】:2015-06-17 08:51:53
【问题描述】:

当我运行 spring-boot 应用程序时,它会显示以下日志:

2014-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean:将过滤器:“hiddenHttpMethodFilter”映射到:[/*]

记录器名称缩写为: org.springframework.boot.context.embedded.FilterRegistrationBean

如何显示它的完整源类名称?

谢谢!

【问题讨论】:

    标签: log4j spring-boot


    【解决方案1】:

    默认情况下,Spring Boot 使用 Logback 日志记录。您可以通过将 logback.xml 文件放在类路径中来更改配置。他们有一个默认的base.xml,它定义了整体配置并包括他们的defaults.xml 文件。由于日志模式的定义位置,您将需要创建一个文件,其中复制了其中的一些项目。这是我创建的 logback.xml 示例:

    <configuration>
        <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
        <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
        <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
        <property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%C){cyan} %clr(:){faint} %m%n%wex"/>
        <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
        <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
        <root level="INFO">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="FILE" />
        </root>
        <logger name="org.springframework.web" level="DEBUG"/>
    </configuration>
    

    以下是使用此配置的日志消息示例:

    2015-06-17 09:02:06.511  INFO 18816 --- [           main] org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
    

    您可以根据需要进一步调整模式。请参阅this page 和 PatternLayout 部分了解可能包含的项目。

    您还可以根据 logging documentation 使用其他日志记录实现,然后根据需要进行配置。

    更新:从 Spring Boot 版本 1.3.0(本次编辑尚未发布)开始,您可以使用 logging.pattern.console 和 logging.pattern.file 属性来设置默认 Logback 配置的模式。请参阅其文档中的示例属性文件here。注意:此链接可能会更改,因为它指向构建 SNAPSHOT 文档。

    【讨论】:

    • 谢谢,帮了大忙!
    • 对不起,我来自 log4j,不熟悉 logback。基于以上配置,我们如何在日志中显示 MethodName 和 LineNumber 呢?谢谢!
    • 我更新了答案,并提供了指向它的文档的链接。我自己并不熟悉它,但它看起来类似于 log4j。根据页面,您可以使用 L 或 line 作为行号,使用 M 或 method 作为方法。还有一个“调用者”,看起来可能两者都有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2017-09-05
    • 2016-08-13
    相关资源
    最近更新 更多