1、logback邮件推送时,过滤正常错误日志:
问题背景:之前将error日志推送邮件的方式来监控系统bug,但是大量Custexception错误,我们是不需要关注的,也会打error日志而推送邮件。这样的邮件多了之后发发你好会分散我们的关注精力,所以想将Custexception的error日志过滤掉不推送邮件。
1)Custexception抛出时,message里面放入拼接了[CustException-]的错误内容信息,新建属性msg,保留原本错误内容信息,用于返回前端
2)判断表达式里面根据throwableProxy.getMessage().contains("CustException");来判断error级别的错误日志里面是否包含需要过滤的字符串[CustException]
1 <filter class="ch.qos.logback.core.filter.EvaluatorFilter"> 2 <evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator"> 3 <expression> 4 <!-- & encoded as & --> 5 if(level > WARN) { 6 if(null != throwableProxy){ 7 return !throwableProxy.getMessage().contains("CustException"); 8 } 9 return true; 10 } 11 return false; 12 </expression> 13 </evaluator> 14 <onMatch>ACCEPT</onMatch> 15 <onMismatch>DENY</onMismatch> 16 </filter>
1 public class CustException extends RuntimeException { 2 private static String DEFAULT_MSG = "CustException-"; 3 4 public CustException(String message) { 5 super(DEFAULT_MSG + message); 6 this.msg = message; 7 } 8 9 private String msg; 10 11 public CustException(int errCode, String message) { 12 super(DEFAULT_MSG + message);14 this.msg = message; 15 } 16 17 public CustException(Throwable cause) { 18 super(cause); 19 } 20 21 public CustException(String message, Throwable cause) { 22 super(DEFAULT_MSG + message, cause); 23 this.msg = message; 24 } 25 26 public String getMsg() { 27 return msg; 28 } 29 30 public void setMsg(String msg) { 31 this.msg = msg; 32 } 33 }
1 } catch (CustException ce) { 2 logger.error("BasExpressController.qryExpress.error", ce); 3 return RetUtils.resFailed(ce.getMsg()); 4 } catch (Exception e) { 5 logger.error("BasExpressController.qryExpress.error", e); 6 return RetUtils.resFailed("系统异常"); 7 }