About interceptors implementing code debugging for log storage to DB

Source: Internet
Author: User
Tags log log

The problem is that the original system has the code of the log operation, but the log is not saved to the database eventually.

Interceptor Configuration in XML:

        < Mvc:interceptor >            <  path= "/admin/**"/>            <id= " Loginterceptor "  class=" Com.store.interceptor.LogInterceptor"/>        </ Mvc:interceptor >

Loginterceptor:

@Override Public voidPosthandle (httpservletrequest request, httpservletresponse response, Object handler, Modelandvi EW Modelandview)throwsException {List<LogConfig> Logconfigs =Logconfigservice.getall (); if(Logconfigs! =NULL) {String path=Request.getservletpath ();  for(LogConfig logconfig:logconfigs) {if(Antpathmatcher.match (Logconfig.geturlpattern (), path)) {String username=Adminservice.getcurrentusername (); String Operation=logconfig.getoperation (); String operator=username; String content=(String) request. getattribute (Log.log_content_attribute_name); String IP=request.getremoteaddr ();                    Request.removeattribute (Log.log_content_attribute_name); StringBuffer parameter=NewStringBuffer (); Map<string, string[]> parametermap =request. Getparametermap (); if(Parametermap! =NULL)                    {                         for(Entry<string, string[]>Entry:parametermap. EntrySet ()) {String parametername=Entry.getkey (); if(!arrayutils.contains (Ignoreparameters, parametername)) {string[] parametervalues=Entry.getvalue (); if(ParameterValues! =NULL)                                {                                     for(String parametervalue:parametervalues) { Parameter.append (parametername+ "=" + ParameterValue + "\ n");                    }                                }                            }                        }                    } Log log=NewLog ();                    Log.setoperation (operation);                    Log.setoperator (operator);                    Log.setcontent (content);                    Log.setparameter (Parameter.tostring ());                    Log.setip (IP);                    Logservice.save (log);  Break; }            }        }    }

When you debug your code, you find that the value of path is always "". Because "", it is not allowed to enter if (Antpathmatcher.match (), Logconfig.geturlpattern (), path) of the judgment statement, the Logservice.save (log) Inside the statement can not be executed naturally.

Antpathmatcher is spring's tool class, specifically Baidu.

The implementation of Loginterceptor can also be Baidu.

Later, instead:

@Override Public voidPosthandle (httpservletrequest request, httpservletresponse response, Object handler, Modelandvi EW Modelandview)throwsException {List<LogConfig> Logconfigs =Logconfigservice.getall (); if(Logconfigs! =NULL)        {           //String Path = Request.getservletpath ();String URL =Request.getrequesturl (). toString (); String Path= Url.substring (16);  for(LogConfig logconfig:logconfigs) {if(Antpathmatcher.match (Logconfig.geturlpattern (), path)) {String username=Adminservice.getcurrentusername (); String Operation=logconfig.getoperation (); String operator=username; String content=(String) request. getattribute (Log.log_content_attribute_name); String IP=request.getremoteaddr ();                    Request.removeattribute (Log.log_content_attribute_name); StringBuffer parameter=NewStringBuffer (); Map<string, string[]> parametermap =request. Getparametermap (); if(Parametermap! =NULL)                    {                         for(Entry<string, string[]>Entry:parametermap. EntrySet ()) {String parametername=Entry.getkey (); if(!arrayutils.contains (Ignoreparameters, parametername)) {string[] parametervalues=Entry.getvalue (); if(ParameterValues! =NULL)                                {                                     for(String parametervalue:parametervalues) { Parameter.append (parametername+ "=" + ParameterValue + "\ n");                    }                                }                            }                        }                    } Log log=NewLog ();                    Log.setoperation (operation);                    Log.setoperator (operator);                    Log.setcontent (content);                    Log.setparameter (Parameter.tostring ());                    Log.setip (IP);                    Logservice.save (log);  Break; }            }        }    }

Solved.



About interceptors implementing code debugging for log storage to DB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.