0

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.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    Just create a Class with static methods in it and use flag for logging . Change flag if want to disable logs . You can also use [Timber](https://github.com/JakeWharton/timber) if you want to . – ADM Feb 14 '19 at 06:24
  • you can also create an interface which you use its methods in your classes – masoud vali Feb 14 '19 at 06:28
  • What is better is to get rid of logs as soon as you don't need them anymore.Instead of making all these objects and fill your memory. – Steve Moretz Feb 14 '19 at 07:02
  • I mean, I want to create some kind of listener for Method or Object Callback and Log it, and the controller of that Log is within one class, so the problem still same, if I have a hundred of method or class, I still need to write it one by one in every method by jumping to another file, I can delete it instanly using replacement ***for cleaning purpose on production, but if I want to rewite the log for my devopment purpose it will need more effort to positioning the Log again in every method*** – Davin Reinaldo Gozali Feb 14 '19 at 10:17

4 Answers4

0

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;
}


}
Farid Haq
  • 3,728
  • 1
  • 21
  • 15
  • Thank you, this is help for disable the Log by using flag, but not solving my problem if I still need to call the Log method in every method that I want to Log. the problem still same, if I have a hundred of method or class, I still need to write it one by one in every method by jumping to another file, I can delete it instanly using replacement for cleaning purpose, but if I want to rewite the log for my devopment purpose it will need more effort to positioning the Log again in every method – Davin Reinaldo Gozali Feb 14 '19 at 10:13
0

Yes you can create a separate class for LOGS containing static methods with a flag and disable the flag when you don't need that.

  • Thank you, this is help for disable the Log by using flag, but not solving my problem if I still need to call the Log method in every method that I want to Log. the problem still same, if I have a hundred of method or class, I still need to write it one by one in every method by jumping to another file, I can delete it instanly using replacement for cleaning purpose, but if I want to rewite the log for my devopment purpose it will need more effort to positioning the Log again in every method – Davin Reinaldo Gozali Feb 14 '19 at 10:13
0

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);
        }
    }
}
Nilesh Panchal
  • 1,059
  • 1
  • 10
  • 24
0

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

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141