springのaopはログ記録を実現します.
6549 ワード
既に作成されたシステムに対しては、いくつかの業務に対してログを記録し、例えばcrud操作のタイプは、springのaopを用いて実装することができます.具体的な実現は以下の通りです.
私は注解バージョンを使っています.まず配置ファイルに配置を追加します.
私は注解バージョンを使っています.まず配置ファイルに配置を追加します.
そして、新しい注釈を作成してログを記録するためのcontrollerを表示します.@Target(value = { ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SaveLogAnnotation {
String operateDesc();
OperateType operateType();//
PlatformType platformType();//
}
うどん類は具体的に操作します. @Aspect
@Component
public class TxAop {
@Autowired
private HttpServletRequest request;
private static final String OPERATE_TYPE = "operateType";
private static final String PLATFORM_TYPE = "platformType";
//@Pointcut("execution(* org.roger.service.impl.*.*(..))")
@Pointcut("@annotation(org.roger.test.SaveLogAnnotation)")
public void pt(){}
// method
@Before(value = "operateLogAspect()")
public void methodBefore(JoinPoint joinPoint){
try {
/*ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();*/
String requestAddr=request.getRequestURL().toString();
Map operateMap=getOperateType(joinPoint);
String platformType=operateMap.get(PLATFORM_TYPE);
logger.info(" :" + requestAddr);
logger.info(" :" + joinPoint.getSignature().toString());
String requestParams=Arrays.toString(joinPoint.getArgs());
logger.info(" :" + requestParams);
logger.info(" :" + platformType);
logger.info(" ip:" + NetworkUtil.getIpAddress(request));
//
AuditLogDto info=new AuditLogDto();
info.setTransNo(LogTransNoUtil.getTransNo());
info.setOperateBy(operateBy);
info.setOperateDesc(operateMap.get(OPERATE_DESC));
info.setOperateType(operateMap.get(OPERATE_TYPE));
info.setRequestIp(NetworkUtil.getIpAddress(request));
info.setRequestAddr(requestAddr);
info.setRequestMethod(joinPoint.getSignature().toString());
info.setRequestParams(requestParams);
info.setPlatformType(platformType);
auditLogService.saveAuditLog(info);
} catch (Exception e) {
logger.error("ERROR:", e);
}
}
/**
*
* @return
* @throws Exception
*/
private Map getOperateType(JoinPoint joinPoint)throws Exception {
Class targetClass = joinPoint.getTarget().getClass();
Method[] methods = targetClass.getMethods();
String methodName = joinPoint.getSignature().getName();
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Map result = new HashMap();
result.put(OPERATE_DESC, method.getAnnotation(SaveLogAnnotation.class).operateDesc());
result.put(OPERATE_TYPE, method.getAnnotation(SaveLogAnnotation.class).operateType().toString());
result.put(PLATFORM_TYPE, method.getAnnotation(SaveLogAnnotation.class).platformType().toString());
return result;
}
}
return null;
}
}
ipアドレスツール類を取得するpublic final class NetworkUtil {
/**
* Logger for this class
*/
private static final Logger logger = LoggerFactory.getLogger(NetworkUtil.class);
/**
* IP , , IP ;
*
* @param request
* @return
* @throws IOException
*/
public final static String getIpAddress(HttpServletRequest request) throws IOException {
// IP , , IP
String ip = request.getHeader("X-Forwarded-For");
if (logger.isInfoEnabled()) {
logger.info("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip=" + ip);
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
if (logger.isInfoEnabled()) {
logger.info("getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip=" + ip);
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
if (logger.isInfoEnabled()) {
logger.info("getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip=" + ip);
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
if (logger.isInfoEnabled()) {
logger.info("getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip=" + ip);
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
if (logger.isInfoEnabled()) {
logger.info("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip=" + ip);
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if (logger.isInfoEnabled()) {
logger.info("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip=" + ip);
}
}
} else if (ip.length() > 15) {
String[] ips = ip.split(",");
for (int index = 0; index < ips.length; index++) {
String strIp = (String) ips[index];
if (!("unknown".equalsIgnoreCase(strIp))) {
ip = strIp;
break;
}
}
}
return ip;
}
}
最後にcontrollerの要求方法に注解をすればいいです.@SaveLogAnnotation(operateDesc=" ",operateType=OperateType.ADD,platformType=PlatformType.APP)