码迷,mamicode.com
首页 > 其他好文 > 详细

Log4j配置文件

时间:2019-09-06 15:50:48      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:work   str   thread   开发   nat   cat   aac   logger   context   

技术图片
1 ## 1.应用于控制台 
2 # Global logging configuration 开发时候建议使用 debug
3 log4j.rootLogger=INFO, CONSOLE
4 # Console output...
5 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
6 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
7 log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] - %m%n
View Code

日志类:

技术图片
 1 package cn.spring.log;
 2 
 3 import org.aopalliance.intercept.Joinpoint;
 4 import org.apache.log4j.Logger;
 5 import org.aspectj.lang.JoinPoint;
 6 import org.aspectj.lang.ProceedingJoinPoint;
 7 
 8 import java.util.Arrays;
 9 
10 /**
11  * 日志类,该类中的方法,需要时,调用
12  * 测试aop
13  */
14 public class Mylog {
15 
16     private Logger logger = Logger.getLogger(Mylog.class);
17 
18     /**
19      * 之前
20      * @param joinPoint
21      */
22     public void doBefore(JoinPoint joinPoint){
23         logger.info("方法执行前  调用  方法名为==》"+joinPoint.getSignature().getName());
24         logger.info("方法执行前  调用  参数名为==》"+ Arrays.toString(joinPoint.getArgs()));
25     }
26 
27     /**
28      * 之后
29      */
30     public void doAfter(JoinPoint joinPoint){
31         logger.info("方法执行后   调用,无论是否发生异常===");
32     }
33 
34     /**
35      * 返回时
36      * @param joinPoint
37      */
38     public void doReturn(JoinPoint joinPoint){
39         logger.info("返回值时   调用,如果有异常,则该方法不会执行");
40     }
41 
42     /**
43      * 抛出异常时
44      */
45     public void doThrow(JoinPoint joinPoint){
46         logger.info("有异常时  调用");
47     }
48 
49     /**
50      * 环绕增强
51      * @param joinPoint
52      * @return
53      */
54     public Object doAround(ProceedingJoinPoint joinPoint){
55         long b = System.currentTimeMillis();//开始时间
56         Object proceed=null;
57         try {
58             proceed = joinPoint.proceed();//返回的结果
59         } catch (Throwable throwable) {
60             throwable.printStackTrace();
61         }
62         long e = System.currentTimeMillis();
63         logger.info("环绕增强,,,,总计小耗时间为"+(e-b)+"毫秒,方法的返回值为:"+proceed);
64         return proceed;
65     }
66 }
View Code

spring核心配置文件:

技术图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xsi:schemaLocation="
 8          http://www.springframework.org/schema/beans
 9         http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd
12         http://www.springframework.org/schema/aop
13         http://www.springframework.org/schema/aop/spring-aop.xsd
14         http://www.springframework.org/schema/tx
15         http://www.springframework.org/schema/tx/spring-tx.xsd">
16 
17 <!--找到需要的数据驱动配置文件-->
18     <context:property-placeholder location="classpath:mysql.properties"></context:property-placeholder>
19 
20     <!--找到spring需要创建的bean所在的包-->
21     <context:component-scan base-package="cn.spring.aop"></context:component-scan>
22     <context:component-scan base-package="cn.spring.dao"></context:component-scan>
23     <context:component-scan base-package="cn.spring.service"></context:component-scan>
24 
25     <!--c3p0数据源-->
26 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
27     <property name="password" value="${jdbc-password}"></property>
28     <property name="user" value="${jdbc-user}"></property>
29     <property name="jdbcUrl" value="${jdbc-url}"></property>
30     <property name="driverClass" value="${jdbc-driver}"></property>
31 </bean>
32 <!--QueryRunner对象-->
33     <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
34         <constructor-arg index="0" ref="dataSource"></constructor-arg>
35     </bean>
36 
37 
38     <!--配置通知类,需要调用的方法所在的类-->
39     <bean id="aspectLog" class="cn.spring.log.Mylog"/>
40     <!--配置切面aop-->
41     <aop:config>
42         <!--切入点表达式-->
43         <aop:pointcut id="pointcut" expression="execution(* cn.spring.service.*.*(..))"/>
44         <!--配置通知的类型-->
45         <aop:aspect id="aspect" ref="aspectLog">
46             <!--配置前置通知:调用方法doBefore方法,          测试ok-->
47             <!--<aop:before method="doBefore" pointcut-ref="pointcut"></aop:before>-->
48 
49             <!--后置增强增强,无论是否发生异常都会执行      -->
50             <aop:after method="doAfter" pointcut-ref="pointcut"></aop:after>
51 
52             <!--环绕增强,无论是否发生异常都会执行            测试ok-->
53             <aop:around method="doAround" pointcut-ref="pointcut"></aop:around>
54 
55             <!--方法异常增强,出现异常执行     测试ok-->
56             <!--<aop:after-throwing method="doThrow" pointcut-ref="pointcut"></aop:after-throwing>-->
57 
58             <!--方法返回后增强,但是如果出现异常,就不会执行     测试ok-->
59             <!--<aop:after-returning method="doReturn" pointcut-ref="pointcut"></aop:after-returning>-->
60         </aop:aspect>
61     </aop:config>
62 </beans>
View Code

POM文件:

技术图片
  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5     <modelVersion>4.0.0</modelVersion>
  6 
  7     <groupId>cn.spring</groupId>
  8     <artifactId>spring2</artifactId>
  9     <version>1.0-SNAPSHOT</version>
 10     <packaging>war</packaging>
 11 
 12     <name>spring2 Maven Webapp</name>
 13     <!-- FIXME change it to the project‘s website -->
 14     <url>http://www.example.com</url>
 15 
 16     <properties>
 17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 18         <maven.compiler.source>1.8</maven.compiler.source>
 19         <maven.compiler.target>1.8</maven.compiler.target>
 20         <spring-version>4.3.9.RELEASE</spring-version>
 21     </properties>
 22 
 23     <dependencies>
 24         <dependency>
 25             <groupId>junit</groupId>
 26             <artifactId>junit</artifactId>
 27             <version>4.12</version>
 28             <!--<scope>test</scope>-->
 29         </dependency>
 30 
 31         <!--mysql驱动依赖;-->
 32         <dependency>
 33             <groupId>mysql</groupId>
 34             <artifactId>mysql-connector-java</artifactId>
 35             <version>5.1.40</version>
 36         </dependency>
 37         <!--c3p0相关的依赖;-->
 38         <dependency>
 39             <groupId>commons-dbutils</groupId>
 40             <artifactId>commons-dbutils</artifactId>
 41             <version>1.4</version>
 42         </dependency>
 43         <dependency>
 44             <groupId>org.apache.commons</groupId>
 45             <artifactId>commons-pool2</artifactId>
 46             <version>2.3</version>
 47         </dependency>
 48         <dependency>
 49             <groupId>c3p0</groupId>
 50             <artifactId>c3p0</artifactId>
 51             <version>0.9.1.2</version>
 52         </dependency>
 53 
 54         <!--fastjson依赖;-->
 55         <dependency>
 56             <groupId>com.alibaba</groupId>
 57             <artifactId>fastjson</artifactId>
 58             <version>1.2.50</version>
 59         </dependency>      <!--jsp,servlet相关依赖;-->
 60         <dependency>
 61             <groupId>javax.servlet</groupId>
 62             <artifactId>javax.servlet-api</artifactId>
 63             <version>3.1.0</version>
 64             <scope>provided</scope>
 65         </dependency>
 66 
 67         <!--jstl、standard依赖;-->
 68         <dependency>
 69             <groupId>taglibs</groupId>
 70             <artifactId>standard</artifactId>
 71             <version>1.1.2</version>
 72         </dependency>
 73         <dependency>
 74             <groupId>javax.servlet</groupId>
 75             <artifactId>jstl</artifactId>
 76             <version>1.2</version>
 77         </dependency>
 78 
 79         <!--spring框架依赖包-->
 80         <dependency>
 81             <groupId>org.springframework</groupId>
 82             <artifactId>spring-core</artifactId>
 83             <version>${spring-version}</version>
 84         </dependency>
 85 
 86         <dependency>
 87             <groupId>org.springframework</groupId>
 88             <artifactId>spring-beans</artifactId>
 89             <version>${spring-version}</version>
 90         </dependency>
 91         <dependency>
 92             <groupId>org.springframework</groupId>
 93             <artifactId>spring-context</artifactId>
 94             <version>${spring-version}</version>
 95         </dependency>
 96         <dependency>
 97             <groupId>org.springframework</groupId>
 98             <artifactId>spring-expression</artifactId>
 99             <version>${spring-version}</version>
100         </dependency>
101         <dependency>
102             <groupId>org.springframework</groupId>
103             <artifactId>spring-test</artifactId>
104             <version>${spring-version}</version>
105         </dependency>
106         <dependency>
107             <groupId>org.springframework</groupId>
108             <artifactId>spring-web</artifactId>
109             <version>${spring-version}</version>
110         </dependency>
111 
112         <!--aop依赖-->
113         <dependency>
114             <groupId>org.springframework</groupId>
115             <artifactId>spring-aop</artifactId>
116             <version>${spring-version}</version>
117         </dependency>
118         <dependency>
119             <groupId>org.springframework</groupId>
120             <artifactId>spring-tx</artifactId>
121             <version>${spring-version}</version>
122         </dependency>
123         <!-- 此包的版本非常重要!否则会报错! -->
124         <dependency>
125             <groupId>org.aspectj</groupId>
126             <artifactId>aspectjweaver</artifactId>
127             <version>1.6.8</version>
128         </dependency>
129         <dependency>
130             <groupId>aopalliance</groupId>
131             <artifactId>aopalliance</artifactId>
132             <version>1.0</version>
133         </dependency>
134 
135         <!-- https://mvnrepository.com/artifact/log4j/log4j -->
136         <dependency>
137             <groupId>log4j</groupId>
138             <artifactId>log4j</artifactId>
139             <version>1.2.17</version>
140         </dependency>
141         <!--引入cglib包-->
142         <dependency>
143             <groupId>cglib</groupId>
144             <artifactId>cglib</artifactId>
145             <version>2.1_3</version>
146         </dependency>
147 
148 
149     </dependencies>
150 
151     <build>
152         <finalName>spring2</finalName>
153         <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
154             <plugins>
155                 <plugin>
156                     <artifactId>maven-clean-plugin</artifactId>
157                     <version>3.1.0</version>
158                 </plugin>
159                 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
160                 <plugin>
161                     <artifactId>maven-resources-plugin</artifactId>
162                     <version>3.0.2</version>
163                 </plugin>
164                 <plugin>
165                     <artifactId>maven-compiler-plugin</artifactId>
166                     <version>3.8.0</version>
167                 </plugin>
168                 <plugin>
169                     <artifactId>maven-surefire-plugin</artifactId>
170                     <version>2.22.1</version>
171                 </plugin>
172                 <plugin>
173                     <artifactId>maven-war-plugin</artifactId>
174                     <version>3.2.2</version>
175                 </plugin>
176                 <plugin>
177                     <artifactId>maven-install-plugin</artifactId>
178                     <version>2.5.2</version>
179                 </plugin>
180                 <plugin>
181                     <artifactId>maven-deploy-plugin</artifactId>
182                     <version>2.8.2</version>
183                 </plugin>
184             </plugins>
185         </pluginManagement>
186     </build>
187 </project>
View Code

被代理的类

技术图片
 1 package cn.spring.service;
 2 
 3 import org.springframework.stereotype.Service;
 4 
 5 /**
 6  * 数据操作
 7  */
 8 @Service
 9 public class ModelMath implements IModelMath{
10 
11     /**
12      * 加
13      * @param a
14      * @param b
15      * @return
16      */
17     public int addition( int a , int b ){
18         try {
19             Thread.sleep(20);
20         } catch (InterruptedException e) {
21             e.printStackTrace();
22         }
23        int num = 1/0;
24         return a+b;
25     }
26 
27     /**
28      * 减
29      * @param a
30      * @param b
31      * @return
32      */
33     public int subtraction( int a , int b ){
34         try {
35             Thread.sleep(40);
36         } catch (InterruptedException e) {
37             e.printStackTrace();
38         }
39         return a-b;
40     }
41 }
View Code

 

Log4j配置文件

标签:work   str   thread   开发   nat   cat   aac   logger   context   

原文地址:https://www.cnblogs.com/in-the-game-of-thrones/p/11475025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!