文章目录
1.common-log4j2-starter 1.目录 2.pom.xml 引入依赖 3.LogAspect.java 4.Log4j2AutoConfiguration.java Log4j2自动配置类条件注入切面类
2.common-log4j2-starter-demo 测试 1.目录 2.application.yml 启用日志切面 3.TraceController.java 4.结果
1.common-log4j2-starter
1.目录
2.pom.xml 引入依赖
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-aop</ artifactId>
</ dependency>
< dependency>
< groupId> com.google.code.gson</ groupId>
< artifactId> gson</ artifactId>
</ dependency>
3.LogAspect.java
package com. sunxiansheng. log4j2. aspectj ;
import com. google. gson. Gson ;
import com. google. gson. GsonBuilder ;
import lombok. extern. slf4j. Slf4j ;
import org. aspectj. lang. ProceedingJoinPoint ;
import org. aspectj. lang. annotation. * ;
import org. aspectj. lang. reflect. MethodSignature ;
@Aspect
@Slf4j
public class LogAspect {
private static final Gson GSON = new GsonBuilder ( )
. setPrettyPrinting ( )
. disableHtmlEscaping ( )
. create ( ) ;
private static final String ANSI_RESET = "\u001B[0m" ;
private static final String ANSI_CYAN = "\u001B[92m" ;
@Pointcut ( "execution(* com.sunxiansheng..controller..*(..)) || execution(* com.sunxiansheng..service..*(..))" )
public void applicationPackagePointcut ( ) {
}
@Around ( "applicationPackagePointcut()" )
public Object logAround ( ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = ( MethodSignature ) joinPoint. getSignature ( ) ;
String className = signature. getDeclaringTypeName ( ) ;
String methodName = signature. getName ( ) ;
Object [ ] args = joinPoint. getArgs ( ) ;
String requestParams = GSON . toJson ( args) ;
log. info ( "==> 进入方法: {}.{}()" , className, methodName) ;
log. info ( "参数:\n" + ANSI_CYAN + "{}" + ANSI_RESET , requestParams) ;
long startTime = System . currentTimeMillis ( ) ;
Object result;
try {
result = joinPoint. proceed ( ) ;
} catch ( Throwable throwable) {
String exceptionStackTrace = getStackTraceAsString ( throwable) ;
log. error ( "<== 方法异常: {}.{}() | 异常信息: {}" , className, methodName, throwable. getMessage ( ) ) ;
log. error ( "堆栈信息:\n{}" , exceptionStackTrace) ;
throw throwable;
}
long endTime = System . currentTimeMillis ( ) ;
long executionTime = endTime - startTime;
String response = GSON . toJson ( result) ;
log. info ( "<== 退出方法: {}.{}() | 耗时: {} ms" , className, methodName, executionTime) ;
log. info ( "返回值:\n" + ANSI_CYAN + "{}" + ANSI_RESET , response) ;
return result;
}
private String getStackTraceAsString ( Throwable throwable) {
StringBuilder sb = new StringBuilder ( ) ;
for ( StackTraceElement element : throwable. getStackTrace ( ) ) {
sb. append ( "\tat " ) . append ( element. toString ( ) ) . append ( "\n" ) ;
}
return sb. toString ( ) ;
}
}
4.Log4j2AutoConfiguration.java Log4j2自动配置类条件注入切面类
package com. sunxiansheng. log4j2. config ;
import com. sunxiansheng. log4j2. aspectj. LogAspect ;
import org. springframework. boot. autoconfigure. condition. ConditionalOnMissingBean ;
import org. springframework. boot. autoconfigure. condition. ConditionalOnProperty ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
@Configuration
public class Log4j2AutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty ( name = "log.aspect.enable" , havingValue = "true" , matchIfMissing = true )
LogAspect logAspect ( ) {
return new LogAspect ( ) ;
}
}
2.common-log4j2-starter-demo 测试
1.目录
2.application.yml 启用日志切面
log :
aspect :
enable : true
3.TraceController.java
@Data
static class AspectBeanIn {
private String name;
private Integer age;
private String phone;
}
@Data
static class AspectBeanOut {
private String name;
private Integer age;
private String phone;
}
@RequestMapping ( "/aspect" )
public AspectBeanOut aspect ( @RequestBody AspectBeanIn aspectBeanIn) {
AspectBeanOut aspectBean = new AspectBeanOut ( ) ;
aspectBean. setName ( "sun" ) ;
aspectBean. setAge ( 18 ) ;
aspectBean. setPhone ( "123456789" ) ;
int i = 1 / 0 ;
return aspectBean;
}
4.结果