android简化版log输出
希望实现的效果:
只需要将类实现ILog接口(不需要进行任何额外的操作),然后就能直接通过printLog进行日志的输出
如下:
class A implements ILog{
void test() {
printLog("Hello World");
}
}
然后log输出的结果和Log.e(A.getClass().getCanonicalName(),"Hello World");
的输出效果一致。
实现
实现方式也很简单,只需要两个接口定义即可直接使用,只是稍微利用了接口默认实现而已:
public interface ITag {
default String tag() {
return this.getClass().getCanonicalName();
}
}
public interface ILog extends ITag{
boolean IS_DEBUGGING= true;
default void printLog(String tagContent) {
if(IS_DEBUGGING)
LogUtil.e(tag(), tagContent);
}
}