<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
dependency>
- @interface 注解类,自定义注解
- @Target 用来说明该注解可以被声明在那些元素之前
- ElementType.METHOD说明该注解只能被声明在一个类的方法前
- @Retention 用来说明该注解类的生命周期。
- RetentionPolicy.RUNTIME注解保留在程序运行期间,此时可以通过反射获得定义在某个类上的所有注解
/**
* 自定义注解
* 用于在Controller层使用注解标注哪个方法需要增加日志
*/
@Target(ElementType.METHOD) //在方法加载Log
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
String value() default "";
}
@RestController
@RequestMapping("/index")
public class IndexController {
/*一般将这个日志放到Controller,因为要记录方法的执行时间。
也可以记录service,但是最好Controller*/
@Log("这是Index的修改方法")
@GetMapping("/updateIndex")
public int updateIndex(){
System.out.println("这是修改");
return 10;
}
}
@Aspect
@Component
public class LogAspect {
//切入点,指定当使用Log注解时进入环绕增强
@Pointcut("@annotation(com.wy.log.Log)")
public void pointcut() {}
/**
* 环绕增强
* @param point
* @return
*/
@Around("pointcut()")
public Object around(ProceedingJoinPoint point) {
try {
System.out.println("执行环绕增强..............开始");
Object result = point.proceed(); //调用目标方法:point.proceed()
System.out.println("执行环绕增强..............结束");
return result; //与Controller的返回值一致(没什么用)
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
}
执行:http://localhost:8080/index/updateIndex
结果:
说明:调用目标的方法的输出结果没什么大用,就是Controller的返回值
使用mybatis的话,要导入mybatis的核心包,和sql驱动,配置文件
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.3version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.44version>
dependency>
spring:
datasource:
username: zzzz
password: zzzz
url: jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
DROP TABLE IF EXISTS `sys_log`;
CREATE TABLE `sys_log` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`USERNAME` varchar(50) DEFAULT NULL COMMENT '用户名',
`OPERATION` varchar(50) DEFAULT NULL COMMENT '用户操作',
`TIME` int(11) DEFAULT NULL COMMENT '响应时间',
`METHOD` varchar(200) DEFAULT NULL COMMENT '请求方法',
`PARAMS` varchar(500) DEFAULT NULL COMMENT '请求参数',
`IP` varchar(64) DEFAULT NULL COMMENT 'IP地址',
`CREATE_TIME` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
@Data
public class SysLog {
private Long id;
private String username;
private String operation;
private Integer time;
private String method;
private String params;
private String ip;
private String createTime;
}
@Repository
public interface SysLogDao {
@Insert("insert into sys_log(USERNAME,OPERATION,TIME,METHOD,PARAMS,IP) " +
"values(#{username},#{operation},#{time},#{method},#{params},#{ip})")
public int insertSysLog(SysLog sysLog);
}
@Aspect
@Component
public class LogAspect {
@Autowired
private SysLogDao sysLogDao;
//切入点,指定当使用Log注解时进入环绕增强
@Pointcut("@annotation(com.wy.log.Log)")
public void pointcut() {}
/**
* 环绕增强
* @param point
* @return
*/
@Around("pointcut()")
public Object around(ProceedingJoinPoint point) {
try {
//1、获取时间
Long beginTime = System.currentTimeMillis(); //调取方法前开始计时(毫秒)
Object res = point.proceed();//调用目标方法:point.proceed()
Long endTime = System.currentTimeMillis(); //调取方法前开始计时(毫秒)
Long time = endTime-beginTime;
//2、获取本地IP:工具类直接使用
//从request中解析出ip地址
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
//3、拿到方法上的描述:某个操作
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
Log logAnnotation = method.getAnnotation(Log.class);
//4、请求的方法名
String className = point.getTarget().getClass().getName();
String methodName = signature.getName();
//5、请求的参数
// 请求的方法参数值
Object[] args = point.getArgs();
// 请求的方法参数名称
String params = "";
LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
String[] paramNames = u.getParameterNames(method);
if (args != null && paramNames != null) {
for (int i = 0; i < args.length; i++) {
params += " " + paramNames[i] + ": " + args[i];
}
/*sysLog.setParams(params);*/
}
SysLog sysLog = new SysLog();
sysLog.setUsername("朱尔斯"); //一般是从session或request中拿取
sysLog.setTime(Integer.parseInt(time.toString()));
sysLog.setIp(IPUtils.getIpAddr(request));
sysLog.setOperation(logAnnotation.value());
sysLog.setMethod(className + "." + methodName + "()");
sysLog.setParams(params);
sysLogDao.insertSysLog(sysLog);
return res;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
}
package com.hz.utils;
import javax.servlet.http.HttpServletRequest;
public class IPUtils {
/**
* 获取IP地址
* 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址
*/
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
}
}
package com.hz.utils;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class HttpContextUtils {
public static HttpServletRequest getHttpServletRequest() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
}
【之前有说过log4j的处理方式:https://blog.csdn.net/m0_70083523/article/details/127498052
】
关于logback的日志配置网上就能下载到【此文件私发】
<configuration scan="true" scanPeriod="10 seconds">
<contextName>logbackcontextName>
<property name="log.path" value="D:/nmyslog/nmys" />
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>infolevel>
filter>
<encoder>
<Pattern>${CONSOLE_LOG_PATTERN}Pattern>
<charset>UTF-8charset>
encoder>
appender>
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/log_debug.logfile>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>15maxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>debuglevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/log_info.logfile>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>15maxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>infolevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/log_warn.logfile>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>15maxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>warnlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/log_error.logfile>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>15maxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<springProfile name="dev">
<logger name="com.nmys.view" level="debug"/>
springProfile>
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
root>
configuration>
#读取配置文件
logging.config=classpath:logback-spring.xml
#上面文件中也有路径的配置,如果上面没有,则可以在此处配置
#日志打印文件所在目录
logging.path=D:/项目名/log
//这个类要是当前Controller类
Logger log = Logger.getLogger(SfUserController.class);
log.info("要输出的内容");//输出信息
-------------------------------------------------
等级可分为OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL
如果配置OFF则不打出任何信息,
如果配置为INFO这样只显示INFO, WARN, ERROR的log信息,而DEBUG信息不会被显示