如何仅在应用程序处于开发模式时打印日志?
我只需要在开发时在我的应用程序中打印日志。那么有没有像布尔的方式,将决定我的应用程序是否在某种模式,并只允许打印日志呢?这将节省我每次准备签名版本时删除日志的时间。如何仅在应用程序处于开发模式时打印日志?
我试图解决方案,如:
boolean isDebuggable = 0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE);
和:
if (BuildConfig.DEBUG) {
// do something for a debug build
}`
他们不喜欢的工作,我希望它。
谢谢。
只需使用一个静态变量debug
这样的:
if(debug){
//do something
}
并设置debug
到false
当您松开apk文件。
或者创建自己的自定义日志方法:
可用于打印日志静态单一的方法public static void logInfo(String tag, String msg){
if(DEBUG){
Log.i(tag,msg);
}
}
谢谢。应该是这样。得到它了。 – Pritish
无论哪种方式,像图所示:
/**
* @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类,只是注释掉日志行。而已。我希望你明白。
是的,这可能是一种方法。 – Pritish
是的,那是更简单的方法。 – bharat7777
如果您使用的是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'
}
}
试试吧,谢谢。 – Pritish
[删除使用ProGuard登录电话](http://*.com/questions/13218772/removing-log-call-using-proguard),用于发布模式 –
这些proguard的规则可能重复你意味着我正在发展? –
你想在调试模式下关闭日志吗? –