android 如何只在应用程序处于开发模式时打印日志?

dpiehjr4  于 2022-12-09  发布在  Android
关注(0)|答案(4)|浏览(250)

我只需要在我的应用程序开发时打印日志。那么,有没有一种像布尔值这样的方法来决定我的应用程序是否处于某个模式,并且只允许打印日志呢?这将保存我每次准备签名构建时删除日志的时间。
我尝试了以下解决方案:

boolean isDebuggable = 0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE);

以及:

if (BuildConfig.DEBUG) {
  // do something for a debug build
}`

他们不像我想的那样工作。

  • 谢谢-谢谢
y1aodyip

y1aodyip1#

如果您使用的是Android Studio,请尝试利用gradle文件。

defaultConfig {
        applicationId "com.your.application.id"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        //Defining Log debugging
        buildConfigField "boolean", "LOG_DEBUG_MODE", "false"
        buildConfigField "boolean", "LOG_DEBUG_WITH_STACKTRACE_MODE", "false"
    }

    buildTypes {
        debug {
            testCoverageEnabled = "true"
            buildConfigField "boolean", "LOG_DEBUG_MODE", "true"
            buildConfigField "boolean", "LOG_DEBUG_WITH_STACKTRACE_MODE", "true"
        }

        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
d7v8vwbk

d7v8vwbk2#

只需使用静态变量debug,如下所示:

if(debug){
//do something
}

并在发布apk文件时将debug设置为false
或者创建您自己的自定义日志方法:

public static void logInfo(String tag, String msg){
    if(DEBUG){
        Log.i(tag,msg);
    }
}
chy5wohz

chy5wohz3#

无论哪种方式,您都可以使用静态单一方法打印日志,如下所示:

/**
 * @param TAG
 * @param Message
 * @param LogType
 */
public static void Log(String TAG, String Message, int LogType) {
    switch (LogType) {
        // Case 1- To Show Message as Debug
        case 1:
            Log.d(TAG, Message);
            break;
        // Case 2- To Show Message as Error
        case 2:
            Log.e(TAG, Message);
            break;
        // Case 3- To Show Message as Information
        case 3:
            Log.i(TAG, Message);
            break;
        // Case 4- To Show Message as Verbose
        case 4:
            Log.v(TAG, Message);
            break;
        // Case 5- To Show Message as Assert
        case 5:
            Log.w(TAG, Message);
            break;
        // Case Default- To Show Message as System Print
        default:
            System.out.println(Message);
            break;
    }
}

public static void Log(String TAG, String Message) {
    AppDelegate.Log(TAG, Message, 1);
}

/* Function to show log for error message */
public static void LogD(String Message) {
    AppDelegate.Log(Tags.DATE, "" + Message, 1);
}

/* Function to show log for error message */
public static void LogDB(String Message) {
    AppDelegate.Log(Tags.DATABASE, "" + Message, 1);
}

/* Function to show log for error message */
public static void LogE(Exception e) {
    if (e != null) {
        AppDelegate.LogE(e.getMessage());
        e.printStackTrace();
    } else {
        AppDelegate.Log(Tags.ERROR, "exception object is also null.", 2);
    }
}

/* Function to show log for error message */
public static void LogE(OutOfMemoryError e) {
    if (e != null) {
        AppDelegate.LogE(e.getMessage());
        e.printStackTrace();
    } else {
        AppDelegate.Log(Tags.ERROR, "exception object is also null.", 2);
    }
}

/* Function to show log for error message */
public static void LogE(String message, Exception exception) {
    if (exception != null) {
        AppDelegate.LogE("from = " + message + " => "
                + exception.getMessage());
        exception.printStackTrace();
    } else {
        AppDelegate.Log(Tags.ERROR, "exception object is also null. at "
                + message, 2);
    }
}

/**
 * Funtion to log with tag RESULT, you need to just pass the message.
 *
 * @String Message = pass your message that you want to log.
 */
public static void LogR(String Message) {
    AppDelegate.Log(Tags.RESULT, "" + Message, 1);
}

/**
 * Funtion to log with tag RESULT, you need to just pass the message.
 *
 * @String Message = pass your message that you want to log.
 */
public static void LogI(String Message) {
    AppDelegate.Log(Tags.INTERNET, "" + Message, 1);
}

之后,你只需要为登录应用程序写:

AppDelegate.LogT("Hello for testing purpose");

当你不想显示日志的时候,就去AppDelegate类,注解掉日志行。就是这样。我希望你能理解。

mw3dktmi

mw3dktmi4#

你甚至可以使用Timber作为第三方库,你不需要每次都在类中声明TAG并设置它何时显示(Debug/Release)模式。

implementation 'com.jakewharton.timber:timber:4.7.1'

if (BuildConfig.DEBUG) {
    Timber.plant(new Timber.DebugTree());
}

相关问题