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

How to print log only when app is in development mode?

我仅在开发应用程序时才需要在应用程序中打印日志。那么有没有像布尔值这样的方法来决定我的应用程序是否处于某种模式,然后只允许打印日志?这将节省我每次准备签名构建时删除日志的时间。

我尝试了以下解决方案:

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

和:

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

它们不像我想要的那样工作。

谢谢。

只需像这样使用静态变量debug

if(debug){
//do something
}

并在发布apk文件时将debug设置为false

或创建您自己的自定义日志方法:

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

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

/**
 * @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 class 并注释掉日志行。而已。希望大家理解。

如果您使用的是 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'
        }
    }

您甚至可以将 Timber 用作第三方库。您不需要每次都在 类 中声明 TAG 并在显示 (Debug/Release) 模式时进行设置。

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

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