Record Project log with aop+log4j

Source: Internet
Author: User
Tags aop throwable log4j
Reprinted from: http://blog.csdn.net/liu765023051/article/details/11889337

Directory (?) [+]

With regard to AOP, I have compared AOP and oop before spring--chatterabout AOP and OOP, with a clearer understanding of some of the knowledge of AOP in a macroscopic sense. But the application of AOP is only used when declarative transactions are processed. It is not yet possible to use AOP flexibly. AOP is a complement to OOP, and the following is a case study of AOP that deals with log4j logging.


first, AOP processing logs are required, and there is a lot of inconvenience in not using AOP to process logs.

Log processing is a very important part of each project. Without the log, it loses the controllability of the system. No log, system problems, there will be no traces to find, which is very dangerous for an information system.

However, the use of pure OOP thought for log processing, we will find that every logical part always mixed with the code of log processing, resulting in the design of pure oop is not a fish or fowl. Also, if the type requirements for logging are changed, then we have to modify the Java code in each logical unit. Also, if the demand changes today and tomorrow, I think it will be a very heavy and annoying job.

In fact, the log processing should be a separate part of the software system, developers in the system development, should not be considered log processing. AOP does this by allowing developers to focus more on the business code of the system without worrying about log issues (which you can contact with declarative transactions, configure the configuration file to process the transaction, write the manager or action without worrying about the transaction).


then, introduce the code implementation in the example.

1, the introduction of JAR package:



2, section aspect Code:

[Java]  View plain copy print? package com.lzq.spring.aop;      import org.apache.log4j.logger;   Import  org.aspectj.lang.JoinPoint;   import org.aspectj.lang.proceedingjoinpoint;   import com.lzq.spring.test.aoptest;  /**   *  slicing    *  @author   lzq   *   */   public class aspect {               logger logger = logger.getlogger (Aoptest.class);        String strLog = null ;               /**       *  Forward notifications: notifications to be executed before a connection point, However, this notification does not prevent execution        *  @param  jp  connection points before the connection point: an action in the execution of a program, for example, Aserviceimpl.bara () Call or thrown exception behavior        */       public void dobefore (JOINPOINT JP)  {            strLog =  "log begining method: "                     + jp.gettarget (). GetClass (). GetName ()  +  "."                    +  jp.getsignature (). GetName ();           logger.warn ( Strlog);       }       /**        *  Surround Notification: A notification that surrounds a connection point that can complete a custom behavior before and after the invocation of a method, or you can choose not to execute        *  A Dofilter method for filter in a web-like servlet specification.        *  @param  pjp  connection points in the current process        *   @return        *  @throws  throwable       */       public Object  Doaround (PROCEEDINGJOINPOINT PJP)  throws Throwable {            long time = system.currenttimemillis ();            object retval = pjp.proceed ();            time = system.currenttimemillis ()  - time;            system.out.println ("process time: "  + time  +  " ms");           return retVal;       }       /**       *  Notification  :  execution When a method throws an exception exit after an exception is thrown.        *  @param  jp  connection point: an action in the execution of a program, for example, AserviceImpl.bara () Call or thrown exception behavior        */       public  Void doafter (JOINPOINT JP)  {           strlog  = "doafter:log ending method: "   

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.