Log restful endpoints on container startup in a Spring application

后端 未结 3 1588
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 16:11

I\'ve a Spring application that expose restful endpoints through @RequestMapping annotation in Controller classes.

I wish that were logged into the console, at serve

相关标签:
3条回答
  • 2020-12-10 16:55

    In log4J, add the info log level for org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping class.

    With the level INFO :

    log4j.category.org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping=INFO
    

    You should have this kind of information (lines are truncated):

    2016-11-15 23:34:30.040  INFO 10156 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/contacts/{id}],methods=[GET],produces=[application/json]}" o
    2016-11-15 23:34:30.040  INFO 10156 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/contacts/{id}],methods=[DELETE],produces=[application/json]}
    2016-11-15 23:34:30.040  INFO 10156 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/contacts],methods=[POST],consumes=[application/json],produce
    2016-11-15 23:34:30.040  INFO 10156 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/contacts/{id}],methods=[PUT],consumes=[application/json],pro
    2016-11-15 23:34:30.040  INFO 10156 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/contacts],methods=[GET],produces=[application/json]}" onto p
    2016-11-15 23:34:30.040  INFO 10156 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/contacts/search],methods=[GET],params=[group-id],produces=[a
    

    UPDATE

    Since Spring MVC 5.1/Spring Boot 2, the logging strategy has changed.
    Now very few information are logged with the INFO level, the DEBUG level provides more information but it is not detailed.
    Only the TRACE level will provide detail information.

    Here is the changelog (emphasis is mine) :

    Logging revision:

    Spring's JCL bridge can be detected by standard Commons Logging.

    Less noise on info, readable debug logs, details at trace level.

    So change the logging configuration in this way to list all mappings :

    Log4J properties way :

    log4j.category.org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping=TRACE
    

    Logback way :

    <logger level="TRACE" name="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    
    0 讨论(0)
  • 2020-12-10 16:57

    For those who use spring-boot:

    In the latest spring-boot release (since v2.1), they changed the mapping default log level (as specified in the Release Notes here).

    Add one of the following properties to application.properties file:

    • logging.level.web=TRACE
    • logging.level.org.springframework.web=TRACE

    Sample Console Output:

    2018-12-12 11:16:51.793 TRACE 11868 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping
        c.n.c.MyController:
        {POST /users}: addUser(User)
        {DELETE /users}: deleteUser(User)
        {PUT /users}: updateUser(User)
        {GET /users/{id}}: getUserById(String)
        {GET /users}: getUsers()
        {GET /users_static}: getUsersStaticList()
    2018-12-12 11:16:51.795 TRACE 11868 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping
        o.s.b.a.w.s.e.BasicErrorController:
        { /error}: error(HttpServletRequest)
        { /error, produces [text/html]}: errorHtml(HttpServletRequest,HttpServletResponse)
    
    0 讨论(0)
  • 2020-12-10 17:02

    To add to the previous answer in Spring Boot there is the Actuator that among other things is exposing a dedicated Endpoint called mappings accessible under /mappings.

    0 讨论(0)
提交回复
热议问题