Log restful endpoints on container startup in a Spring application

Alberto Giantin picture Alberto Giantin · Nov 15, 2016 · Viewed 9.7k times · Source

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

I wish that were logged into the console, at server startup, all the endpoints of all the application's controller.

I use a tomcat server and log4j for logging.

Thanks.

Answer

Naor Bar picture Naor Bar · Dec 12, 2018

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)