1. 程式人生 > >專案中日誌的簡單記錄

專案中日誌的簡單記錄

根據需要新增日誌的方法配置方法名稱

logMethod.properties

#公共日誌方法配置
#key :介面方法名和入參物件   value :方法對應的模組值
createOrder_OrderInfo=4
#申請退款
applyForRefund_OrderReturnRequest=4
載入配置檔案類:

public class LogMethodConstants {
private final static Logger logger = LoggerFactory.getLogger(LogMethodConstants.class);

public final  static List<String> methods =Lists.newArrayList();

    public final static Map<String,String>  map=Maps.newHashMap();

			static{

				Properties pro = new Properties();
				try {
					pro.load(LogMethodConstants.class.getResourceAsStream("/props/logMethod.properties"));
					Set<Entry<Object,Object>> entrySet = pro.entrySet();
					for (Entry<Object, Object> entry : entrySet) {
						methods.add(entry.getKey().toString().split("_")[0]);
						map.put(entry.getKey().toString().split("_")[0], entry.getValue().toString());
						methodEntrys.put(entry.getKey().toString().split("_")[0], entry.getKey().toString().split("_")[1]);
			        }
				} catch (IOException e) {
					logger.info("LogMethodConstants 異常:{}",e);
				}

			}
}

日誌切面類:

@Component
@Aspect
public class CommonLogAspect {
    private final static Logger logger = LoggerFactory.getLogger(CommonLogAspect.class);

    @Resource(name="fmqLogProducer")
    private MqProducer producer;

    List<String> methods =	LogMethodConstants.methods;
    Map<String,String> map  = LogMethodConstants.map;

    private String beforeParam ="";
    @Value("${app.name}")
    private String appName;

	/**
	  *  定義一個切點public * com.jd.crowdfunding.mall.miste.service..*.*(..)
	  *
	  */

   @Pointcut("execution(public * com.jd.crowdfunding.mall.order.*.*.*(..))")
    public void pointcut(){}

    //	value = "pointcut() &&"+ "args(request,..)"
	@Before(value = "pointcut()")
	public void saveLogBefore(JoinPoint point){

		logger.info("===============before====================");
		String methodName = point.getSignature().getName();
		if(methods.contains(methodName)){
			Object[] args = point.getArgs();
			logger.info("進入before:{}",JSON.toJSONString(args));
			beforeParam = JSON.toJSONString(args);
		}

	}

	//value="pointcut() &&"+ "args(request,..)"
	@After(value="pointcut()")
	public  void  saveLogAfter(JoinPoint point){
		logger.info("================after===================");
		String methodName = point.getSignature().getName();
		String className = point.getTarget().getClass().getName();
		if(methods.contains(methodName)){
			Object[] args = point.getArgs();
			String afterParam = JSON.toJSONString(args);
			logger.info("進入after:{}",JSON.toJSONString(args));
			Integer code = Integer.valueOf(map.get(methodName));
			
			saveLog(code,beforeParam,afterParam,methodName,className);
		}
	}


    public void saveLog(Integer code,Object beforeParam,Object afterParam,String methodName,String className){
    	try {
//   HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    		/** 儲存日誌,傳送MQ  */
    		CallerInfo callerInfo = new CallerInfo(LogSystem.CF_MALL.getCode(),appName,code,dataId,beforeParam.toString(),afterParam.toString(),IP,className,methodName);
    		callerInfo.setOperationPin(operationPin);
    		logger.info("儲存日誌,傳送MQ入參:{}",JSON.toJSONString(callerInfo));
    		producer.process(callerInfo);
   	} catch (Exception e) {
   		logger.info("儲存日誌,傳送MQ異常",e);
    	}
    }
    }

這裡我們採用非同步傳送MQ的形式記錄日誌,也可以呼叫JSF介面形式,根據不同的專案按照實際情況操作。