您的当前位置:首页正文

Spring AOP --实现日志记录

2024-11-23 来源:个人技术集锦

log4j

log4j

1.2.17

二.编写 log4j 配置文件


配置根

log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE

设置输出sql的级别,其中logger后面的内容全部为jar包中所包含的包名

log4j.logger.org.apache=debug

log4j.logger.java.sql.Connection=debug

log4j.logger.java.sql.Statement=debug

log4j.logger.java.sql.PreparedStatement=debug

log4j.logger.java.sql.ResultSet=debug

配置输出到控制台

log4j.appender.console = org.apache.log4j.ConsoleAppender

log4j.appender.console.Target = System.out

log4j.appender.console.layout = org.apache.log4j.PatternLayout

log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n

配置输出到文件

log4j.appender.fileAppender = org.apache.log4j.FileAppender

log4j.appender.fileAppender.File = logs/log.log

log4j.appender.fileAppender.Append = true

log4j.appender.fileAppender.Threshold = DEBUG

log4j.appender.fileAppender.layout = org.apache.log4j.PatternLayout

log4j.appender.fileAppender.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

配置输出到文件,并且每天都创建一个文件

log4j.appender.dailyRollingFile = org.apache.log4j.DailyRollingFileAppender

log4j.appender.dailyRollingFile.File = logs/log.log

log4j.appender.dailyRollingFile.Append = true

log4j.appender.dailyRollingFile.Threshold = DEBUG

log4j.appender.dailyRollingFile.layout = org.apache.log4j.PatternLayout

log4j.appender.dailyRollingFile.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

配置输出到文件,且大小到达指定尺寸的时候产生一个新的文件

log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender

log4j.appender.ROLLING_FILE.Threshold=ERROR

log4j.appender.ROLLING_FILE.File=rolling.log

log4j.appender.ROLLING_FILE.Append=true

log4j.appender.ROLLING_FILE.MaxFileSize=10KB

log4j.appender.ROLLING_FILE.MaxBackupIndex=1

log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout

三.编写代码


(一)定义包含增强方法的类

Joint Point 简单说明

/**

  • @Classname ServiceLog

  • @Description 包含增强方法的JavaBean

  • @Date 2021/12/14 16:59

  • @Created by 闲言

*/

public class ServiceLog {

//获取log日志对象

private Logger logger = Logger.getLogger(ServiceLog.class);

/**

  • 前置增强的方法

  • @param joinPoint

*/

public void before(JoinPoint joinPoint){

logger.info(“前置方法执行,当前对象为:”+joinPoint.getTarget()+" 方法为:“+joinPoint.getSignature().getName()+” 参数为:"+ Arrays.toString(joinPoint.getArgs()));

}

/**

  • 后置增强的方法

  • @param joinPoint

*/

public void afterRound(JoinPoint joinPoint,Object result){

logger.info(“后置方法执行,当前对象为:”+joinPoint.getTarget()+" 方法为:“+joinPoint.getSignature().getName()+” 返回值为:"+result);

}

}

(二)定义要增强的方法

public class UserServiceImpl implements UserService {

@Override

public void add(User user) {

System.out.println(“添加的用户为:”+user);

}

}

四.编写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:context=“http://www.springframework.org/schema/context”

xmlns:aop=“http://www.springframework.org/schema/aop”

xsi:schemaLocation="http://www.springframework.org/schema/beans

https://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

https://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop

https://www.springframework.org/schema/aop/spring-aop.xsd">

aop:config

<aop:pointcut id=“pt” expression=“execution( * cn.bloghut.service….(…))”/>

<aop:aspect ref=“logger”>

<aop:before method=“before” pointcut-ref=“pt”/>

<aop:after-returning method=“afterRound” pointcut-ref=“pt” returning=“result”/>

</aop:aspect>

</aop:config>

五.测试代码


显示全文