Trying to create a "general" logging solution for requests in Spring Boot - spring

I'm currently trying to create a info log for every request in the API I'm working on, but instead of creating a log for every request at the controller level, I wanted to try creating a "catch all" solution but I'm not being able to fullfill my goals.
I've decided to start the idea using Spring's #Aspect annotation, but using it isn't the best idea because I'd have to create one aspect for every controller that I have.
After some digging I found 2 possible solution paths that I could try to work on, #ControllerAdvice and creating an interceptor (which in theory is the one idea that I think has the most chances of succeeding and being a clean solution).
The problem is that I'm not being able to find much information regarding these ideas.
For the #ControllerAdvice idea I'm only finding it being used to handle exceptions, and for the interceptor idea I'm not finding anything similar to what I want to create.
Are these ideas feasible at all? Or should I just stick to logging everything at controller level?
Not sure if it's important but I'm currently working using LogBack library.

There is a ready solution for complete request and response logging for different client- and server-side technologies.
I'm using logbook tool for tracking request-response info for my rest endpoints.
https://github.com/zalando/logbook

Related

Quarkus discovering of JAXRS-Endpoints

I'am currently trying to understand the discovering of JAXRS-Endpoints in Quarkus. My assumption is that they are automatically discovered and there is no need to register them. In addition I tried to import Endpoints from an other module/jar File. It is working to out of the box.
But than I tried to understand the order of discovering endpoints.
I tried to overwrite a jar file provided endpoint in my application, but it is not working. Therfore in my opinion there is a potential security problem, if any third party module can overwrite my endpoints. Has anyone the same problems and can provide me informations how to think about this problem? (My only solution is to write an own extension which removes all endpoints an add only the ones I want to have, but I think this is against the idea)
Thanks in advance!
Duplicate endpoint detection was added to RESTEasy Reactive in https://github.com/quarkusio/quarkus/pull/15037.
This means that from version 2.7.0.CR1 and on, Quarkus will fail the build if duplicate JAX-RS endpoints are defined (assuming you are using quarkus-resteasy-reactive).

How correctly add RetryAdvice to Jms.messageDriverChannelAdapter

I'm very new to Spring Integration DSL and I need to add retrial with recovery to the existing IntegrationFlow. It takes data from Jms.messageDriverChannelAdapter and send it through different channels. I want to add RequestHandlerRetryAdvice for the whole flow.
My idea was to add it to the adapter, but it seems to support only ErrorChannel, without Advices & Retries and such. I've ended up wrapping the whole flow after Jms.messageDriverChannelAdapter in a .gateway with advice and .requireReply(false). It feels like a hack, but I couldn't find a better way to do it. Is there any better way to deal with such situation?
Unlike Spring AMQP's listener container a JMS one doesn't provide a hook to inject Advice chain into it, so, we really don't have choice and workaround it somehow. The RequestHandlerRetryAdvice and a .gateway() DSL is not so bad idea to go. The point is that a RequestHandlerRetryAdvice is applied only for the particular handleRequestMessage() method and is not propagated downstream. Having the flow wrapped into the gateway call from the mentioned handleRequestMessage() is the way to go.
I think we can come up with the solution on the JmsMessageDrivenEndpoint to wrap an internal listener with injected Advice chain, where one of them might be a RetryOperationsInterceptor.
Please, raise a GH issue and we'll think what we can do.

Spring MVC Identifying the Controller used for a given url/view

I am working on a well undocumented project and I was wondering if is it possible to get the controller used for a certain url or view?
Without knowing more information, one of the easiest ways is to turn on DEBUG logging for the Spring servlet. For example if you are using log4j:
log4j.logger.org.springframework.web.servlet=DEBUG
After you turn that on, you'll see log entries anytime a page is hit like this:
Mapping [/some/path] to HandlerExecutionChain with handler [com.myapp.controller.MyController#4cda661a]
Anyway, there are various ways to do this, including scanning the code for the view name or path. Another approach would be to create a interceptor that logs some additional information for every request.
You could try Spring Tool Suite (comes either standalone or as an add-on for Eclipse). It's essentially Eclipse with extra Spring-specific features, one of them is what you're looking for:

Spring Context Event

I am currently studying Spring.
While reading a Spring book, I met a part regarding Event.
By using context.publishEvent(..), I could trigger the event.
But I don't know what It's exactly for.
I can use other Method instead of using complicated publishEvent.
Please, tell me. thank you.
Spring Events are used to implement publish-subscribe model (or observer pattern) where two not-related parts of code must be somehow connected.
Think of the analogy of web applications where servlet container creates http sessions and your code is informed about this using javax.servlet.http.HttpSessionListener.
Spring uses this mechanism internally. It's much more visible in Spring Security where several parts of the code are informed about e.g., successfull authentication.

How to log efficiently with configurations?

I am using Spring framework in my application and it is deployed on MULE server.
Based on debug or info level, the amount of logging and the percentage of logging will vary.
Till date, I write the log statements explicitly in all my business logic.
Is there any way to do this through configuration, say configure at some point -
CLASS NAME - METHOD NAME - LOG AT ENTRY POINT WITH INPUT PARAMETERS - LOG AT EXIT POINT WITH RETURN
This way my code will not look very clutter.
I am not sure exactly what you are asking, but it sounds like you would like to automatically log entry and exit from methods along with parameters. If that is the case, you might consider some form of Aspect Oriented Programing (AOP). Here and here and here are some links to pages with good examples of implementing exactly this kind of logging with PostSharp. Since you are using Spring (.NET or just Spring?), you might know that Spring.NET has an AOP solution (or here for Spring). Here is a project from CodeProject that provides a log4net logging aspect already written for PostSharp (not sure if this is currently up to date or not). There are other AOP solutions out there, PostSharp is one of the more popular.
I answered with a .NET slant because that is what I am more familiar with and you didn't indicate a language preference (via the tags) in your question.
As wageoghe mentioned, using Spring AOP is an option.
Another one, since you're running in Mule, is to use component interceptors around your Spring beans.

Resources