@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface GrpcRecord {
/**
* 是否记录请求参数
*/
boolean recordReq() default true;
/**
* 是否记录请求结果
*/
boolean recordResp() default false;
}
@Slf4j
@Aspect
@Component
public class GrpcRecordAspect {
@Around("@annotation(grpcRecord)")
public Object process(ProceedingJoinPoint pjp, GrpcRecord grpcRecord) throws Throwable {
String traceId = StringUtils.replace(UUID.randomUUID().toString(), "-", "");
try (MDC.MDCCloseable ignored = MDC.putCloseable(MDC_TRACE_ID, traceId)) {
long start = System.currentTimeMillis();
Object obj = pjp.proceed();
StringBuilder sb = new StringBuilder(pjp.getSignature().toShortString());
if (grpcRecord.recordReq()) {
sb.append(" param:").append(JSON.toJSONString(pjp.getArgs()));
}
if (grpcRecord.recordResp()) {
sb.append(" result:").append(JSON.toJSONString(obj));
}
sb.append(" cost:").append(System.currentTimeMillis() - start);
log.info(sb.toString());
return obj;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<!-- 增加一个jvm关闭时的hook,释放logback的线程和资源,jvm关闭时延迟3s关闭logback context -->
<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook">
<delay>3000</delay>
</shutdownHook>
<property name="LOG_PATH" value="${user.home}/logs"/>
<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/application.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>%X{trace:--} ${FILE_LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_PATH}/application.%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>2GB</maxFileSize>
</triggeringPolicy>
</appender>
<appender name="async_infoAppender" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="infoAppender"/>
<discardingThreshold>0</discardingThreshold>
<queueSize>2048</queueSize>
<maxFlushTime>2500</maxFlushTime>
</appender>
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/error.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>%X{trace:--} ${FILE_LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOG_PATH}/error.%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>2GB</maxFileSize>
</triggeringPolicy>
</appender>
<appender name="async_errorAppender" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="errorAppender"/>
<discardingThreshold>0</discardingThreshold>
<queueSize>2048</queueSize>
<maxFlushTime>2500</maxFlushTime>
</appender>
<root level="INFO">
<appender-ref ref="async_infoAppender"/>
<appender-ref ref="async_errorAppender"/>
</root>
</configuration>