在官方手册dmsdk.pdf中提到了如何自定义消息通知,但是没有找到如何将自定义方法的日志输出到DEM log日志文件中。以下内容是本地开发环境对此部分的补充。
public class MyNotify implements INotify {
// 获取当前类对应的Logger(建议每个类单独声明),这是将日志输出到DEM log文件的关键
private final Log logger = LogFactory.getLog("MyNotify");
public static final String LINE_SEPARATOR = System.lineSeparator();
@Override
public boolean send(NotifyUser user, Notify notify) {
// 获取notify对象数据,拼接警告消息
StringBuilder notifyContent = new StringBuilder(256);
notifyContent.append("警告ID:").append(notify.id);
notifyContent.append(LINE_SEPARATOR);
notifyContent.append("异常发现时间:").append(notify.gmtHappen);
notifyContent.append(LINE_SEPARATOR);
notifyContent.append("警告生成时间:").append(notify.gmtCreate);
notifyContent.append(LINE_SEPARATOR);
notifyContent.append("警告所属告警配置的名称:").append(notify.alertName);
notifyContent.append(LINE_SEPARATOR);
notifyContent.append("警告所属的规则的名称:").append(notify.ruleName);
notifyContent.append(LINE_SEPARATOR);
notifyContent.append("资源类型:").append(notify.resType);
notifyContent.append(LINE_SEPARATOR);
notifyContent.append("警告所属的资源的名称:").append(notify.resName);
notifyContent.append(LINE_SEPARATOR);
notifyContent.append("警告级别:").append(notify.level);
notifyContent.append(LINE_SEPARATOR);
notifyContent.append("警告具体内容:").append(notify.message);
......
// 定义布尔变量,记录目标方法是否执行成功(初始为false)
boolean isWechatSuccess = false; //企业微信通知
boolean isDingIngSuccess = false; //叮叮通知
// 判断是否开启微信通知
boolean containsWechatInArray = Arrays.asList(strArray).contains("WECHAT");
if (containsWechatInArray) {
isWechatSuccess = sendWechatMsg(notifyContent); // 执行微信相关业务逻辑
}
// 判断是否开启钉钉通知
boolean containsDingIngInArray = Arrays.asList(strArray).contains("DINGING");
if (containsDingIngInArray) {
isDingIngSuccess = sendDingTalkMsg(notifyContent); // 执行叮叮相关业务逻辑
}
// 返回消息发送结果
return isWechatSuccess || isDingIngSuccess;
}
}
此后就可以将日志输出到webapps/dem/log目录下的日志文件中。在代码中使用例如:
logger.info("输出内容");
logger.warn("输出内容");
logger.error("输出内容");
文章
阅读量
获赞
