日志的环绕通知(ip获取dubbo版)

来源:互联网 发布:淘宝查号网 编辑:程序博客网 时间:2024/04/29 22:16
package com.sf.inv.eims.aspect;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Date;import javax.annotation.PostConstruct;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.alibaba.dubbo.rpc.RpcContext;import com.sf.inv.dto.service.RequestBaseDto;import com.sf.novatar.tpl.dao.IConfigDao;import com.sf.novatar.tpl.redis.util.RedisUtil;import com.sf.novatar.tpl.util.DateUtil;import com.sf.novatar.tpl.util.StringUtil;/** * 描述:外部接口调用接口记录 *  * <pre> * HISTORY * **************************************************************************** *  ID   DATE           PERSON          REASON *  1    2016年8月25日           80002241         Create * **************************************************************************** * </pre> *  * @author 80002241 * @since 1.0 */@Component@Aspectpublic class LogAspect {// 日志private Logger logger = LoggerFactory.getLogger(LogAspect.class);@Autowiredprivate IConfigDao configDao;private String TM_CONFIG = "TM_CONFIG_";/** 访问日志打印总开关(ON/OFF) */private String AdviceLogSwitch = "SYS-AdviceLogSwitch";// 最新从redis中获取的时间private static long GETTIME;// 默认为每隔10分钟从redis里面查询一次private static final long MIN = 600000;// 缓存开关的值默认为OFFprivate static String SWITCHVALUE = "OFF";@PostConstructpublic void init() {GETTIME = new Date().getTime();SWITCHVALUE = redis(AdviceLogSwitch);}/** * 切入点测试 */@Pointcut("execution(public * com.sf.inv.eims.service.impl..*.*(..))")public void monitorStateInfo() {}// 环绕通知@Around("monitorStateInfo()")public Object aroundMonitor(ProceedingJoinPoint jPoint) throws Throwable {// 判断是否超过再次的查询的时间间隔if (new Date().getTime() - GETTIME >= MIN) {SWITCHVALUE = redis(AdviceLogSwitch);GETTIME = new Date().getTime();}String methodName = ((MethodSignature) jPoint.getSignature()).getMethod().toString();Object object = null;long methodStart = System.currentTimeMillis();Object[] args = jPoint.getArgs();try {object = jPoint.proceed(args);if ("ON".equals(SWITCHVALUE)) {logger.info("^*^|{}|{}|{}|{}|{}|{}|{}|{}|{}|",new Object[] {methodName,getRequestIp(),getLocalHostAddress(),userInfo(args),getRequestUri(),System.currentTimeMillis() - methodStart,DateUtil.date2Str(new Date(methodStart),"yyyy-MM-dd HH:mm:ss"),"ESG-EIMS-CORE", "" });}} catch (Throwable e) {if ("ON".equals(SWITCHVALUE)) {logger.info("^*^|{}|{}|{}|{}|{}|{}|{}|{}|{}|",new Object[] {methodName,getRequestIp(),getLocalHostAddress(),userInfo(args),getRequestUri(),System.currentTimeMillis() - methodStart,DateUtil.date2Str(new Date(methodStart),"yyyy-MM-dd HH:mm:ss"),"ESG-EIMS-CORE", e.getClass() });}// 将异常抛出去throw e;}return object;}private String getLocalHostAddress() {String hostAddress = "0.0.0.1";try {hostAddress = InetAddress.getLocalHost().getHostAddress();return hostAddress;} catch (UnknownHostException e) {return hostAddress;}}public String getRequestIp() {String remoteHost = "0.0.0.1";try {RpcContext context = RpcContext.getContext();if (null == context) {return remoteHost;} else {remoteHost = context.getRemoteHost();return remoteHost;}} catch (Exception e) {return remoteHost;}}private String getRequestUri() {String url = "";try {RpcContext context = RpcContext.getContext();if (null == context) {return url;} else {url = context.getUrl().toString();url = url.substring(0, url.indexOf("?"));return url;}} catch (Exception e) {return url;}}public String userInfo(Object[] args) {String userName = "system";try {if (args != null && args.length > 0) {Object object = args[0];if (object.getClass().isAssignableFrom(RequestBaseDto.class)) {userName = ((RequestBaseDto) object).getUSERNAME();if (StringUtil.isEmpty(userName)) {userName = "system";}}}return userName;} catch (Exception e) {return userName;}}/** * redis存取工具 *  * @return String */private String redis(String key) {String value = RedisUtil.get(TM_CONFIG + key);if (StringUtil.isEmpty(value)) {value = configDao.selectByPrimaryKey(key).getCfgValue();RedisUtil.setAndExpire(TM_CONFIG + key, value, Integer.parseInt(configDao.getValueByCode("SYS-EXPIRE_TIME")));}return value;}}

0 0
原创粉丝点击