简体   繁体   中英

Is it possible to create one class that handle all log code in android (ex. Log.d(TAG,message) )

I want to find a way if it's possible in my case like this : I have many java class:

First :

 class A{
       A(){
          Log.d(TAG,"message construct A");
       }

       public void myMethod(){
          Log.d(TAG,"message A");
       }

       public void myAnotherMethod(){
          Log.d(TAG,"message another A");
       }
    }

Second :

class B{
       B(){
          Log.d(TAG,"message construct B");
       }

       public void myBMethod(){
          Log.d(TAG,"message B");
       }

       public void myAnotherBMethod(){
          Log.d(TAG,"message another B");
       }
}

it's quite a waste of time if i want to delete all the Log within 300 class that I have created and if I want to re-create the Log again will be very tiring for jumping from one class to another class..

I wonder if it's possible for some kind of this Log Management design :

A.class

class A{
       A(){
          //my code
       }

       public void myMethod(){
          //my code
       }

       public void myAnotherMethod(){
          //my code
       }
}

B.class

class B{
       B(){
          //my code
       }

       public void myBMethod(){
          //my code
       }

       public void myAnotherBMethod(){
          //my code
       }
}

and the log class management :

class Log {

    onObjectCreated(){
       if(object == A) Log.d(TAG,"construct A");
       else Log.d(TAG,"construct B");

    }
    onMethodCall(){
       Log.d(TAG,"message A");
    }

    onMethodBCall(){
       Log.d(TAG,"message B");
    }

}

So, in future development if I want to remove or add some Log code, I just need to manage it within one class

Is there any way for that in Native Java Android ? Thank you.

Try it , I think will helpful for you.

Logger.i(TAG, AppConst.EACH_PAGE_FOR_LOG);

Logger Example:

public class Logger {

public static void d(String tag, String data){
    if(BuildConfig.DEBUGGABLE) {
        Log.d(tag, data);
    }
}

public static void i(String tag, String data){
    if(BuildConfig.DEBUGGABLE) {
        Log.i(tag, data);
    }
}

public static void e(String tag, String data){
    if(BuildConfig.DEBUGGABLE) {
        Log.e(tag, data);
    }
}

public static void e(String tag, String data, Throwable t){
    if(BuildConfig.DEBUGGABLE) {
        Log.e(tag, data, t);
    }
}

public static void w(String tag, String data){
    if(BuildConfig.DEBUGGABLE) {
        Log.w(tag, data);
    }
}

public static void wtf(String tag, String data){
    if(BuildConfig.DEBUGGABLE) {
        Log.wtf(tag, data);
    }
}

public static boolean isDebugEnabled(String tag) {
   return BuildConfig.DEBUGGABLE;
}


}

是的,您可以为包含静态方法的LOGS创建一个带有标志的单独类,并在不需要时禁用该标志。

Maybe it will help to make your task easier for all in one class

import android.util.Log;


public class AppLogClass {

    public void e(String tag, String msg){
        if (BuildConfig.DEBUG) {
            Log.e(tag, msg);
        }
    }

    public void d(String tag, String msg){
        if (BuildConfig.DEBUG) {
            Log.d(tag, msg);
        }
    }
}

Aside from creating own logger, you can alternatively use proguard to remove all log calls of your choice (or all) without a need to altering your sources. See this question: Removing Log call using proguard

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM