1、导入maven依赖
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aspectj/aspectjweaver -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
2、编写AOP类(自动扫描service下的impl类下所有方法,只要impl下的类任何一个方法执行,则自动记录日志)
@Aspect
@Component
public class LoggingAop {
Logger logger = LoggerFactory.getLogger(getClass()); //创建LoggerFactory工厂
/**
* 前置通知
* @param joinPoint
*/
@Before("execution(* com.kinglove.springbootlucky.service.impl.*.*(..))")
public void beforeLogging(JoinPoint joinPoint){
logger.info(" "); //为了美观,为了看起来更舒服,做了格式化
logger.info(" "); //为了美观,为了看起来更舒服,做了格式化
logger.info(" "); //为了美观,为了看起来更舒服,做了格式化
logger.info("---------------------------------------START--------------------------------------------"); //为了美观,为了看起来更舒服,做了格式化
logger.trace("前置通知:方法‘"+joinPoint+"’执行了!");
logger.info("----------------------------------------END---------------------------------------------"); //为了美观,为了看起来更舒服,做了格式化
logger.info(" "); //为了美观,为了看起来更舒服,做了格式化
}
/**
* 后置通知
* @param joinPoint
*/
@After("execution(* com.kinglove.springbootlucky.service.impl.*.*(..))")
public void afterLogging(JoinPoint joinPoint){
logger.info(" "); //为了美观,为了看起来更舒服,做了格式化
logger.info("---------------------------------------START--------------------------------------------"); //为了美观,为了看起来更舒服,做了格式化
logger.trace("后置通知:方法‘"+joinPoint+"’执行了!");
logger.info("----------------------------------------END---------------------------------------------"); //为了美观,为了看起来更舒服,做了格式化
logger.info(" "); //为了美观,为了看起来更舒服,做了格式化
}
/**
* 后置返回通知
* @param joinPoint
* @param object
*/
@AfterReturning(returning = "object", pointcut = "execution(* com.kinglove.springbootlucky.service.impl.*.*(..))")
public void afterReturningLogging(JoinPoint joinPoint,Object object){
logger.info(" "); //为了美观,为了看起来更舒服,做了格式化
logger.info("---------------------------------------START--------------------------------------------"); //为了美观,为了看起来更舒服,做了格式化
logger.trace("后置返回通知:方法‘"+joinPoint+"’执行了!");
logger.info("数据:"+object);
logger.info("----------------------------------------END---------------------------------------------"); //为了美观,为了看起来更舒服,做了格式化
logger.info(" "); //为了美观,为了看起来更舒服,做了格式化
}
/**
* 异常通知
* @param joinPoint
* @param e
*/
@AfterThrowing(value = "execution(* com.kinglove.springbootlucky.service.impl.*.*(..))" , throwing = "e")
public void afterThrowing(JoinPoint joinPoint,Exception e){
logger.info(" "); //为了美观,为了看起来更舒服,做了格式化
logger.info("---------------------------------------START--------------------------------------------"); //为了美观,为了看起来更舒服,做了格式化
logger.trace("异常通知:方法‘"+joinPoint+"’执行了!");
logger.error("出现异常:"+e);
logger.info("----------------------------------------END---------------------------------------------"); //为了美观,为了看起来更舒服,做了格式化
logger.info(" "); //为了美观,为了看起来更舒服,做了格式化
}
}
3、打印结果
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\\ //
|| _ ___ _ ||
// | |/ (_) | | \\
|| | ' / _ _ __ __ _ | | _____ _____ ||
\\ | < | | '_ \ / _` | | | / _ \ \ / / _ \ //
|| | . \| | | | | (_| | | |___| (_) \ V / __/ ||
// |_|\_\_|_| |_|\__, | |______\___/ \_/ \___| \\
|| __/ | ||
\\ |___/ //
|| ||
// 深圳市皇爱购商务有限公司 © 版权所有 \\
|| 如想使用本系统,请关注微信公众号‘皇爱购’ ||
\\ 30天内联系客服进行授权,否则后果自负 //
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2019-11-18 11:15:36.512 INFO 21072 --- [ main] c.k.s.SpringbootluckyApplication : Starting SpringbootluckyApplication on WIN-CKBRPH1ODJI with PID 21072 (E:\java\springbootlucky\target\classes started by Administrator in E:\java\springbootlucky)
2019-11-18 11:15:36.523 DEBUG 21072 --- [ main] c.k.s.SpringbootluckyApplication : Running with Spring Boot v2.1.10.RELEASE, Spring v5.1.11.RELEASE
2019-11-18 11:15:36.524 INFO 21072 --- [ main] c.k.s.SpringbootluckyApplication : No active profile set, falling back to default profiles: default
2019-11-18 11:15:37.883 INFO 21072 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8088 (http)
2019-11-18 11:15:37.910 INFO 21072 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-18 11:15:37.910 INFO 21072 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-18 11:15:38.004 INFO 21072 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-18 11:15:38.004 INFO 21072 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1422 ms
2019-11-18 11:15:38.712 INFO 21072 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-18 11:15:39.060 INFO 21072 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8088 (http) with context path ''
2019-11-18 11:15:39.062 INFO 21072 --- [ main] c.k.s.SpringbootluckyApplication : Started SpringbootluckyApplication in 2.948 seconds (JVM running for 3.392)
2019-11-18 11:15:44.306 INFO 21072 --- [nio-8088-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-11-18 11:15:44.307 INFO 21072 --- [nio-8088-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-11-18 11:15:44.328 INFO 21072 --- [nio-8088-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 21 ms
2019-11-18 11:15:44.354 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop :
2019-11-18 11:15:44.356 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop :
2019-11-18 11:15:44.357 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop :
2019-11-18 11:15:44.357 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop : ---------------------------------------START--------------------------------------------
2019-11-18 11:15:44.359 TRACE 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop : 前置通知:方法‘execution(Object com.kinglove.springbootlucky.service.impl.GavinProjectServiceImpl.select(Integer))’执行了!
2019-11-18 11:15:44.359 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop : ----------------------------------------END---------------------------------------------
2019-11-18 11:15:44.360 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop :
2019-11-18 11:15:44.408 INFO 21072 --- [nio-8088-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-11-18 11:15:45.060 INFO 21072 --- [nio-8088-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-11-18 11:15:45.068 DEBUG 21072 --- [nio-8088-exec-1] c.k.s.m.G.selectByExample : ==> Preparing: select id, name, user, add_time, is_show from gavin_project
2019-11-18 11:15:45.093 DEBUG 21072 --- [nio-8088-exec-1] c.k.s.m.G.selectByExample : ==> Parameters:
2019-11-18 11:15:45.160 TRACE 21072 --- [nio-8088-exec-1] c.k.s.m.G.selectByExample : <== Columns: id, name, user, add_time, is_show
2019-11-18 11:15:45.161 TRACE 21072 --- [nio-8088-exec-1] c.k.s.m.G.selectByExample : <== Row: 2, KL系统项目, gavin, 2019-10-30 15:00:04, 0
2019-11-18 11:15:45.162 DEBUG 21072 --- [nio-8088-exec-1] c.k.s.m.G.selectByExample : <== Total: 1
2019-11-18 11:15:45.164 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop :
2019-11-18 11:15:45.164 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop : ---------------------------------------START--------------------------------------------
2019-11-18 11:15:45.164 TRACE 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop : 后置通知:方法‘execution(Object com.kinglove.springbootlucky.service.impl.GavinProjectServiceImpl.select(Integer))’执行了!
2019-11-18 11:15:45.164 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop : ----------------------------------------END---------------------------------------------
2019-11-18 11:15:45.164 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop :
2019-11-18 11:15:45.164 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop :
2019-11-18 11:15:45.164 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop : ---------------------------------------START--------------------------------------------
2019-11-18 11:15:45.164 TRACE 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop : 后置返回通知:方法‘execution(Object com.kinglove.springbootlucky.service.impl.GavinProjectServiceImpl.select(Integer))’执行了!
2019-11-18 11:15:45.164 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop : 数据:[com.kinglove.springbootlucky.entity.GavinProject@11a4f6d]
2019-11-18 11:15:45.164 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop : ----------------------------------------END---------------------------------------------
2019-11-18 11:15:45.164 INFO 21072 --- [nio-8088-exec-1] c.k.springbootlucky.config.LoggingAop :