[实战教学]将log.error的日志输送到钉钉群告警_钉钉群

大概思路

拦截所有的log.error,然后统一通过钉钉的API进行输送到钉钉群。

实现方法

首先需要自定义一个日志appender,我们采用继承UnsynchronizedAppenderBase的方式,代码如下

@Component
public class DingDingLogbackAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
@Override
protected void append(ILoggingEvent event) {
if (event.getLevel().equals(Level.ERROR)) {
DingDingUtils.send(event.getMessage());
}
}
}

代码的逻辑很简单,就是当日志级别是ERROR的时候,就通过钉钉Utils帮助类将信息发送到钉钉群告警。

然后还需要配置logback,我们以配置文件logback-spring.xml为例

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<contextName>logback</contextName>
<!-- 设置变量:输出格式 -->
<property name="log.format" value="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{68}[%line] - %msg%n"/>
<!-- 输出到控制台 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>${log.format}</pattern>
</encoder>
</appender>
<!--输出到DINGDING中-->
<appender name="DINGDING" class="com.dh.platform.core.logback.DingDingLogbackAppender">
</appender>

<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="DINGDING"/>
</root>
</configuration>


DingDingUtils的代码如下

@Slf4j
@Component
public class DingDingUtils implements ApplicationContextAware {

private static final int time_out = 2 * 60 * 1000;
private static final String address_prefix = "https://oapi.dingtalk.com/robot/send?access_token=";

private static ApplicationContext applicationContext;

public static void send(String body) {
send(applicationContext.getBean(DingDingConfig.class).getTipsKey(), body);
}

public static <E extends Exception> void send(E ex) {
send(applicationContext.getBean(DingDingConfig.class).getTipsKey(), ex);
}

public static void send(String key, DingDingContext context) {
post(getUrl(key), JsonUtils.from(context));
}

public static void send(String key, String body) {
post(getUrl(key), JsonUtils.from(DingDingContext.of().setContext(body)));
}

public static <E extends Exception> void send(String key, E ex) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
DingDingContext context = DingDingContext.of();
try {
ex.printStackTrace(pw);
post(getUrl(key), JsonUtils.from(context.setContext(sw.toString())));
} finally {
pw.close();
}
}

private static String post(String url, String body) {
//post请求代码省略
}

private static String getUrl(String key) {
return address_prefix + key;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

说明,上面的key,其实就是钉钉机器人的,如

https://oapi.dingtalk.com/robot/send?access_token=69e934acf42116653737ace4b1c9f5382ced22adf1b4c66653fd5f40fceb

就是access_token后的值


另外,如果需要针对异常,也发送到钉钉告警群,则需要使用特殊方法处理下,调用以下方法即可

public static <E extends Exception> void send(String key, E ex) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
DingDingContext context = DingDingContext.of();
try {
ex.printStackTrace(pw);
post(getUrl(key), JsonUtils.from(context.setContext(sw.toString())));
} finally {
pw.close();
}
}


最后,因为现在springboot项目比较流行,所以增加了一个配置,将钉钉群的key,通过配置文件来记录,可以动态的修改

@Data
@Configuration
public class DingDingConfig {

@Value("${dingding.tips.key}")
private String tipsKey;

}


相关文档

钉钉机器人文档:​​https://developers.dingtalk.com/document/robots/custom-robot-access?spm=ding_open_doc.document.0.0.52b57b4bxbms8G#topic-2026027​