开篇
现在应用都需要对日志进行监控或者报警,现在普遍的做法是采用EKL收集日志,然后再由Grafana进行内容展示和及告警策略等,那如果项目架构比较简单(单体应用),又不想搞那么多中间件依赖怎么办,这里有一种简单的方式可以实现~
继承UnsynchronizedAppenderBase
Springboot默认集成的是logback,所以自定义Appender
非常简单,继承一下AppenderBase
类即可。
再来看看AppenderBase
之上的Appender
下都有哪些实现
UnsynchronizedAppenderBase ch.qos.logback.core) cAsyncAppenderBase (ch.qos.ogback.core) cOutputStreamAppender (ch.qos.logback.core9:DBAppenderBase (ch.qos.logback.core.db) SendErrorMsgAppender (com.mty.jls.config)(cmAppenderBase (ch.qos.loqback.core) AbstractServerSocketAppender(ch.qos.logback.core.net.se C:NOPAppender (ch.qos.logback.core.helpers)@=SyslogAppenderBase (ch.gos.logback.core.net)(CSMTPAppenderBase (ch.qos.logback.core.net)@SiftingAppenderBase (ch.qos.logback.core.sift) CCycicBufferAppender (ch.qos.logback.core.read)@AbstractSocketAppender (ch.qos.logback.core.net) ListAppender (ch.qos.logback.core.read)
UnsynchronizedAppenderBase
。从名字就能看出来是异步的、普通的、不加锁的。 它类似于AppenderBase
,只是派生的Appender
实现类需要自己处理线程同步。
演示
定义一个SendErrorMsgAppender
这个类要继承UnsynchronizedAppenderBase
,并实现其中的抽象方法,不需要手动让它线程同步,异步就好,这样可以避免出错了导致业务操作回滚。
/** * UnsynchronizedAppenderBase 用于异步处理,不阻塞主线程, 拦截error日志,并发送到钉钉群 */ @Getter @Setter public class SendErrorMsgAppender extends UnsynchronizedAppenderBase<ILoggingEvent> { // ILoggingEvent里面是日志的内容 Layout<ILoggingEvent> layout; //自定义配置 String printString; @Override public void start() { //这里可以做些初始化判断 比如layout不能为null , if (layout == null) { addWarn("Layout was not defined"); } //或者写入数据库、redis时的初始化连接等等 super.start(); } @Override public void stop() { //释放相关资源,如数据库连接,redis线程池等等 if (!isStarted()) { return; } super.stop(); } @Override public void append(ILoggingEvent event) { if (event.getLevel() == Level.ERROR) { try { var isEnableSendLog = "true".equals(SpringUtil.environment(PropertiesConstant.IS_ENABLE_SEND_LOG)); if (isEnableSendLog) { //获取服务器Ip,告知哪台服务器抛异常 var ip = InetAddress.getLocalHost().getHostAddress(); var message = ip + " " + layout.doLayout(event); sendMsgToDingDing(message); } } catch (UnknownHostException ignored) { addWarn("获取服务器ip失败"); } } } /** * 发送日志消息到钉钉的逻辑 **/ private void sendMsgToDingDing(String msg) { Text text = new Text(); text.setContent(msg); DdMsgBody msgBody = DdMsgBody.builder().msgtype("text").text(text).build(); Long timestamp = System.currentTimeMillis(); String secret = SpringUtil.environment(PropertiesConstant.OAPI_DINGTALK_SECRET); var charsetName = "UTF-8"; // 把timestamp+"\n"+密钥当做签名字符串 String stringToSign = timestamp + "\n" + secret; try { // 使用HmacSHA256算法计算签名 Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(secret.getBytes(charsetName), "HmacSHA256")); byte[] signData = mac.doFinal(stringToSign.getBytes()); // 最后再把签名参数再进行urlEncode,得到最终的签名(需要使用UTF-8字符集) String sign = URLEncoder.encode(Base64.getEncoder().encodeToString(signData), charsetName); var paramMap = Map.of("timestamp", timestamp, "sign", sign); var sendUrl = RestTemplateUtils.buildGetUrlByMap(SpringUtil.environment(PropertiesConstant.OAPI_DINGTALK_URL), paramMap); RestTemplateUtils.executeHttpPost(sendUrl, msgBody); } catch (Exception e) { } } @Builder @Data private static class DdMsgBody { private String msgtype; private Text text; private Markdown markdown; } @Accessors(chain = true) @Data private class Markdown { private String title; } @Accessors(chain = true) @Data private class Text { private String content; } } 复制代码
追加配置
<appender>
标签的name
随意设置,但<appender-ref>
需要引用该name
, class
属性的值就是刚刚上面定义实现UnsynchronizedAppenderBase
的类
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="sendErrorMsg" class="com.mty.jls.config.SendErrorMsgAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> <layout class="ch.qos.logback.classic.PatternLayout"> <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 --> <pattern>%d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n</pattern> </layout> </appender> <root level="info"> <appender-ref ref="stdout"/> <appender-ref ref="sendErrorMsg"/> </root> </configuration> 复制代码
效果如下:
192.168.561 2020-10-3117:15:06「http-nio-80-exec-2;12234031-[ERROR I mybatisPlusInterceptor 没有获取到登录人的tenantId,可能是登录成功前调用了sql操作 com.mty.jls.contract.exception.BaseException:登录状态过期 atcom.mtyjls.utils.RbacUtil.getSecurityUser(RbacUtil.java:47) at com.mtyjls.config.mybatisplus.MybatisPlusConfig$1.getTenantId(MybatisPlusConfig java:53 at com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor.builderExpr ession(TenantLineInnerInterceptor.java:295) at com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor.processJoin( TenantLineInnerInterceptor.java:285) com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor.lambda$pro cessPlainSelect$2(TenantLineInnerInterceptor.iava:239) at java.base/iava.util.ArrayList.forEach(ArrayListjava:1511) at com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor.processPlai nSelect(TenantLineInnerInterceptor.java:238)