1

パフォーマンスに関しては、次の方法を使用することを提案する人もいます。

public class MyActivity extends Activity {  

 private static final String TAG = "MyApp";  
 private static final boolean D = true;

 @Override  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(D) Log.e(TAG, "MyActivity.onCreate debug message");  }

しかし、大規模なプロジェクトで作業している場合、これは意味がありません。デバッグする場合、debugフラグ用に多くのファイルを更新する必要があるためです。より良い方法はありますか?

4

6 に答える 6

6

BuildConfig で DEBUG ブール値を確認できます。

if (BuildConfig.DEBUG) {
    // Do what you need
}

または、デバッグ変数を使用することもできますが、代わりに、またはすべてのアクティビティでそれを保持し、Application クラスで宣言し、必要なときにいつでもその値を確認します。

その変数の目的がログ記録である場合は、ログを別のクラスにラップして、DEBUG 変数をチェックすることをお勧めします。

public class LogUtils {
    public static void LOGD(final String tag, String message) {
        if (BuildConfig.DEBUG) {
            Log.d(tag, message);
        }
    }

    public static void LOGV(final String tag, String message) {
        if (BuildConfig.DEBUG) {
            Log.v(tag, message);
        }
    }

    public static void LOGI(final String tag, String message) {
        if (BuildConfig.DEBUG) {
            Log.i(tag, message);
        }
    }

    public static void LOGW(final String tag, String message) {
        if (BuildConfig.DEBUG) {
            Log.w(tag, message);
        }
    }

    public static void LOGE(final String tag, String message) {
        if (BuildConfig.DEBUG) {
            Log.e(tag, message);
        }
    }

}

次に、このクラスへのログ呼び出しを行います。

LogUtils.LOGD(TAG, "MyActivity.onCreate debug message");
于 2012-07-22T17:10:16.420 に答える
4

Google の担当者がオープン ソース アプリ iosched で開発したものを強くお勧めします。他の理由の中でも特に、指定されたタグのログがisLoggableBuildConfigで指定されたレベルでログ可能かどうかを確認し、チェックします。私のプロジェクトには必須です。

/*
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.apps.iosched.util;

import com.google.android.apps.iosched.BuildConfig;

import android.util.Log;

/**
 * Helper methods that make logging more consistent throughout the app.
 */
public class LogUtils {
    private static final String LOG_PREFIX = "iosched_";
    private static final int LOG_PREFIX_LENGTH = LOG_PREFIX.length();
    private static final int MAX_LOG_TAG_LENGTH = 23;

    public static String makeLogTag(String str) {
        if (str.length() > MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH) {
            return LOG_PREFIX + str.substring(0, MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH - 1);
        }

        return LOG_PREFIX + str;
    }

    /**
     * WARNING: Don't use this when obfuscating class names with Proguard!
     */
    public static String makeLogTag(Class cls) {
        return makeLogTag(cls.getSimpleName());
    }

    public static void LOGD(final String tag, String message) {
        if (Log.isLoggable(tag, Log.DEBUG)) {
            Log.d(tag, message);
        }
    }

    public static void LOGD(final String tag, String message, Throwable cause) {
        if (Log.isLoggable(tag, Log.DEBUG)) {
            Log.d(tag, message, cause);
        }
    }

    public static void LOGV(final String tag, String message) {
        //noinspection PointlessBooleanExpression,ConstantConditions
        if (BuildConfig.DEBUG && Log.isLoggable(tag, Log.VERBOSE)) {
            Log.v(tag, message);
        }
    }

    public static void LOGV(final String tag, String message, Throwable cause) {
        //noinspection PointlessBooleanExpression,ConstantConditions
        if (BuildConfig.DEBUG && Log.isLoggable(tag, Log.VERBOSE)) {
            Log.v(tag, message, cause);
        }
    }

    public static void LOGI(final String tag, String message) {
        Log.i(tag, message);
    }

    public static void LOGI(final String tag, String message, Throwable cause) {
        Log.i(tag, message, cause);
    }

    public static void LOGW(final String tag, String message) {
        Log.w(tag, message);
    }

    public static void LOGW(final String tag, String message, Throwable cause) {
        Log.w(tag, message, cause);
    }

    public static void LOGE(final String tag, String message) {
        Log.e(tag, message);
    }

    public static void LOGE(final String tag, String message, Throwable cause) {
        Log.e(tag, message, cause);
    }

    private LogUtils() {
    }
}
于 2014-01-29T09:47:06.747 に答える
3

別の解決策は、このやや関連する質問に対する回答の 1 つにあります。次のように Log クラスをオーバーライドできます。

public class Log {
    static final boolean LOG = false;

    public static void i(String tag, String string) {
        if (LOG) android.util.Log.i(tag, string);
    }
    public static void e(String tag, String string) {
        if (LOG) android.util.Log.e(tag, string);
    }
    public static void d(String tag, String string) {
        if (LOG) android.util.Log.d(tag, string);
    }
    public static void v(String tag, String string) {
        if (LOG) android.util.Log.v(tag, string);
    }
    public static void w(String tag, String string) {
        if (LOG) android.util.Log.w(tag, string);
    }
}

これにより、log を使用するたびに if ステートメントを使用する必要がなくなります。オーバーライドされた Log クラスのブール値を変更するだけです。公開する準備ができたら、ProGuard などのツールを使用して、パフォーマンスのためにログへのすべての参照を取り除くことができます。

于 2012-07-22T17:18:03.320 に答える
2

ProGuard を使用して Log.v および Log.d メッセージを取り除く

より少ないコードでの代替アプローチは、ProGuardを使用して最終リリース アプリ用にこれらを削除することです。

基本的に、app\proguard-rules.proファイルで、android.util.Log取り除きたいクラスのメソッドを定義します。次のように proguard-rules.pro ファイルに追加すると、ビルド時にv(verbose) およびd(debug) メソッドが取り除かれます。

# This tell Proguard to assume Log.v and Log.d have no side effects (even
# though they do since they write to the logs) and thus can be removed
# during optimization:
-assumenosideeffects class android.util.Log {
    public static int v(...);
    public static int d(...);
}

if (BuildConfig.DEBUG)これにより、コード全体に散らばる -style チェックの必要がなくなります。

参照: Android アプリのリリースで LogCat 出力を完全に無効にしますか?

于 2014-09-30T17:38:02.403 に答える
1

私は最近同じ問題を抱えていましたが、ログを無効にするために Proguard でクラスを削除することは良い考えではないと思います。そのため、標準の Android Log クラスの単純なドロップイン置換を作成することになりました

https://github.com/zserge/log

ログレベルを制御できます。また、複数の値をログに記録したり、ログタグなどを記録したりするための多くの「砂糖」も提供します。Maven Central/JCenter で利用できるコードはわずか 200 行です。

于 2015-06-14T12:10:50.700 に答える
1

LogWrapperシンプルで次のようなクラスを作成しました。

public class LogWrapper {
    private static final String DEBUG_TAG = "some-tag"
    private static boolean logsEnabled;

    public static void e(String msg) {
      if (logsEnabled) {      
        Log.e(DEBUG_TAG, msg);
      }
    }

    // other Log methods
}

Log クラスの代わりに使用でき、1 か所で必要に応じてブール変数を変更できます。お役に立てれば。

于 2012-07-22T17:15:44.290 に答える