我有一个带@Traceable
实现的自定义TraceAspect
注释。我们对要在方法调用之前和之后记录的方法使用@Traceable
注释。我们刚刚添加了通过value
中的@Traceable
属性指定要使用的日志级别的功能(旧版本始终只使用INFO
)。我有什么作品,但我想知道是否有办法让它更高效。具体来说,如果存在某种方面背景,则可以在应用启动时检查给定方法的value
@Traceable
设置。
@Traceable
注释:
@Documented
@Retention(RUNTIME)
@Target(METHOD)
public @interface Traceable {
Level value() default Level.INFO;
}
当前TraceAspect
impl:
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class TraceAspect {
@Around("@annotation(com.collaterate.logging.Traceable)")
public Object traceAround(ProceedingJoinPoint joinPoint) throws Throwable {
Traceable traceable = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(Traceable.class);
Logger classLog = LoggerFactory.getLogger(joinPoint.getSignature().getDeclaringType());
LoggingHelper loggingHelper = getLoggingHelper(traceable, classLog);
String methodName = joinPoint.getSignature().getName();
loggingHelper.log("{}() - started", methodName);
Object returnVal = joinPoint.proceed();
loggingHelper.log("{}() - ended", methodName);
return returnVal;
}
private LoggingHelper getLoggingHelper(Traceable traceable, Logger classLog) {
if (Level.INFO == traceable.value() || null == traceable.value()) {
// try to avoid the switch below... most of the time it will be INFO
return (format, args) -> classLog.info(format, args);
} else {
switch (traceable.value()) {
case ERROR :
return (format, args) -> classLog.error(format, args);
case WARN :
return (format, args) -> classLog.warn(format, args);
case DEBUG :
return (format, args) -> classLog.debug(format, args);
case TRACE :
return (format, args) -> classLog.trace(format, args);
default :
return (format, args) -> classLog.info(format, args);
}
}
}
@FunctionalInterface
interface LoggingHelper {
void log(String format, Object... args);
}
}
我唯一的另一个想法是创建多个注释(每个日志级别一个),然后TraceAspect
将为每个注释提供一个@Around
处理程序,我们避免在运行时进行反射/切换。我不喜欢这个是我们已经在多个项目中使用现有的@Traceable
注释遍布生产代码。我想保留1注释并允许它通过属性指定日志级别。
理论上,我想要做的事情应该是可能的,因为创建代理时应用程序启动时会有所有信息。每个带注释的方法都必须有某种上下文。
答案 0 :(得分:2)
我做了类似于指标的事情。您可以使用class and method pair
作为键维护日志注册表,并将LoggingHelper
维护为值。
如果类方法对在日志注册表中没有条目,请创建日志记录助手并将其存储在注册表中。第二次,你只需在注册表中查找。
注册表是一个自定义的Spring bean,应该自动装入你的方面。
以下是修改后的TraceAspect
。
@Component
@Aspect
public class TraceAspect {
private LogRegistry registry;
@Autowired
public TraceAspect(LogRegistry registry) {
this.registry = registry;
}
@Around("@annotation(com.collaterate.logging.Traceable)")
public Object traceAround(ProceedingJoinPoint joinPoint) throws Throwable {
String loggerName = joinPoint.getSignature()
.getDeclaringType().getCanonicalName() + "."
+ joinPoint.getSignature().getName();
LoggingHelper loggingHelper = registry.get(loggerName);
if (loggingHelper == null) {
Traceable traceable = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(Traceable.class);
Logger classLog = LoggerFactory.getLogger(joinPoint.getSignature().getDeclaringType());
loggingHelper = getLoggingHelper(traceable, classLog);
registry.put(loggerName, loggingHelper)
}
String methodName = joinPoint.getSignature().getName();
loggingHelper.log("{}() - started", methodName);
Object returnVal = joinPoint.proceed();
loggingHelper.log("{}() - ended", methodName);
return returnVal;
}
}