Skip to content

Enable WARN logging with DefaultHandlerExceptionResolver #2176

Closed

Description

@philwebb

Currently if an exception such as MissingServletRequestParameterException is thrown no stack-trace is logged. It would be useful to provide an option to add them.

Something like this in EnableWebMvcConfiguration:

        @Override
        protected void configureHandlerExceptionResolvers(
                List<HandlerExceptionResolver> exceptionResolvers) {
            addDefaultHandlerExceptionResolvers(exceptionResolvers);
            for (HandlerExceptionResolver resolver : exceptionResolvers) {
                if (resolver instanceof DefaultHandlerExceptionResolver) {
                    ((DefaultHandlerExceptionResolver) resolver)
                            .setWarnLogCategory(DefaultHandlerExceptionResolver.class
                                    .getName());
                }
            }
        }

I'm not sure if this should be on or off by default.

Activity

  1. added
    type: enhancement
    on Dec 16, 2014
  2. added this to the 1.2.1 milestone on Dec 16, 2014
  3. philwebb commented on Dec 16, 2014

    @philwebb
    MemberAuthor

    @wilkinsona @dsyer Any thoughts about this? Trying to debug #2162 highlighted the problem. Not sure if we should enable by default because the logs could get pretty full just by people posting incomplete data.

  4. wilkinsona commented on Dec 16, 2014

    @wilkinsona
    Member

    Tricky one. On the one hand, it could be used as a form of DoS attack if you managed to flood the logs with exception messages. On the other hand, it'd make problem diagnosis easier.

    I could be convinced either way but perhaps we should err toward secure by default and default to off?

  5. modified the milestones: 1.3.0, 1.2.1 on Dec 16, 2014
  6. philwebb commented on Dec 16, 2014

    @philwebb
    MemberAuthor

    Moving to 1.3. Could be another good candidate to enable during development but not in production.

  7. chrylis commented on Jan 14, 2015

    @chrylis
    Contributor

    I'm not familiar enough with the internals of the MVC chain post-controller to say whether mine is a duplicate, but they're semantically related. This one seems to be dealing with resolving controller arguments, and mine is about exceptions thrown after the controller has returned an argument during JSON/XML serialization.

  8. modified the milestones: 1.4.0, 1.3.0.RC1 on Jul 10, 2015
  9. modified the milestones: 1.4.0.RC1, 1.4.0 on Jan 8, 2016
  10. self-assigned this
    on May 24, 2016
  11. added a commit that references this issue on May 24, 2016
    745671a
  12. snicoll commented on May 24, 2016

    @snicoll
    Member

    @philwebb I gave that a try, you can find a proposal (see snicoll@745671a)

    Let's assume I have added a @ResponseStatus annotation on a custom exception, here's what I get for a single call to the service that throws that exception:

    2016-05-24 13:28:05.641  WARN 97219 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Handler execution resulted in exception: com.example.DemoApplication$MyException
    2016-05-24 13:28:05.643  WARN 97219 --- [nio-8080-exec-1] .w.s.m.a.ResponseStatusExceptionResolver : Handler execution resulted in exception: com.example.DemoApplication$MyException
    

    Your proposal above does not work as MVC ships with 3 "default" resolvers and that default is the fallback. Spring MVC logs the exception even if the handler does not ultimately resolve the model (that's what happens with ExceptionHandlerExceptionResolver).

    Long story short:

    • Enabling only DefaultHandlerExceptionResolver looks incomplete to me: there are two other implementations before this one that have a chance to resolve the exception
    • Enabling all 3 defaults can easily lead to duplicate warning log

    Thoughts?

  13. celiovasconcelos commented on Jun 8, 2016

    @celiovasconcelos

    Tested in 1.3.5.RELEASE

    I was forced to workaround like this:

    @Slf4j //lombok
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public class MyCustomException extends RuntimeException {
    
      public MyCustomException(String msg) {
        super(msg);
        log.warn(msg, this);
      }
    
    }

    Otherwise, no way to see my custom exception in the log.

  14. added
    for: team-attention
    on Jun 13, 2016
  15. removed this from the 1.4.0.RC1 milestone on Jun 22, 2016
  16. self-assigned this
    on Jun 22, 2016
  17. bclozel commented on Jun 23, 2016

    @bclozel
    Member

    SPR-14392 has been fixed.

    You should now get a single WARN log when the exception is actually resolved, like this:

    [.m.m.a.ExceptionHandlerExceptionResolver] - Resolved exception caused by Handler execution: java.lang.IllegalStateException
    

    @snicoll I think you can now merge your proposal.

  18. removed
    for: team-attention
    on Jun 25, 2016
  19. added this to the 1.4.0.RC1 milestone on Jun 27, 2016
  20. fedotxxl commented on Nov 8, 2017

    @fedotxxl

    Exception has been correctly processed by ExceptionHandler. What's the point to log it? What's the point to log it at WARN level? What's the point to enable it by default?

  21. TorosyanV commented on Oct 3, 2018

    @TorosyanV

    When I have ControllerAdvice and have method anotated with ExceptionHandler and I'm doing redirect ExceptionHandlerExceptionResolver logs with WARN level.
    Why?
    I already handled custom exception and logged it

  22. wilkinsona commented on Oct 3, 2018

    @wilkinsona
    Member

    ExceptionHandlerExceptionResolver is part of Spring Framework. If you have a question about it, please ask on Stack Overflow. If you have a suggestion for an improvement, please open a JIRA ticket.

  23. MSMTFaro commented on Nov 5, 2018

    @MSMTFaro

    @TorosyanV I have same issue and having to fix by setting log level to ERROR. I wonder if you found a better way.
    org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver: ERROR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

type: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions