Log4j logs and log4j logs

Source: Internet
Author: User

Log4j logs and log4j logs

First, AOP is required to process logs. it is inconvenient to process logs without using AOP.

Log processing is a very important part of each project. Without logs, the controllability of the system is lost. Without logs, there will be no trace of any problems in the system, which is very dangerous for an information system.

However, when we use the pure OOP idea for log processing, we will find that every logic part will always be mixed into the log processing code, resulting in the design of the pure OOP idea is a bit nondescribable. At the same time, if the logging type needs to be changed, we need to modify the Java code in each logical unit. In addition, if the demand changes today and tomorrow, I think this will be a very heavy and annoying job.

In fact, log processing should be a separate part of the software system. Developers should not consider log processing when developing the system. AOP enables developers to focus more on System Business coding without worrying about log issues. (You can contact declarative transaction processing, after configuring the configuration file for transaction processing, you do not need to worry about transactions when writing managers or actions ).

Next, we will introduce the code implementation in the example.

1. Import the jar package:

2. 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;/*** cut ** @ author lzq **/public class Aspect {Logger logger = Logger. getLogger (AOPTest. class); String strLog = null;/*** pre-notification: A notification executed before a connection point, but this notification cannot prevent execution before the connection point * @ param jp connection point: A line in the program execution process is, for example, AServiceImpl. callback () call or thrown abnormal behavior */p Ublic void doBefore (JoinPoint jp) {strLog = "log Begining method:" + jp. getTarget (). getClass (). getName () + ". "+ jp. getSignature (). getName (); logger. warn (strLog);}/*** surround notification: notifications that enclose a connection point. You can complete custom actions before and after a method call, you can also choose not to execute the * doFilter method similar to the Filter in the Servlet specification in the Web. * @ Param pjp 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: Notification executed when the method throws an exception and exits. * @ Param jp connection point: a line in the program execution process is, for example, AServiceImpl. invoke () or thrown abnormal behavior */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 */public interface AService {public void cool (String _ msg); public void cool ();}

 

Business interface implementation:

Package com. lzq. spring. service;/*** interface implementation * @ author lzq **/public class AServiceImpl implements AService {@ Override public void cool () {System. out. println ("wow, handsome landlord! ") ;}@ Override public void cool (String name) {System. out. println (" wow, "+ name +", so handsome! ");}}

 

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 Wujiang ");}}

 

5. Spring configuration file ApplicationContext. xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "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" d Efault-autowire = "autodetect"> <aop: config> <aop: aspect id = "aspect" ref = "aspectBean"> <! -- Configure com. lzq. spring. all methods of all classes or interfaces under the service package --> <aop: pointcut id = "logService" expression = "execution (* com. lzq. spring. service. *. *(..)) "/> <aop: before pointcut-ref =" logService "method =" doBefore "/> <aop: after pointcut-ref = "logService" method = "doAfter"/> <aop: around pointcut-ref = "logService" method = "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. Main configuration of the log4j properties file:

### 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{ABSOLUTE} %5p %c{1}:%L - %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 Results:

Console window

File Location:

Log File Content:

 

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.