aop日志 android,AOP实现的操作日志记录

需求

产品有个需求是记录用户在APP的操作,比如切换页签到主页:记录一条日志 function:“切换主页”,比如上传了一份文件记录:function:上传文件,type:MP4,size:34M等,最后统计操作的总数分析用户习惯。先不说这个统计有价值没,看下如何在android上如何实现。

简单实现:直接封装一个LogService类负责上传日志,在每个需要的地方组装日志数据,然后调用logservice.send(log),这种方法对功能代码有很大的破坏性,随着版本迭代会难以维护。

好点的办法就是开发者无感,将所有的日志记录放到一个地方处理,这种思想就是切面编程简称AOP。AOP是一种方法论,JAVA里面的OOP(面向对象编程),精髓是把功能或问题模块化,每个模块处理自己的家务事。但在现实世界中,并不是所有问题都能完美得划分到模块中。如果说,OOP如果是把问题划分到单个模块的话,那么AOP就是把涉及到众多模块的某一类问题进行统一管理。比如我们可以设计一个Aspects,管理某个软件中所有模块的日志输出的功能。

@Aspect实现切面记录操作日志

Project的build.gradle 配置:classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.14'

Moduler的build.gradle配置:apply plugin: 'com.uphyca.android-aspectj'

基本流程就是在某处定义一个空方法,方法参数是我们需要上传服务器的参数。比如某个界面CompInfoFragment

private void collectLog(String function, boolean isSucc, long ppid, long finishOpenTime, long startOpenTime) {

}

然后在需要记录的地方调用这个方法: 比如用户点击back键需要记录一条日志

577e4bd47195

接下来,我需要找到这个方法的位置,并获取方法里面的参数。定义一个日志类加上@Aspect注解,在里面定义一个切点,并且在这个切点前执行日志的记录:

577e4bd47195

image.png

1是定义切点:通过类名+方法名模糊匹配到之前定义的空方法

2是切点名称

3是切点时机:这里在这个切点(也就是空方法执行)前,插入我们的操作日志记录,注意这里的方法参数名称必须和空方法一致,并且不能是对象!不能是enum,只能是基本数据类型。

4是真正的错日志记录,获取到业务层传过来的参数,组装日志上传(后面再讲)。

在一个日志类里面可以定义多个切点:

@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 ppids = new ArrayList();

List extendInfos = new ArrayList();

SendLogInfoEvent.BVUserOperLog log = new SendLogInfoEvent.BVUserOperLog();

log.function = function;

//省略 日志的组装

BVLogService.getInstance().addLog(log);

}

private void collectLog2(int ppid,int position,int enterType){

List ppids = new ArrayList();

List extendInfos = new ArrayList();

SendLogInfoEvent.BVUserOperLog log = new SendLogInfoEvent.BVUserOperLog();

//省略 日志的组装

BVLogService.getInstance().addLog(log);

}

}

这样在业务层只需要插入一个空方法,真正的日志记录全部封装到一个地方管理。同时,根据不同的业务把日志类分类,不要把所有的切点放到一个类:

577e4bd47195

image.png

每个类负责维护一个业务的日志记录。

日志记录类的封装

上面实现了对操作日志切点的捕捉,下面的工作就是上传这条日志。用户点击一次就上传一条显然不合理,为了提高效率可以开启工作线程收集日志,满5条一起上传。并且while循环10秒钟检测一次,有日志立刻上传。

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 addOperLog(@Body List var1);

}

private class LogerSubmitter extends Thread {

protected boolean exitFlag = false;

protected Event event = new Event();

protected Queue 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 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 日志实体怎么定义,根据需求和 服务端约定吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值