打印mq异常消息记录
mq的异常日志,发现在线上有大量的异常信息,但是钉钉告警里面却没有搜到,自己已经重写了logback的TurboFilter方法,仍然无法打印。最后发现原来是自己给过滤了。代码如下:
package com.gwm.marketing.filter.log; @Component public class MarketingLogFilter extends TurboFilter { /**rocketmq异常信息打印*/ private static final String ROCKETMQ_ERROR = "rocketmq"; @Resource private SimpleBufferTriggerUtils simpleBufferTriggerUtils; @Resource private OptionalAlarmUriUtils optionalAlarmUriUtils; @Override public FilterReply decide(Marker marker, Logger logger, Level level, String s, Object[] objects, Throwable throwable) { if(logger.getName().contains("rocketmq")){ System.out.println("mq error:" + (Optional.of(logger).map(l->l.getName()).orElse(null))+ ",throwable:" + Optional.ofNullable(throwable).map(t->t.getMessage()).orElse(null)); } //todo 如果是非运行时异常 发送钉钉告警。排除运行时异常 if(throwable != null && StringUtils.isNotEmpty(throwable.getMessage()) && level.equals(Level.ERROR)){ if(optionalAlarmUriUtils == null){ optionalAlarmUriUtils = ApplicationContext.getBean(OptionalAlarmUriUtils.class); } if(!optionalAlarmUriUtils.isExistUri(logger.getName())){ if(simpleBufferTriggerUtils == null){ simpleBufferTriggerUtils = ApplicationContext.getBean(SimpleBufferTriggerUtils.class); } simpleBufferTriggerUtils.proceErrorAlarm(Tag.builder().applicationName(DingdingAlarmUtil.applicationName) .env(DingdingAlarmUtil.env) .ip(IpUtil.initIp()) .traceId(MDC.get("traceId")) .requestUri(logger.getName()) .exMessage(JSONObject.toJSON(throwable.getMessage().length() >500 ? throwable.getMessage().substring(0,500).toString():throwable.getMessage()).toString()) .build()); } }else if(Level.ERROR.equals(level) && logger.getName().contains(ROCKETMQ_ERROR)){ //如果是mq的异常 也要进行告警 if(!optionalAlarmUriUtils.isExistUri(logger.getName())){ String exMessage = Optional.ofNullable(s).orElse("") + Optional.ofNullable(objects).map(t->t[0]).orElse(null); simpleBufferTriggerUtils.proceErrorAlarm(Tag.builder().applicationName(DingdingAlarmUtil.applicationName) .env(DingdingAlarmUtil.env).ip(IpUtil.initIp()).requestUri(logger.getName()).traceId(MDC.get("traceId")) .exMessage(Optional.ofNullable(exMessage).map(t->t.length()>500?t.substring(0,500):t).orElse("")) .build()); } } //todo 如果接入kafka,则此处需要需要改为NEUTRAL,以防止kakfa的多次打印 return FilterReply.NEUTRAL; } }
本地调试时候发现,在mq异常时候 其throwable是空的。而自己刚好判断时候有写,如果为空的话,不会进行打印,所以导致打印不出来了。