【问题标题】:Android Extends:How to Extend import android.util.Log? and What its extends name?Android扩展:如何扩展导入android.util.Log?它的扩展名是什么?
【发布时间】:2013-07-16 12:21:30
【问题描述】:

导入 android.util.Log

现在我在 Android 中使用 Log.e,Log.i,并且我正在使用导入 android.util.Log;

Log.i("TIME", "USER " + user_id + " DATE: " + date);

Log.e("TIMESHEET", "USER ID: " + user_id + " DATE: " + date);

Log.d("TIMESHEET", "USER ID: " + user_id + " DATE: " + date);

我们的团队推出我们的产品。

所以我们在代码中使用了各种日志。

为此我不得不考虑。

我创建一个类并扩展日志。

通过使用覆盖/实现方法,我可以访问我们需要的东西。

因此,我需要在我创建的类中扩展该日志。

so if any way to make all Log in one class control please suggest.

谢谢你的进步。

【问题讨论】:

  • 扩展是什么意思:添加一个新的函数,比如 Log.doMySpecilFunction()?

标签: android android-layout android-emulator


【解决方案1】:

您可以创建自己的类,例如 LogUtils,并为不同类型的日志创建静态函数。在该类中使用本机 Log 函数。例如:

/**
 * Utility methods to print log messages
 * 
 * @author shraddhas
 */
public class LogUtils
{
    /**
     * Send a message to the debug log if debugging is on
     */

    public static void trace(final String msg)
    {
        if (ApplicationConstants.IS_DEBUGGING_ON)
        {
            final String fullClassName = Thread.currentThread().getStackTrace()[3].getClassName();
            final String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
            final String methodName = Thread.currentThread().getStackTrace()[3].getMethodName();
            final int lineNumber = Thread.currentThread().getStackTrace()[3].getLineNumber();

            Log.d(ApplicationConstants.LOG_TAG, "#" + lineNumber + " " + className + "." + methodName + "() : " + msg);
        }
    }
}

【讨论】:

  • 您可以创建另一个重载函数来接受日志的类型,以便您可以根据其类型相应地打印日志。
  • 是我的 log.e 方法会自动调用你的 trace 方法。我该如何使用 LogUtils 类。
  • LogUtils.trace("yourmessage here")
  • Aravintha,你应该接受我的回答,如果它对你有用,以便其他用户可以从中受益,谢谢
猜你喜欢
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 2019-01-19
相关资源
最近更新 更多