Search code examples
springspring-bootspring-mvcspring-aop

Common Request Logging JAR Across Multiple Spring Boot Projects


I am trying to build a common request logging jar that can be used as a dependency across multiple Spring Boot projects for standardized request logging.

Example

  • Project A has 2 controller endpoints
  • Project B has 10 controller endpoints

Once the logging jar is added as a dependency to projects A & B, then when a request is handled by a controller, the following log message will be generated:

url="/api/locations", requestMethod="GET", ip="192.168.1.23", userAgent="curl/7.35.0"

I'm trying to minimize or eliminate the need for any actual code changes in projects A & B.

Do I need to implement an interceptor to accomplish this? Spring AOP?


Solution

  • Sounds like there's a couple of topics here.

    1. Shared configuration.

    What I've done is create a JAR with a logback-spring.xml config file under src/main/resources. Build that (or whatever config works for you) and have it as a dependency in your projects.

    1. Consistent logging.

    Here you can implement your own javax.servlet.Filter as a spring component, or use the CommonsRequestLoggingFilter as provided by Spring. Instantiate it as a @Bean in a @Configuration class and away you go. NB: IIRC, this doesn't log responses or request bodies.