日志打印是一个项目中必不可少的部分,没有日志打印,我们的代码的可维护性将会非常的差,为了能让我们的代码便于自己调试也便于别人维护,所以在项目中必须要打印日志,所以此文要解决的问题就是在spring项目中如何配置log日志。
1.首先目录结构是这样的:
注:红色标注的部分为运行项目后实际生成的log文件
2.pom文件是这样的
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zlb.springaop</groupId>
<artifactId>spring-aop</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--spring核心依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<!--sping aop 所必须依赖的jar包-->
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aspectj/aspectjrt -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
注意:
aopalliance依赖一定不能少,如果少了就会报如下异常
org.springframework.beans.factory.BeanCreationExcspring-aopeption: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception isJava.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
3.log4j.xml文件这样写
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p][%d{ISO8601}][%t][%c][%m]%n" />
</layout>
</appender>
<!--class定义日志记录器-->
<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
<!--相对于应用服务器的输出路径,应用服务器的路径指定在common包中指定-->
<param name="File" value="springaop/log/debug/app-debug.log" />
<!--是否追加写入文件 true-是 false-否,此部分不建议改动-->
<param name="Append" value="true" />
<!--当日志文件超过定义数值则自动备分一个新文件-->
<!--可以定义为1MB,1KB,1GB这样的类型,也可以定义为1000则表示1000字节-->
<param name="MaxFileSize" value="30MB" />
<!--备分文件最多的数量-->
<param name="MaxBackupIndex" value="30" />
<!--输出的日志文件格式,根据操作系统编码指定-->
<param name="Encoding" value="UTF8" />
<!--log4j的打印日志布局,可参考相关log4j文档设置-->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p][%d{ISO8601}][%t][%c][%m]%n" />
</layout>
<!--日志级别过滤器-->
<!--LevelMin表示最小的级别,LevelMax表示最大的级别,建议不做改动-->
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<appender name="INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="springaop/log/debug/app-debug.log" />
<!--<param name="File" value="D:/Program Files/log/info/app-info.log" />-->
<param name="Append" value="true" />
<param name="MaxFileSize" value="30MB" />
<param name="MaxBackupIndex" value="30" />
<param name="Encoding" value="UTF8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p][%d{ISO8601}][%t][%c][%m]%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<appender name="WARN" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="springaop/log/debug/app-debug.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="30MB" />
<param name="MaxBackupIndex" value="30" />
<param name="Encoding" value="UTF8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p][%d{ISO8601}][%t][%c][%m]%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="springaop/log/debug/app-debug.log" />
<!--<param name="File" value="D:/Program Files/log/error/app-error.log" />-->
<param name="Append" value="true" />
<param name="MaxFileSize" value="30MB" />
<param name="MaxBackupIndex" value="30" />
<param name="Encoding" value="UTF8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p][%d{ISO8601}][%t][%c][%m]%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<logger name="mapper/userMapper.xml">
<level value="DEBUG"></level>
<appender-ref ref="STDOUT"></appender-ref>
</logger>
<root>
<!--定义日志的最低输出级别-->
<priority value="INFO" />
<appender-ref ref="STDOUT" />
<appender-ref ref="DEBUG" />
<appender-ref ref="INFO" />
<appender-ref ref="WARN" />
<appender-ref ref="ERROR" />
</root>
</log4j:configuration>
注意:<param name="File" value="springaop/log/debug/app-debug.log" />
这就是你想要log文件写出的地址!
4.java类这样写
(1)LogAspect
package com.zlb.spring.aop;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
@Aspect
@Order(1)
public class LogAspect {
private org.apache.log4j.Logger logger = Logger.getLogger(this.getClass());
@Pointcut("execution(public * com.zlb.spring.aop.*.*(..))")
public void pointCut(){}
@Before("pointCut()")
public void doBefore(JoinPoint joinPoint){
logger.info("class_method : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()+" start");
logger.info("args : " + Arrays.toString(joinPoint.getArgs()));
}
@After("pointCut()")
public void doAfter(JoinPoint joinPoint){
logger.info("class_method : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()+" end");
}
@AfterReturning(returning ="result",value = "pointCut()")
public void doAfterReturning(Object result){
logger.info("returned"+result);
}
@AfterThrowing(value = "pointCut()",throwing = "exception")
public void doAfterThrowing(Exception exception){
logger.info("method exception"+exception.getMessage());
}
}
注意:
@AfterReturning与@AfterThrowing所标记的方法,参数位置不能改变!
(2)LogConfig这样写
package com.zlb.spring.aop;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@ComponentScan(value = "com.zlb.spring.aop")
@Configuration
@EnableAspectJAutoProxy
public class LogConfig {
}
注意:如果用spring注解方式开发项目,一定要使用@EnableAspectJAutoProxy注解,不然没法实现aop
(3)被测试的目标方法类这样写
package com.zlb.spring.aop;
import org.springframework.stereotype.Component;
@Component
public class MathCalculator {
public int getDiv(int x,int y){
return x/y;
}
}
(4)测试类这样写
package com.zlb.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestAop {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(LogConfig.class);
System.out.println("ioc 容器舒适化完成!");
//打印logger日志
MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
mathCalculator.getDiv(6,0);
}
}
运行结果长这样:
ioc 容器舒适化完成!
[INFO][2019-06-27 19:09:15,758][main][com.zlb.spring.aop.LogAspect][class_method : com.zlb.spring.aop.MathCalculator.getDiv start]
[INFO][2019-06-27 19:09:15,759][main][com.zlb.spring.aop.LogAspect][args : [6, 1]]
[INFO][2019-06-27 19:09:15,787][main][com.zlb.spring.aop.LogAspect][class_method : com.zlb.spring.aop.MathCalculator.getDiv end]
[INFO][2019-06-27 19:09:15,787][main][com.zlb.spring.aop.LogAspect][returned6]
Process finished with exit code 0
思考:如果 MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
改成 MathCalculator mathCalculator = new MathCalculator()还能实现日志打印吗?
答:这样是不能的,因为切面日志只能管理spring容器中的bean,而不能管理我们手动创建的bean。