我在我的 Web 应用程序中使用了 spring aop(面向方面的编程)。现在我想将日志添加到连接点出现的地方。如何为此任务配置我的 log4j xml。
问问题
1949 次
1 回答
3
为此,我创建了一个 Aspect 记录器。这适用于不同的层,如控制器、具有不同日志记录级别的服务 DAO。
@Aspect
public class LoggerAspect {
private final static Logger LOGGER = LoggerFactory
.getLogger(LoggerAspect.class);
@Autowired
private RequestInfo requestInfo;
@Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object logWebRequest(ProceedingJoinPoint pjp) throws Throwable {
Object obj;
Long startTime = System.currentTimeMillis();
if (SecurityContextHolder.getContext().getAuthentication() != null) {
Object user = SecurityContextHolder.getContext()
.getAuthentication().getPrincipal();
LOGGER.info("User: " + user);
}
LOGGER.info("Controller UUID: " + requestInfo.getRequestUUID());
LOGGER.info("Controller URL: " + requestInfo.getRequestMethod());
LOGGER.info("Controller Method: " + pjp.getSignature());
logArguments(pjp);
try {
obj = pjp.proceed();
} catch (Exception e) {
LOGGER.error("Controller:" + e.getMessage());
throw e;
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Returns: " + obj);
LOGGER.debug("Controller Response Time: "
+ (System.currentTimeMillis() - startTime) + " ms");
}
return obj;
}
@Around("execution(public * com.common.service..*.*(..))")
public Object logCommonServiceRequest(ProceedingJoinPoint pjp)
throws Throwable {
return logServiceRequest(pjp);
}
@Around("execution(public * com.modules.*.service..*.*(..))")
public Object logModulesServiceRequest(ProceedingJoinPoint pjp)
throws Throwable {
return logServiceRequest(pjp);
}
private Object logServiceRequest(ProceedingJoinPoint pjp) throws Throwable,
Exception {
Object obj;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Service: " + pjp.getTarget().getClass() + " -- "
+ pjp.getSignature());
logArguments(pjp);
}
try {
obj = pjp.proceed();
} catch (Exception e) {
LOGGER.error("Service Exception:" + e.getMessage());
throw e;
}
if (LOGGER.isDebugEnabled())
LOGGER.debug("Service Returns: " + obj);
return obj;
}
@Around("execution(public * com.common.dao..*.*(..))")
public Object logCommonDAORequest(ProceedingJoinPoint pjp) throws Throwable {
return logDAORequest(pjp);
}
@Around("execution(public * com.modules.*.dao..*.*(..))")
public Object logModulesDAORequest(ProceedingJoinPoint pjp)
throws Throwable {
return logDAORequest(pjp);
}
private Object logDAORequest(ProceedingJoinPoint pjp) throws Throwable,
Exception {
Object obj;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("DAO: " + pjp.getTarget().getClass() + " -- "
+ pjp.getSignature());
logArguments(pjp);
}
try {
obj = pjp.proceed();
} catch (Exception e) {
LOGGER.error("DAO Exception:" + e.getMessage());
throw e;
}
if (LOGGER.isDebugEnabled())
LOGGER.debug("DAO Returns: " + obj);
return obj;
}
private void logArguments(ProceedingJoinPoint pjp) {
StringBuffer argLog = new StringBuffer();
for (Object arg : pjp.getArgs()) {
argLog.append(arg);
argLog.append(",");
}
LOGGER.info("Args: " + argLog);
}
}
<aop:aspectj-autoproxy proxy-target-class="true">
<aop:include name="loggerAspect" />
</aop:aspectj-autoproxy>
<bean id="loggerAspect" class="com.service.LoggerAspect">
</bean>
于 2012-07-25T05:30:50.003 回答