# dinglog-spring-boot-started **Repository Path**: itsforkgithub/dinglog-spring-boot-started ## Basic Information - **Project Name**: dinglog-spring-boot-started - **Description**: https://github.com/cutiyu/dinglog-spring-boot-started - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-11-15 - **Last Updated**: 2023-11-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # dinglog-spring-boot-started 基于钉钉的webhook,实现钉钉消息日志报警,及封装了钉钉发消息接口 # Getting Started ### Reference Documentation For further reference, please consider the following sections: * [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) * [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.2.6.RELEASE/maven-plugin/) #### 使用钉钉发送日志报警 1、添加依赖 ```xml com.cutiyu.dinglog dinglog-spring-boot-started ${version} ``` *** 2、配置logback.xml *** [注释内容]: 这是注释 * DingTalkAppenderUnSync: 继承了 UnsynchronizedAppenderBase 的appender * DingTalkAppender,继承了AppenderBase 的appender。两者的区别,在于是否同步处理日志事件。 * dingTalkLogbackParam:为该appender 配置的一些参数: * sendMsg: true | false , 是否开启日志报警,默认 false 不开启 * markMsg: 配置钉钉机器人webHook,发消息的关键词,匹配该关键词,钉钉才会发送对应的消息,如未配置,需要发送的消息中包含webHook配置的关键词 * webHookAccessToken:钉钉webHook 的accessToken。 * layoutLog: true | false, 是否按照layoutLog 输出告警日志,默认为false, * warnLevel: 发送钉钉告警的日志级别配置,默认 ERROR级别。 ```xml ERROR %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n ${ding.talk.webhook.send.msg} ${ding.talk.webhook.mark.msg} ${ding.talk.webhook.access.token} ${ding.talk.webhook.log.layout} ERROR 0 512 ``` 完整的配置示例: ````xml ${DEV_HOME}/debug.log %d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger{36} -%msg%n ${DEV_HOME}/debug.%d{yyyy-MM-dd}.%i.log 300MB %d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger{36} -%msg%n ERROR %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n ${ding.talk.webhook.send.msg} ${ding.talk.webhook.mark.msg} ${ding.talk.webhook.access.token} ${ding.talk.webhook.log.layout} ERROR 0 512 ```` 3、注入服务,直接发送钉钉消息 配置钉钉信息 ```yaml dinglog: webhook: security: access-token: 030bxxxxccxxxxxxxx**********9bcc6f74 key-words: 【任务1】 ``` 配置信息完整属性 目前只实现了基于关键词的业务,签名和ip段的暂未实现 ```java @ConfigurationProperties(prefix = "dinglog.webhook.security") public class DingTalkWebHookProperties { /** * 业务关键词 */ private String keyWords; /** * robot token */ private String accessToken; /** * 签名的秘钥 */ private String signSecret; /** * ip段 */ private String ipSegments; ``` 注入依赖,进行测试 ```java @Autowired private DingTalkSendMsgService dingTalkSendMsgService; @GetMapping("/test") public void test() { log.error("这是一个测试:"); /** * 在application.properties 或者 application.yaml 配置了 相关属性,则直接调用 */ dingTalkSendMsgService.sendTextMsg("测试发消息"); /** * 在application.properties 或者 application.yaml 未配置相关属性规则 */ String content = "这是发送的内容"; String accessToken = ""; String keyWords = "webHook配置的关键词"; dingTalkSendMsgService.sendTextMsg(content, accessToken); dingTalkSendMsgService.sendTextMsg(content, accessToken,keyWords); } ```