How do I not log a particular type of Exception in Logback?

前端 未结 3 1379
礼貌的吻别
礼貌的吻别 2020-12-03 14:51

How do I configure Logback to ignore logging on exceptions of a particular type?

3条回答
  •  悲哀的现实
    2020-12-03 15:26

    This question is 5 years old, but I'm supplying the solution I found just to keep it up to date.

    I found a solution in the offical docs: http://logback.qos.ch/manual/layouts.html

    For my particular situation, which is a 17 year old web site running plain old servlets (ah, the days), the servlet is now throwing an exception if the user was not logged in. So here's my code snippets:

    Servlet

      try {
        InvalidLoginException.throwIfBadLogin(webUser);
    
        // main logic here
    
      } catch (InvalidLoginException e) {
        throw e;
      } catch (Throwable t) {
        log.error(t);
        throw new UnhandledException(t);
      }
    

    web.xml

    
        com.mycompany.servlet.exception.InvalidLoginException
        /servlet/Login
    
    

    From the setup above, I didn't want to log this exception as it's not really an exception, but rather a break in logic to redirect to login.

    So, the start of my logback.xml file is this:

      
        
          throwable != null && throwable instanceof com.mycompany.servlet.exception.InvalidLoginException
        
    

    and further down in the logback.xml file, my appenders:

      
        
          %d{MM-dd HH:mm:ss.SSS} %-5level %logger{36} - %msg %ex{full,InvalidLoginExceptionSuppressor}%n
          false
        
    

    Also note, in order to this work, I had to include janino to handle the expression parsing.

提交回复
热议问题