hanboAudit
java编写的操作日志埋点插件,基于ORM框架的拦截器实现,目前兼容mybatis和hibernate。
插件功能
记录操作人、菜单、模块、功能以及数据库行级别操作前后的值,多用于审计日志展示。
源码请移步这里
插件使用
添加maven依赖
com.lmx
hanbo-audit
1.0-SNAPSHOT
hibernate配置
springMVC
hibernate.ejb.interceptor=com.lmx.commom.oplog.OpHibernateInterceptor
springBoot
spring.jpa.properties.hibernate.ejb.interceptor=com.lmx.commom.oplog.OpHibernateInterceptor
mybatis配置
logback设置appender,输出操作日志到指定文件(path需要自己设置)
0
1024
${logPath}/oplog-es.log
${logPath}/oplog-es.%d{yyyy-MM-dd}-%i.log
7
500MB
5000MB
%msg%n
0
1024
demo代码
注解声明
bean扫描com.lmx.common.oplog.OpLogAspect
@Service
public class DemoServiceDelegate {
@Autowired
DemoService demoService;
@OpLog(opId = "1", operationName = "测试一下", opName = "zhangsan")
public void hibernate() {
demoService.hibernate();
}
@OpLog(opId = "2", operationName = "测试一下", opName = "lisi")
public void mybatis() {
demoService.mybatis();
}
}
servlet拦截器声明
在filter配置中指定url pattern即可激活埋点。
springBoot
@Bean
public FilterRegistrationBean newOpLogFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
OpLogFilter opLogFilter = new OpLogFilter();
opLogFilter.setNeedOpRecordUrlList(Lists.newArrayList("/demo/cj"));
filterRegistrationBean.setFilter(opLogFilter);
filterRegistrationBean.setUrlPatterns(Lists.newArrayList("/*"));
return filterRegistrationBean;
}
springMVC
OpLogFilter
com.lmx.commom.oplog.OpLogFilter
needOpRecordUrlList
/demo/cj
OplogFilter
/*
日志输出效果
结构化输出,后续通过filebeats导入es即可,可以结合kibana或者定制化展示
{"appId":0,"operationType":"Modify","originalValue":"张三-mybatis-74009d85-6703-4f60-b715-da9e1e98530e","modifyValue":"张三-mybatis-f05cc7e1-6a84-4154-accd-18c0fae34fc3"}
{"appId":0,"operationType":"Modify","originalValue":"503","modifyValue":"603.0"}
{"appId":0,"operationType":"Modify","originalValue":"李四-mybatis-cf95c752-d375-4865-a322-4fda4857da44","modifyValue":"李四-mybatis-58b819a0-be0e-4d99-b0d4-8a3455354d19"}
{"appId":0,"operationType":"Modify","originalValue":"522.0","modifyValue":"622.0"}
{"appId":0,"operationType":"Modify","originalValue":"张三-mybatis-f05cc7e1-6a84-4154-accd-18c0fae34fc3","modifyValue":"张三-mybatis-6a541445-a609-4b53-b1f8-24d695210509"}
{"appId":0,"operationType":"Modify","originalValue":"603","modifyValue":"703.0"}
{"appId":0,"operationType":"Modify","originalValue":"李四-mybatis-58b819a0-be0e-4d99-b0d4-8a3455354d19","modifyValue":"李四-mybatis-2d903541-24e2-49ff-98f3-e50fde784a4a"}
{"appId":0,"operationType":"Modify","originalValue":"622.0","modifyValue":"722.0"}
{"appId":0,"operationType":"Modify","originalValue":"张三-mybatis-6a541445-a609-4b53-b1f8-24d695210509","modifyValue":"张三-mybatis-629e5ded-3e1d-423c-8bd3-0eed16bd44f5"}
{"appId":0,"operationType":"Modify","originalValue":"703","modifyValue":"803.0"}
{"appId":0,"operationType":"Modify","originalValue":"李四-mybatis-2d903541-24e2-49ff-98f3-e50fde784a4a","modifyValue":"李四-mybatis-3bead3f8-5099-4b9d-bbfa-f2313a573490"}
{"appId":0,"operationType":"Modify","originalValue":"722.0","modifyValue":"822.0"}
扩展
如果是分布式系统,则需要引入traceId来跟踪全链路的日志。实现方式一般是基于http请求头传递全局唯一id即可。如果有依赖APM工具,如skywalking、cat等,则可以直接获取sdk提供的traceId。