项目中总要发布一下服务供系统内外调用,为了方便排查错误,入参出参都要以日志的形式记录下来。
传统做法:缺点一目了然,参数少的时候只能说还好,参数一多,烦不胜烦,浪费时间。
private static final Logger logger = Logger.getLogger(HelloServiceImpl.class);
public String sayHello(String name,String words) {
logger.info("remote input:name="+name+",words="+words);
//业务逻辑
String result = "Hello"+name+","+words);
logger.info("remote output:result="+result);
return result;
}
于是借鉴了AOP的思想,将日志功能做成一个切面,横向切入到业务逻辑前后(方法开始前和结束后)。
另外,解释一下什么是【基于java.lang.annotation约定】。
由于java的反射是不能获取方法的参数名的,网上有一个第三方工具包JAVAssist提供了现成的方法,但笔者觉得挺麻烦的,决定使用约定优于配置的方式来获取方法的参数名。大家大概猜到是什么了吧?对!就是建一个自定义注解。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AOPLog4jAnnotation {
String paramCollection();
}
在需要日志切面的地方(方法)加上这个注解。
public class HelloServiceImpl implements HelloService{
@Override
@AOPLog4jAnnotation(paramCollection="name,words")
public String sayHello(String name,String words) {
String text = "Hello "+name+"!"+words+".";
return text;
}
}
大家也看到了,笔者的这种方式的明显的缺点:需要按一定规则(把参数按顺序填进去,并用英文逗号隔开)把方法参数名配置在注解中。
@AOPLog4jAnnotation(paramCollection="name,words")
唯一的好处是...只需要复制上面这行,然后遵循规则填写参数名。
唯二的好处是...日志格式统一。
完了,伤心了,感觉实用性不强,哎。
算了,把代码都贴上吧,留着以后改进。
Spring配置:
<bean id="helloService" class="com.aop.log4j.service.impl.HelloServiceImpl" />
<bean id="aspect" class="com.aop.log4j.aspect.HelloAspect"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.aop.log4j.service.HelloService.*(..)))" />
<aop:aspect ref="aspect">
<aop:around pointcut-ref="pointcut" method="arround"/>
</aop:aspect>
</aop:config>
切面代码:
public class HelloAspect {
private static final Logger logger = Logger.getLogger(HelloAspect.class);
private static final String DOT = ".";//点号
private static final String COMMA = ",";//逗号
public Object arround(ProceedingJoinPoint joinPoint) throws Throwable {
StringBuilder sb = new StringBuilder();
Object[] paramValues = joinPoint.getArgs();//获取参数值
String[] paramNames = new String[paramValues.length];
Class<? extends Object> invokeClass = joinPoint.getTarget().getClass();
String signatureName = joinPoint.getSignature().getName();
Method methods[] = invokeClass.getMethods();
for (Method method : methods) {
if(method.getName().equals(signatureName)) {
String paramCollection = method.getAnnotation
(AOPLog4jAnnotation.class).paramCollection();//获取注解值
String[] names = paramCollection.split(COMMA);
System.arraycopy(names, 0, paramNames, 0, names.length);
}
}
for (int i = 0; i < paramValues.length; i++) {
sb.append(paramNames[i] + "=" + paramValues[i] + COMMA);
}
//入参日志
logger.info(invokeClass + DOT + signatureName + ",remote input:"
+ sb.toString().substring(0, sb.length() - 1));
try {
Object result = joinPoint.proceed();
//出参日志
logger.info(invokeClass + DOT + signatureName
+ ",remote output:" + result);
return result;
} catch (Exception e) {
logger.error(invokeClass + DOT + signatureName
+ " invoke error");
}
return null;
}
}
调用HelloService服务,输出:
[INFO] 2012-10-03 11:54:29,807 [com.aop.log4j.aspect.HelloAspect.arround] - class com.aop.log4j.service.impl.HelloServiceImpl.sayHello,remote input:name=Java,words=I love you.
[INFO] 2012-10-03 11:54:29,808 [com.aop.log4j.aspect.HelloAspect.arround] - class com.aop.log4j.service.impl.HelloServiceImpl.sayHello,remote output:Hello Java!I love you.