spring boot2.0 实现日志集成的方法(3)-mile米乐体育
目录
- 前言
- 具体实现
- 定义日志注解
- 定义日志切面
- 基本使用
- 输出信息
- 总结
前言
上一章springboot2.0实现日志集成的方法(2)主要讲解了将日志信息根据类别输出到不同的文件中,实际开发中我们需要通过日志来监控用户的操作行为、请求的耗时情况,针对耗时久的请求进行性能分析,提升系统性能。
具体实现
采用的spring aop切面技术来实现控用户的操作行为、请求的耗时情况。
定义日志注解
@target({ elementtype.method }) @retention(retentionpolicy.runtime) @documented public @interface logannotation { // 模块 string model() default ""; // 功能 string func() default ""; //描述 string desc() default ""; }
定义日志切面
@aspect @component public class logaspect { //请求监控日志,输出到不同日志文件 public static log logger = logmanager.getlogger("request-access"); /** * 定义切面 */ @pointcut("@annotation(com.test.aspect.logannotation)") private void logpoincut() { } /** * * @param joinpoint */ @before(value = "logpoincut恰卡编程网()") public void dobefore(joinpoint joinpoint) { string requestid =traceidutil.gettraceid(); logger.info("start invoke requestid:[{}]",requestid); } @around(value = "logpoincut()") public object doaround(proceedingjoinpoint jp) throws throwable { string requestid =traceidutil.gettraceid(); logger.info("enter request start requestid :[{}]",requestid); servletrequestattributes attributes = (servletrequestattributes) requestcontextholder .getrequestattributes(); httpservletrequest request = attributes.getrequest(); methodsignature signature = (methodsignature) jp.getsignature(); method method = signature.getmethod(); long starttime= system.currenttimemillis(); operationlog operationlog = new operationlog(); logannotation logannotation = method.getannotation(logannotation.class); if (logannotation != null) { string model = logannotation.model(); string func = logannotation.func(); string desc = logannotation.desc(); operationlog.setmodel(model); operationlog.setfunc(func); operationlog.setdesc(desc); } string classname = jp.gettarget().getclass().getname(); string methodname = jp.getsignature().getname(); string uri = request.getrequesturi(); string ip = iputil.getipaddr(request); operationlog.setclassname(classname); operationlog.setmethodname(methodname); operationlog.setip(ip); operationlog.seturi(uri); stringbuilder param = new stringbuilder(); object[] args = jp.getargs(); object arg = null; for (int i = 0, j = args.length; i < j; i ) { arg = args[i]; param.append(" ") .append(arg == null ? null : args[i].tostring()); if (i != (j - 1)) { param.append(",").append("\n"); } } operationlog.setparam(param.tostring()); operationlog.setcreatedate(new date()); long endtime=system.currenttimemillis()-starttime; //可以通过配置设置异常调用请求时间 long costtime=3; operationlog.setcosttime(endtime); string logstr = json.tojsonstring(operationlog); //将异常请求数据插入数据库 if(endtime>costtime){ //saveopetionlog(operationlog); } logger.info("invoke finish message:{}",logstr); object obj = jp.proceed(); return obj; } /** * 方法之后调用 * @param joinpoint * @param returnvalue 方法返回值 */ @afterreturning(pointcut = "logpoincut()") public void doafterreturning(joinpoint joinpoint) { string requestid=traceispring boot logbck mdc来实现。
到此这篇关于spring boot2.0 实现日志集成的方法(3)的文章就介绍到这了,更多相关spring boot 日志集成内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!