我需要在开发日志时在我的应用程序中打印日志。那么是否有一种类似布尔值的方式来决定我的应用程序是否处于某种模式,只允许打印日志呢?每次我准备签名版本时,这将节省我删除日志的时间。
我尝试了以下解决方案:
boolean isDebuggable = 0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE);
和
if (BuildConfig.DEBUG) {
// do something for a debug build
}`
他们不像我想要的那样工作。
谢谢。
答案 0 :(得分:4)
如果您使用的是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'
}
}
答案 1 :(得分:2)
只需使用这样的静态变量Java
:
debug
当您释放apk文件时,将if(debug){
//do something
}
设置为debug
。
或者创建自己的自定义日志方法:
false
答案 2 :(得分:1)
无论哪种方式,您都可以使用静态单一方法打印日志,如下所示:
/**
* @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 类,然后注释掉日志行。就是这样。我希望你明白。
答案 3 :(得分:0)
您甚至可以将Timber用作第三方库。您无需每次在类中声明travel a("foo", "bar");
const std::string x = "baz";
travel b(x, x);
并在它显示(调试/释放)模式时进行设置。
TAG