仅当应用程序处于开发模式时如何打印日志?
Posted
技术标签:
【中文标题】仅当应用程序处于开发模式时如何打印日志?【英文标题】:How to print log only when app is in development mode? 【发布时间】:2017-05-23 14:52:09 【问题描述】:只有在开发应用程序时,我才需要在应用程序中打印日志。那么有没有像布尔值这样的方法来决定我的应用程序是否处于某种模式,然后只允许打印日志?这将节省我每次准备签名构建时删除日志的时间。
我尝试了以下解决方案:
boolean isDebuggable = 0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE);
和:
if (BuildConfig.DEBUG)
// do something for a debug build
`
它们不像我想要的那样工作。
谢谢。
【问题讨论】:
你说我正在开发是什么意思? Removing Log call using proguard 的可能重复项,使用这些 proguard 规则进行发布模式 你想在调试模式下关闭日志吗? @VishalPatoliyaツ 表示当时我正在编写代码和所有内容。因此我需要打印日志来检查我的逻辑,但后来我不需要日志。 【参考方案1】:如果您使用的是 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'
【讨论】:
【参考方案2】:只需像这样使用静态变量debug
:
if(debug)
//do something
并在发布 apk 文件时将debug
设置为false
。
或者创建自己的自定义日志方法:
public static void logInfo(String tag, String msg)
if(DEBUG)
Log.i(tag,msg);
【讨论】:
【参考方案3】:无论哪种方式,您都可以使用静态单一方法打印日志,如下所示:
/**
* @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 类并注释掉日志行。而已。希望你能理解。
【讨论】:
【参考方案4】:您甚至可以将Timber 用作第三方库。您无需每次在您的类中声明TAG
并设置它何时显示(调试/发布)模式。
implementation 'com.jakewharton.timber:timber:4.7.1'
if (BuildConfig.DEBUG)
Timber.plant(new Timber.DebugTree());
else
Timber.plant(new NoLogTree());
【讨论】:
以上是关于仅当应用程序处于开发模式时如何打印日志?的主要内容,如果未能解决你的问题,请参考以下文章
当且仅当区域在 Emacs 中处于活动状态时,标记是不是处于活动状态?