我有以下界面:
import requests
txt = {'file': open('/tmp/pysrv01-20151207-212735')}
post = requests.post('someaddr', files=txt)
我也有以下课程:
/**
* Annotation for methods, whose execution should be logged.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Loggable {
/**
* Log severity level of 'before' and 'after' log statements.
*/
enum Level {
DEBUG,
INFO,
WARN,
ERROR
}
/**
* Defines the severity which should be used when logging the method arguments.
*/
Level level() default Level.FATAL;
}
这是我的XML:
/**
* Class for logging input and output parameters of any method with annotation @Loggable.
*/
@Aspect
public final class LoggingAspect {
private final Logger log = LoggerFactory.getLogger(getClass());
/**
* @param jp - ProceedingJointPoint
* @param loggable - Loggable
* @return returns the next executable point to proceed in target
* @throws Throwable - throws exception when proceeding with joint point
*/
@Around("execution(* *(..)) && @annotation(loggable)")
public Object loggingAroundMethod(@Nonnull final ProceedingJoinPoint jp,
@Nonnull final Loggable loggable) throws Throwable {
final String signature = jp.getTarget().getClass().getName() + '.' + jp.getSignature().getName();
final List<Object> arguments = Arrays.asList(jp.getArgs());
final Object result;
try {
doLog(loggable.level(), "[BEFORE] {}{}", signature, arguments);
result = jp.proceed();
doLog(loggable.level(), "[AFTER] {}{} result={}", signature, arguments, result);
} catch (Exception e) {
log.error("[AFTER] {}{} exception={}", signature, arguments, e);
throw e;
}
return result;
}
/**
* Logs the message with appropriate log level.
* @param level - level to log
* @param format - format for logging
* @param arguments - arguments for logging
*/
private void doLog(@Nonnull final Loggable.Level level, @Nonnull final String format, final Object... arguments) {
switch (level) {
case DEBUG:
log.debug(format, arguments);
return;
case INFO:
log.info(format, arguments);
return;
case WARN:
log.warn(format, arguments);
return;
case ERROR:
break;
default:
log.error("Unable to appropriately handle given log level={}", level);
}
log.error(format, arguments);
}
}
现在,当我将@Loggable注释添加到我的程序中其他位置调用的现有方法时,所有内容都按预期在我的日志中正确显示。使用方法的工作注释看起来像这样:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
default-autowire="no">
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean class="path.to.my.package.LoggingAspect" />
</beans>
但是,当我尝试将注释添加到辅助方法而不是已在程序中调用的方法时,不会显示任何日志。所以现在不起作用的代码看起来像这样:
@Loggable
public boolean testString(String test) {
return test.equals("foo");
}
任何人都可以深入了解第一种情况的工作原理,但第二种方案使用辅助方法并不能解决问题吗?另外,辅助方法都是公开的。此外,如果我在帮助器方法中添加常规日志语句,它会显示在我的日志中。它只是由于某种原因无法使用辅助方法的注释。
答案 0 :(得分:2)
Spring只能建议已经注入其他Spring bean的Spring bean的方法。如果bean调用自己的方法之一,则不会执行建议。
Spring {A}代理在docs中解释。
答案 1 :(得分:1)