log4j log for Tangent plane records

Source: Internet
Author: User

First, AOP is required to process logs, and there are many inconveniences in not using AOP to process logs.

Log processing is a very important part of every project. Without the log, you lose control of the system. There is no log, the system has any problems, there will be no trace to find, which is very dangerous for an information system.

However, using pure OOP to log processing, we will find that every logical part is always mixed with log processing code, resulting in the design of pure oop is slightly nondescript. At the same time, if the type requirements for logging are changed, then we are going to modify the Java code in each logical unit. In addition, 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 consider the log processing. AOP can do this by allowing developers to focus more on the business code of the system, without worrying about log issues (this can be done by contacting declarative transactions, configuring a transaction-processing configuration file, and not having to worry about transactions when writing the manager or action).

Then, introduce the code implementation in the example.

1. Introduction of JAR Package:

2. Tangent aspect Code:

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;/** * Facets *@author LZQ * * * *PublicClassAspect { Logger Logger = Logger.getlogger (Aoptest.class);String Strlog =null;  /*** Pre-notification: A notification that is executed before a connection point, but this notification does not prevent execution before the connection point*@param JP Connection point: A behavior during program execution, such as a call to Aserviceimpl.bara () or a thrown exception behavior*/ Publicvoid Dobefore (Joinpoint JP) { Strlog ="Log begining Method:"   + Jp.gettarget (). GetClass (). GetName () +"."   + jp.getsignature (). GetName (); Logger.warn (Strlog);}/** * Surround Notification: Notifications that surround a connection point can be customized before and after the invocation of the method, or you can choose not to execute a Dofilter method that is similar to filter in the servlet specification in the Web. *@param PJP The connection point 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 After an exception is thrown: a notification that is executed when the method throws an unexpected exit. * @param JP Connection point: an action in the execution of a program, such as a call to Aserviceimpl.bara () or a thrown exception, */public  void Doafter (Joinpoint JP) { Strlog ="Doafter:log Ending Method:" + Jp.gettarget (). GetClass (). GetName () + "." + jp.getsignature (). GetName ( ); Logger.warn (Strlog); }}

3. Business code:

Business Logic Interface:

Package com.lzq.spring.service; /** * Interface Aservice */Void Cool (); 

Implementation of the business interface:

Package com.lzq.spring.service;/** * Interface Implementation *@author LZQ * * */public class Aserviceimpl implements aservice { @Override  Public void Cool () { System.out.println ("Wow, the landlord is very handsome!") "); } @Override public  void Cool (String name) { System.out.println ("Wow, landlord" + Name+", you are handsome ah! ");  }}< /c6> 

4, Test aoptest:

Package com.lzq.spring.test;Import Org.springframework.beans.factory.BeanFactory;Import Org.springframework.context.support.ClassPathXmlApplicationContext;import Com.lzq.spring.service.AService;  public class aoptest{public  static void Main (string[] args) { beanfactory factory = New Classpathxmlapplicationcontext ("Applicationcontext.xml");  aservice aservice = (aservice) factory.getbean ("Aservice");  Aservice.cool ();  Aservice.cool ("Spring bath Five Rivers");} }            

5. Spring configuration file Applicationcontext.xml:

<?xml version= "1.0" encoding= "UTF-8"?><Beansxmlns="Http://www.springframework.org/schema/beans" Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.0.xsd "   Default-autowire="AutoDetect" > <Aop:config><Aop:aspectId="Aspect"ref="Aspectbean" ><!--Configure all methods for all classes or interfaces under the Com.lzq.spring.service package--<Aop:pointcutId="Logservice"expression="Execution (* com.lzq.spring.service.*.* (..))"/><Aop:beforepointcut-ref="Logservice"Method="Dobefore"/><Aop:afterpointcut-ref="Logservice"method= "Doafter"/> <aop : Around pointcut-ref= "Logservice" method=< Span class= "value" > "Doaround"/> </aop:aspect>  </aop:config> <bean  Id= "Aspectbean" class= " Com.lzq.spring.aop.Aspect "/> <bean id= "Aservice" class= "Com.lzq.spring.service.AServiceImpl "/> </BEANS>         

6, Log4j's properties file main configuration:

# # # Direct log messages to stdout # # #log4j. appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.target=system.outlog4j.appender.stdout.layout= org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversionpattern=%d%c{1}:%m% Nlog4j.appender.file=org.apache.log4j.fileappenderlog4j.appender.file.file=d:/test_ log.loglog4j.appender.file.layout=org.apache.log4j.patternlayoutlog4j.appender.file.layout.conversionpattern=% D{absolute}%5p%c{1}:%l-%m%nlog4j.rootlogger=warn, stdout,filelog4j.logger.org.hibernate.type= Infolog4j.logger.org.hibernate.tool.hbm2ddl=debugcom.lzq.spring.service = Debug     

7. Display Result:

Console window

File Location:

Log file Contents:

log4j log for Tangent plane records

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.