【问题标题】:spring boot 2.x console log does not print mapped controller infospring boot 2.x 控制台日志不打印映射的控制器信息
【发布时间】:2018-11-04 14:27:27
【问题描述】:

我可以在网络浏览器中访问控制器,但在控制台日志中看不到它的映射消息。正常spring boot app启动时,eclipse控制台应该会打印出映射的控制器和访问的url,为什么我没看到呢?

我已经将logging.level.org.springframework.web=DEBUG 添加到 application.properties 文件中,但它没有帮助。

更新: 这是我的pom.xml 文件:

<modelVersion>4.0.0</modelVersion>

<groupId>com.yp</groupId>
<artifactId>BootTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BootTest</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

The snippet of console log when app starts

有没有办法在应用启动时显示映射的控制器信息?

【问题讨论】:

  • 你能更新代码吗?您希望在控制台中看到什么日志记录?除非您在方法中指定日志消息,否则 Spring Boot 不会将映射 url 记录到控制器
  • 您使用的是 Spring Boot 2.1 吗?它使用 Spring Framework 5.1,其中对 Spring MVC 的日志记录进行了相当大的改革。

标签: java spring spring-boot


【解决方案1】:

请查看Spring Boot 2.1 Release Notes#logging-refinements

Spring Framework 5.1 在处理 Web 应用程序(Spring MVC 或 Spring WebFlux)时重新访问了调试日志输出。如果您正在尝试调试应用程序并且想要恢复 Spring Boot 2.0 样式的日志记录,您应该将以下内容添加到您的 application.properties:

logging.level.web=debug

您可能还希望将 spring.http.log-request-details 设置为 true 以记录实际请求详细信息。默认情况下,此属性为 false,因为它可能会暴露敏感信息。

【讨论】:

  • 这(可能对我有用)我不知道如何获取这些日志记录详细信息。我在 Bootstrap 4.0 上使用命令行中的 mvn 来启动服务器实例。我发现我还需要在我的命令行中添加: -Dspring-boot.run.arguments=--logging.level.,spring.http.log-request-details= ...不确定它是否能解决我的具体问题??
  • 我可能对 log.request.details 之后的 = 有误,否则似乎没有得到太多输出??
  • 更多日志使用 logging.level.root=DEBUG
【解决方案2】:

只需在 eclipse VM 参数中添加这个-Ddebug,详细的调试日志就会开始打印。

【讨论】:

    【解决方案3】:

    正如Andy Wilkinson 所提到的,已经进行了很多大修,尤其是在日志记录方面。 commit 删除了日志。

    如果您希望查看此信息,可以将日志记录级别更改为 TRACE 而不是回退到以前的版本

    logging.level.org.springframework.web: TRACE

    【讨论】:

    【解决方案4】:

    就像 Andy Wilkinson 的评论帖子所说,我使用的是 spring boot 2.1,当我将 spring boot 版本更改为旧版本时,此问题不会再次发生。

    【讨论】:

      【解决方案5】:

      如果只想查看控制器映射日志。尝试将 TRACE 设置为 RequestMappingHandlerMapping 以防止日志过多。

      logging.level.org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping=TRACE
      

      #spring boot 2.2.4

      【讨论】:

        【解决方案6】:

        我喜欢这个答案,我认为这就是我想要的。 只需查看应用程序运行时有哪些 api。

        logging.level.org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping=TRACE
        

        然后你会看到这样的;

        c.u.c.s.c.SmsAuthController:
            {POST /api/sms/send-sms-auth-code}: sendSmsAuthCode(SmsCodeParam,HttpServletRequest)
        2568 2020-10-08 22:09:41,227 TRACE [restartedMain] o.s.w.s.m.m.a.RequestMappingHandlerMapping/detectHandlerMethods  
            c.u.c.t.TestController:
            {POST /api/test/api-retired-warning}: testApiRetiredWithWarning()
            {POST /api/test/api-retired-error}: testApiRetiredWithError()
            {POST /api/test/api-test-only}: testApiTestOnly()、
        2569 2020-10-08 22:09:41,228 TRACE [restartedMain] o.s.w.s.m.m.a
        

        【讨论】:

          【解决方案7】:

          如果您想在 Eclipse 中查看信息日志,只需将以下行添加到属性中 bootstrap.yml

          logging:
            level:
              org.springframework: INFO
          

          【讨论】:

            【解决方案8】:

            如需更多日志记录,请在引导程序/应用程序属性文件中使用 logging.level.root=DEBUG

            【讨论】:

              猜你喜欢
              • 2019-12-15
              • 1970-01-01
              • 1970-01-01
              • 2019-06-03
              • 1970-01-01
              • 2016-12-20
              • 1970-01-01
              • 2015-10-28
              • 1970-01-01
              相关资源
              最近更新 更多