How do I enable/disable log levels in Android?

后端 未结 18 2023
无人共我
无人共我 2020-11-22 16:42

I am having lots of logging statements to debug for example.

Log.v(TAG, \"Message here\");
Log.w(TAG, \" WARNING HERE\");

while deploying t

相关标签:
18条回答
  • 2020-11-22 17:30

    You should use

        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "my log message");
        }
    
    0 讨论(0)
  • 2020-11-22 17:30

    We can use class Log in our local component and define the methods as v/i/e/d. Based on the need of we can make call further.
    example is shown below.

        public class Log{
            private static boolean TAG = false;
            public static void d(String enable_tag, String message,Object...args){
                if(TAG)
                android.util.Log.d(enable_tag, message+args);
            }
            public static void e(String enable_tag, String message,Object...args){
                if(TAG)
                android.util.Log.e(enable_tag, message+args);
            }
            public static void v(String enable_tag, String message,Object...args){
                if(TAG)
                android.util.Log.v(enable_tag, message+args);
            }
        }
        if we do not need any print(s), at-all make TAG as false for all else 
        remove the check for type of Log (say Log.d).
        as 
        public static void i(String enable_tag, String message,Object...args){
        //      if(TAG)
                android.util.Log.i(enable_tag, message+args);
        }
    

    here message is for string and and args is the value you want to print.

    0 讨论(0)
  • 2020-11-22 17:33

    I created a Utility/Wrapper which solves this problem + other common problems around Logging.

    A Debugging utility with the following features:

    • The usual features provided by Log class wrapped around by LogMode s.
    • Method Entry-Exit logs: Can be turned off by a switch
    • Selective Debugging: Debug specific classes.
    • Method Execution-Time Measurement: Measure Execution time for individual methods as well as collective time spent on all methods of a class.

    How To Use?

    • Include the class in your project.
    • Use it like you use android.util.Log methods, to start with.
    • Use the Entry-Exit logs feature by placing calls to entry_log()-exit_log() methods at the beginning and ending of methods in your app.

    I have tried to make the documentation self suffiecient.

    Suggestions to improve this Utility are welcome.

    Free to use/share.

    Download it from GitHub.

    0 讨论(0)
  • 2020-11-22 17:33

    In my apps I have a class which wraps the Log class which has a static boolean var called "state". Throughout my code I check the value of the "state" variable using a static method before actually writing to the Log. I then have a static method to set the "state" variable which ensures the value is common across all instances created by the app. This means I can enable or disable all logging for the App in one call - even when the App is running. Useful for support calls... It does mean that you have to stick to your guns when debugging and not regress to using the standard Log class though...

    It's also useful (convenient) that Java interprets a boolean var as false if it hasn't been assigned a value, which means it can be left as false until you need to turn on logging :-)

    0 讨论(0)
  • 2020-11-22 17:36

    May be you can see this Log extension class: https://github.com/dbauduin/Android-Tools/tree/master/logs.

    It enables you to have a fine control on logs. You can for example disable all logs or just the logs of some packages or classes.

    Moreover, it adds some useful functionalities (for instance you don't have to pass a tag for each log).

    0 讨论(0)
  • 2020-11-22 17:37

    In a very simple logging scenario, where you're literally just trying to write to console during development for debugging purposes, it might be easiest to just do a search and replace before your production build and comment out all the calls to Log or System.out.println.

    For example, assuming you didn't use the "Log." anywhere outside of a call to Log.d or Log.e, etc, you could simply do a find and replace across the entire solution to replace "Log." with "//Log." to comment out all your logging calls, or in my case I'm just using System.out.println everywhere, so before going to production I'll simply do a full search and replace for "System.out.println" and replace with "//System.out.println".

    I know this isn't ideal, and it would be nice if the ability to find and comment out calls to Log and System.out.println were built into Eclipse, but until that happens the easiest and fastest and best way to do this is to comment out by search and replace. If you do this, you don't have to worry about mismatching stack trace line numbers, because you're editing your source code, and you're not adding any overhead by checking some log level configuration, etc.

    0 讨论(0)
提交回复
热议问题