需求
產(chǎn)品有個(gè)需求是記錄用戶在APP的操作,比如切換頁簽到主頁:記錄一條日志 function:“切換主頁”,比如上傳了一份文件記錄:function:上傳文件,type:MP4,size:34M等,最后統(tǒng)計(jì)操作的總數(shù)分析用戶習(xí)慣。先不說這個(gè)統(tǒng)計(jì)有價(jià)值沒,看下如何在android上如何實(shí)現(xiàn)。
簡(jiǎn)單實(shí)現(xiàn):直接封裝一個(gè)LogService類負(fù)責(zé)上傳日志,在每個(gè)需要的地方組裝日志數(shù)據(jù),然后調(diào)用logservice.send(log),這種方法對(duì)功能代碼有很大的破壞性,隨著版本迭代會(huì)難以維護(hù)。
好點(diǎn)的辦法就是開發(fā)者無感,將所有的日志記錄放到一個(gè)地方處理,這種思想就是切面編程簡(jiǎn)稱AOP。AOP是一種方法論,JAVA里面的OOP(面向?qū)ο缶幊?,精髓是把功能或問題模塊化,每個(gè)模塊處理自己的家務(wù)事。但在現(xiàn)實(shí)世界中,并不是所有問題都能完美得劃分到模塊中。如果說,OOP如果是把問題劃分到單個(gè)模塊的話,那么AOP就是把涉及到眾多模塊的某一類問題進(jìn)行統(tǒng)一管理。比如我們可以設(shè)計(jì)一個(gè)Aspects,管理某個(gè)軟件中所有模塊的日志輸出的功能。
@Aspect實(shí)現(xiàn)切面記錄操作日志
用到的開源庫(kù):https://github.com/uPhyca/gradle-android-aspectj-plugin
Project的build.gradle 配置:classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.14'
Moduler的build.gradle配置:apply plugin: 'com.uphyca.android-aspectj'
基本流程就是在某處定義一個(gè)空方法,方法參數(shù)是我們需要上傳服務(wù)器的參數(shù)。比如某個(gè)界面CompInfoFragment
private void collectLog(String function, boolean isSucc, long ppid, long finishOpenTime, long startOpenTime) {
}
然后在需要記錄的地方調(diào)用這個(gè)方法: 比如用戶點(diǎn)擊back鍵需要記錄一條日志

接下來,我需要找到這個(gè)方法的位置,并獲取方法里面的參數(shù)。定義一個(gè)日志類加上@Aspect注解,在里面定義一個(gè)切點(diǎn),并且在這個(gè)切點(diǎn)前執(zhí)行日志的記錄:

1是定義切點(diǎn):通過類名+方法名模糊匹配到之前定義的空方法
2是切點(diǎn)名稱
3是切點(diǎn)時(shí)機(jī):這里在這個(gè)切點(diǎn)(也就是空方法執(zhí)行)前,插入我們的操作日志記錄,注意這里的方法參數(shù)名稱必須和空方法一致,并且不能是對(duì)象!不能是enum,只能是基本數(shù)據(jù)類型。
4是真正的錯(cuò)日志記錄,獲取到業(yè)務(wù)層傳過來的參數(shù),組裝日志上傳(后面再講)。
在一個(gè)日志類里面可以定義多個(gè)切點(diǎn):
@Aspect
public class AttrGroup {
@Pointcut("execution(* *..CompInfoFragment.collectLog(..))")
public void logForFunction(){};
@Before("logForFunction() && args(function,isSucc,ppid,finishOpenTime,startOpenTime)")
public void log(JoinPoint joinPoint,String function, boolean isSucc,long ppid,long finishOpenTime,long startOpenTime){
collectLog(function,isSucc,ppid,finishOpenTime,startOpenTime);
}
@Pointcut("execution(* *..CompInformationActivity.collectLog(..))")
public void logForFunction2(){};
@Before("logForFunction2() && args(ppid,position,enterType)")
public void log2(JoinPoint joinPoint,int ppid,int position,int enterType){
collectLog2(ppid,position,enterType);
}
private void collectLog(String function, boolean isSucc,long ppid,long finishOpenTime,long startOpenTime) {
List<Integer> ppids = new ArrayList<Integer>();
List<SendLogInfoEvent.LogBean> extendInfos = new ArrayList<SendLogInfoEvent.LogBean>();
SendLogInfoEvent.BVUserOperLog log = new SendLogInfoEvent.BVUserOperLog();
log.function = function;
//省略 日志的組裝
BVLogService.getInstance().addLog(log);
}
private void collectLog2(int ppid,int position,int enterType){
List<Integer> ppids = new ArrayList<Integer>();
List<SendLogInfoEvent.LogBean> extendInfos = new ArrayList<SendLogInfoEvent.LogBean>();
SendLogInfoEvent.BVUserOperLog log = new SendLogInfoEvent.BVUserOperLog();
//省略 日志的組裝
BVLogService.getInstance().addLog(log);
}
}
這樣在業(yè)務(wù)層只需要插入一個(gè)空方法,真正的日志記錄全部封裝到一個(gè)地方管理。同時(shí),根據(jù)不同的業(yè)務(wù)把日志類分類,不要把所有的切點(diǎn)放到一個(gè)類:

每個(gè)類負(fù)責(zé)維護(hù)一個(gè)業(yè)務(wù)的日志記錄。
日志記錄類的封裝
上面實(shí)現(xiàn)了對(duì)操作日志切點(diǎn)的捕捉,下面的工作就是上傳這條日志。用戶點(diǎn)擊一次就上傳一條顯然不合理,為了提高效率可以開啟工作線程收集日志,滿5條一起上傳。并且while循環(huán)10秒鐘檢測(cè)一次,有日志立刻上傳。
public class LogService {
private static LogService _instance = new LogService();
private LogService.LogerSubmitter submitter;
protected LogService() {
this.init();
}
public static LogService getInstance() {
return _instance;
}
public void init() {
this.submitter = new LogService.LogerSubmitter();
this.submitter.start();
}
public void addLog(BVUserOperLog info) {
if(this.submitter != null) {
this.submitter.addLog(info);
}
}
public void clearAllLog() {
if(this.submitter != null) {
this.submitter.clearLog();
}
}
public void destory() {
if(this.submitter != null) {
this.submitter.notifyExit();
this.submitter = null;
}
}
public interface IAddOperLog {
@POST("rs/log/addOperLog")
@ServerName("pdscommon")
Call<ResponseBody> addOperLog(@Body List<BVUserOperLog> var1);
}
private class LogerSubmitter extends Thread {
protected boolean exitFlag = false;
protected Event event = new Event();
protected Queue<BVUserOperLog> queue = new ConcurrentLinkedQueue();
private static final long COMMIT_PERIOD = 10000L;
private static final int MAX_LOG_SIZE = 5;
public LogerSubmitter() {
}
public void addLog(BVUserOperLog info) {
this.queue.add(info);
if(this.queue.size() >= 5) {
this.event.event_notifyAll();
}
}
public void clearLog() {
this.queue.clear();
}
public void notifyExit() {
this.exitFlag = true;
this.event.event_notifyAll();
}
public void run() {
while(true) {
if(!this.exitFlag) {
ArrayList lst = new ArrayList();
while(!this.queue.isEmpty()) {
lst.add(this.queue.poll());
}
this.sendLogNow(lst);
if(!this.exitFlag) {
this.event.event_wait(10000L);
continue;
}
}
return;
}
}
protected void sendLogNow(List<BVUserOperLog> lst) {
if(lst != null && !lst.isEmpty()) {
Method m = LbRestMethod.getMethod(LogService.IAddOperLog.class, "addOperLog", List.class);
LbRestMethodProxy.callMethodV2(LogService.IAddOperLog.class, lst, m);
}
}
protected MethodResponse callMethodLog(Class<?> service, Object methodParam, Method method) {
LbRestMethod rm = LbRestMethodProxy.MakeLbRestMethod(method, new LbExceptionHandler());
MethodResponse res = rm.run(service, methodParam, method);
return res;
}
}
}
至于BVUserOperLog 日志實(shí)體怎么定義,根據(jù)需求和 服務(wù)端約定吧。