Springmvc several ways to record system logs

Source: Internet
Author: User

In project development, it is often necessary to log some user-operated system logs to the database, not just records in files or log4j.
The first is the simplest, the most primitive and the most cumbersome and the most stupid way: each need to record the operation of the entry method to call the new log interface.

The second is to use spring interceptors for method interception:
Create an Interceptor:
<mvc:interceptor>
<mvc:mapping path= "/**"/>
<bean class= "COM.DW. Interceptor. Loginterceptor "></bean>
</mvc:interceptor>
The class of the new interceptor inherits the spring Web's Handlerinterceptoradapter class, which has four methods that can be overridden in Spring4, such as:
Prehandle: It executes before processing, and can be used to do things like encoding processing, security restrictions, and so on.
Posthandle: It is performed before the method executes after it has been executed, and can be logged, modified, Modelview, and so on.
Aftercompletion: The last execution, whether error or not, will execute this method, can be used to record the exception information and some necessary operational records.
The Afterconcurrenthandlingstarted:controller method executes this method asynchronously when it starts executing, and posthandle needs to wait until the controller executes asynchronously.
It is important to note that the spring interceptor cannot get the parameter value of the handler function.

The third is to use spring's AOP configuration annotations to intercept:
First of the three concepts in SPRINGAOP: advice, pointcut, advisor.
Create a new class without any inheritance and implementation interface, simply add the annotation @aspect to the class. Create a method for a pointcut, annotate @pointcut, and then create a method that configures the type that needs to be notified by Joinpoint the related class to get the parameter value and the requested content. The specific pointcut and notification type expressions need to refer to the SPRINGAOP-related expression syntax. Then you need to configure &LT;AOP:ASPECTJ-AUTOPROXY&GT in the configuration file, that is, ASPECTJ dynamic agent.

Specific logging implementations can be recorded using a method that matches the requested address (because the query operation may not need to be logged), which requests that need to be logged can be configured through a configuration file, or can be implemented by annotations.
or through custom annotations to achieve log interception, by pointcut interception of specific annotations to achieve the function of logging on demand.

Springmvc several ways to record system logs

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.