本章主要是将ocp原来的[03.log-core模块](03.log-core.md)做一个代码优化,采用spring boot enable的方式重构代码。 [TOC] # log-spring-boot-starter ## 功能 * 定义logback格式,统一log输出 * 定义业务log通用json格式 * sleuth日志埋点,链路跟踪 ## 代码分析 * pom依赖 ![](https://img.kancloud.cn/4b/17/4b17b48864172eda8b003d2b1badc778_1557x778.png) * 定义log格式,方便后续logstash格式化日志 ![](https://img.kancloud.cn/f1/7d/f17dc230ad2d5425509cd9a91c47d640_1850x687.png) * LogServiceImpl 切换数据源,记录log表 ![](https://img.kancloud.cn/a6/20/a620cebd0c605f19e33672f8462713dd_1555x701.png) * LogDao记录sys_log表 ![](https://img.kancloud.cn/e7/69/e7692979bb1409d1f0bd5a80717a13ff_1889x526.png) * AOP标准日志格式 ![](https://img.kancloud.cn/48/a6/48a6294fc193406c67401dacd7fb6e33_1729x758.png) * springboot项目中是否使用过EnableCaching ![](https://img.kancloud.cn/04/54/04540f603e4ce0cddcb756c18198e1b9_1758x599.png) 下面咱们按照springboot的方式 编写EnableLogging * @Import(LogImportSelector.class) ![](https://img.kancloud.cn/4c/19/4c19ceb70317d19f5d64ce58af5bea91_1732x756.png) * LogImportSelector ![](https://img.kancloud.cn/df/97/df97100dbe8e566bbd2bcfb16872e2dc_1271x655.png) * 使用EnableLogging ![](https://img.kancloud.cn/fc/76/fc76b4f6807d767ed0671324dfd2d541_936x614.png) 总结 1.@Import用来导入一个或多个类(会被spring容器托管),或者配置类(配置类里面的bean都会被spring容器托管) 2. ImportSelector 该接口的方法的返回值都会被纳入到spring容器管理中 @Import ![](https://img.kancloud.cn/05/ca/05ca142cf92439f9bf33b4c90848af41_955x277.png) * import可以采用importselecor的返回值加入到spring容器 * import还可以自定义ImportBeanDefinitionRegistrar接口,通过BeanDefinitionRegistry纳入到spring容器 ## 开启数据库记录日志 @LogAnnotation(module="user-center",recordRequestParam=true) [26.日志埋点](27.%E6%97%A5%E5%BF%97%E5%9F%8B%E7%82%B9.md) [15.动态数据源配置](18.%E5%8A%A8%E6%80%81%E6%95%B0%E6%8D%AE%E6%BA%90%E9%85%8D%E7%BD%AE.md) 需要配置多数据源支持 ## 链路跟踪 ### spring-cloud-sleuth-core ![](https://img.kancloud.cn/d0/b6/d0b62b0206eba900f75b037d6c375060_1658x223.png) #### 网关传递traceid ![](https://img.kancloud.cn/2f/d2/2fd27cad9d242fd8d6f8eddbd4d0064b_1645x550.png) #### 方法拦截器设置traceid ![](https://img.kancloud.cn/33/b5/33b53a558234cee38084101e2679934e_1067x371.png) #### aop处理traceid ![](https://img.kancloud.cn/39/b5/39b56bb6132b788e08203132f9e4df97_1626x416.png) #### traceid处理 ![](https://img.kancloud.cn/e4/ec/e4ec7f20baf06daaeb3016d64e8661d5_1522x557.png) #### feign处理traceid ![](https://img.kancloud.cn/41/de/41deb01a55a5bb2a037a4acecb543d6a_1672x515.png) #### RestTemplate处理traceid ![](https://img.kancloud.cn/ac/7c/ac7cc117be5bc610f9d6d912e5eba4cb_1664x647.png) ### api-gateway ![](https://img.kancloud.cn/d0/83/d08379f8ec27f1dabd08eb8b9e0f5be9_1272x109.png) ### auth-server ![](https://img.kancloud.cn/e4/8c/e48cd14819a0cb33cd07db1c1f7bb3de_1180x86.png) ### grep -rn ![](https://img.kancloud.cn/44/b4/44b4a0e54243b48f8eed97452958f4eb_1897x197.png) ``` HP@gitgeek/cygdrive/d/open-capacity-platform $ grep -rn a26003208271fd94 * logs/auth-gateway/auth-gateway-info.log:740:[auth-gateway:130.75.131.208:9200] [a26003208271fd94] [a26003208271fd94] 2019-09-06 09:38:33.898 INFO 6364 [XNIO-2 task-3] com.open.capacity.client.filter.ResponseFilter request url = http://127.0.0.1:9200/api-auth/validata/code/1263967C-FED7-4732-86D7-8CC6BDF752F5, traceId = a26003208271fd94 logs/auth-gateway/auth-gateway-info.log:741:[auth-gateway:130.75.131.208:9200] [a26003208271fd94] [a26003208271fd94] 2019-09-06 09:38:36.703 INFO 6364 [XNIO-2 task-3] com.open.capacity.client.filter.ResponseFilter response url http://127.0.0.1:9200/api-auth/validata/code/1263967C-FED7-4732-86D7-8CC6BDF752F5, traceId = a26003208271fd94 oauth-center/logs/auth-server/auth-server-info.log:722:[auth-server:130.75.131.208:8000] [a26003208271fd94] [b26ed6e6877dd47a] 2019-09-06 09:38:34.795 INFO 15168 [http-nio-8000-exec-1] com.open.capacity.log.aop.LogAnnotationAOP 开始请求,transid=a26003208271fd94, url=com.open.capacity.uaa.server.controller.ValidateCodeController/createCode , httpMethod=null, reqData=["1263967C-FED7-4732-86D7-8CC6BDF752F5"] oauth-center/logs/auth-server/auth-server-info.log:723:[auth-server:130.75.131.208:8000] [a26003208271fd94] [b26ed6e6877dd47a] 2019-09-06 09:38:36.671 INFO 15168 [http-nio-8000-exec-1] com.open.capacity.log.aop.LogAnnotationAOP 请求完成, transid=a26003208271fd94, 耗时=1898, resp=null: ``` ![](https://img.kancloud.cn/1c/37/1c376482b9ed7e7626ab922002f9a6bf_1699x421.png) ## CompletableFuture.runAsync 与链路跟踪 ![](https://img.kancloud.cn/cf/f0/cff01f6b3af204ec88db042d78615248_1389x62.png) ``` [test-log-center:130.75.131.208:8080] [,d1d8bf482db0af10] 2019-09-06 13:40:25.181 INFO 18088 [http-nio-8080-exec-10] com.open.capacity.log.test.controller.TestController ok [test-log-center:130.75.131.208:8080] [,d1d8bf482db0af10] 2019-09-06 13:40:25.220 INFO 18088 [pool-1-thread-1] com.open.capacity.log.test.controller.TestController oook ``` * 异步传递MDC ![](https://img.kancloud.cn/0e/3c/0e3cd45f79d800c365d956424b2cddda_1286x632.png) * CompletableFuture 绑定自定义TaskExecutor ![](https://img.kancloud.cn/45/34/453461c7e5028fe37013cf1bf8aca8a7_1684x605.png) ## X-B3-TraceId生产的源头 * X-B3-TraceId - 128位或者64位的traceId,被编码成32位和16位的小写16进制形式 * X-B3-SpanId - 64位的spanId,被编码成16位的小写16进制形式 * X-B3-ParentSpanId - 64位的父级spanId,被编码成16位的小写16进制形式 * X-B3-Sampled - 1代表采样,0代表不采样,如果没有这个key,则留给header接受端,即服务端自行判断 - X-B3-Flags - debug,如果为1代表采样 ![](https://img.kancloud.cn/74/72/7472c9c3370f2ae9b37bfd41b2211817_1036x419.png) ### traceId生成核心逻辑 ![](https://img.kancloud.cn/b6/d9/b6d9045a64b52fa93783207b13a57611_1510x525.png) ### api-gateway生成traceId ![](https://img.kancloud.cn/c1/0d/c10da8e086113d35f0114e78ff86ea0c_863x138.png) ### new-api-gateway生成traceId ![](https://img.kancloud.cn/1a/af/1aafa7b4bc56297e8c96cd50a32bc4b6_876x273.png) ## 业务日志 ![](https://img.kancloud.cn/7a/9c/7a9c529e36df7af938ebfffdb9782717_1839x480.png) ![](https://img.kancloud.cn/61/03/6103de0ecdfa90b5b8c03b1820945d7d_1807x645.png) ![](https://img.kancloud.cn/b3/83/b3834507636c15aff8f0721f67c5656c_1807x442.png) ## 异常日志 ### 统一异常 [25.统一业务异常处理](27.%E7%BB%9F%E4%B8%80%E5%BC%82%E5%B8%B8.md) ### 异常日志 ![](https://img.kancloud.cn/0b/20/0b20f0021f8d403f09d9d9ec42fb3f1f_1610x622.png) ### Sentry 日志 ![](https://img.kancloud.cn/72/c0/72c000af359a0f1bce1b8d2e9d754aa5_1613x609.png) ### Sentry 配置 ![](https://img.kancloud.cn/3d/a3/3da3cdd16b8a5955ea3834664cc4f237_1807x852.png) ### 配置Sentry服务 ![](https://img.kancloud.cn/d7/2f/d72fb5e3e1c020907ec2dea7234061d0_1826x867.png) Sentry 界面所提供的功能很强大,可以帮助开发人员快速获取相关信息。 ### 异常的分组监控 ![](https://img.kancloud.cn/a0/20/a02020ded465768445fe49dcea50dcaf_1920x352.png) 查看此章节 [1.Elastic APM](1.ElasticAPM.md)