Android 日志记录级别

发布于 2024-10-01 08:59:37 字数 721 浏览 2 评论 0原文

我在配置 Android 日志记录时遇到了一些困难。我的代码如下所示:

    if (Log.isLoggable("MY_TAG", Log.VERBOSE)) {
        Log.v("MY_TAG", "Here's a log message");
    }

很简单,对吧?

但是,我很难让 Log.isLoggable("MY_TAG", Log.VERBOSE) 返回 true。

根据 http://developer.android.com/reference/android/util/Log。 html,我尝试将 local.prop 文件添加到 /data/ 目录,如下所示:

log.tag.MY_TAG=VERBOSE

但没有运气。我也尝试过:

System.setProperty("log.tag.MY_TAG", String.valueOf(Log.VERBOSE));

但这也不起作用。

关于我在这里做错了什么有什么想法吗?我在 Nexus 1 上运行 Android 2.1-update1(如果这有什么区别的话)。

I'm having a little difficulty configuring Android logging. Here's what my code looks like:

    if (Log.isLoggable("MY_TAG", Log.VERBOSE)) {
        Log.v("MY_TAG", "Here's a log message");
    }

Pretty simple, right?

However, I'm having quite a bit of difficulty getting the Log.isLoggable("MY_TAG", Log.VERBOSE) to return true.

Per http://developer.android.com/reference/android/util/Log.html, I tried adding a local.prop file to the /data/ directory which looks like this:

log.tag.MY_TAG=VERBOSE

but no luck. I also tried:

System.setProperty("log.tag.MY_TAG", String.valueOf(Log.VERBOSE));

but that doesn't work either.

Any ideas on what I'm doing wrong here? I'm running Android 2.1-update1 on a Nexus 1 if that makes any difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我要还你自由 2024-10-08 08:59:37

尝试

adb shell setprop log.tag.MyAppTag VERBOSE

Try

adb shell setprop log.tag.MyAppTag VERBOSE
不羁少年 2024-10-08 08:59:37

似乎更高版本的 Android 希望 /data/local.prop 只能由 root 写入。 adb push 命令似乎最初创建文件时授予每个人读/写访问权限(因为默认文件掩码为 777)。 Android 会明智地忽略 /data/local.prop,因为这可能会带来安全风险。

我只尝试过Android 2.3.3和4.1.2。前者在读取全局可写的 local.prop 时没有任何问题,而后者似乎默默地忽略文件的内容。

按照原始问题中的描述创建 local.prop 文件:

log.tag.MY_TAG=VERBOSE

然后按如下方式将其推送到设备上似乎可以解决问题:

adb push local.prop /data/local.prop
adb shell chmod 644 /data/local.prop
adb shell chown root.root /data/local.prop
adb reboot

您可以仔细检查以确保 local 中的值.prop 是通过执行读取的:

adb shell getprop | grep log.tag

所以总而言之:

  • /data/local.prop 仅在启动期间读取。
  • 更高版本的 Android 似乎要求必须正确设置 /data/local.prop 文件的权限,否则将无法读取该文件。该文件必须只能由 root 写入。

使用 adb shell setprop log.tag.MyAppTag VERBOSE 也可以。问题是重启后属性值会丢失。

It seems that later versions of Android want /data/local.prop to be writable by root only. The adb push command appears to initially create files with granting everyone read/write access (because default file mask is 777). Android, wisely, ignores /data/local.prop since this can be a security risk.

I have only experimented with Android 2.3.3, and 4.1.2. The former has no issues with reading a local.prop that is world writable, while the latter appears to silently ignore the file's contents.

Creating a local.prop file as described in the original question:

log.tag.MY_TAG=VERBOSE

And then pushing it onto the device as follows seems to do the trick:

adb push local.prop /data/local.prop
adb shell chmod 644 /data/local.prop
adb shell chown root.root /data/local.prop
adb reboot

You can double check to make sure that the values in local.prop were read by executing:

adb shell getprop | grep log.tag

So in summary:

  • /data/local.prop is only read during boot.
  • Later versions of Android appear to require that the permissions on the /data/local.prop file must be properly set, or it will not be read. The file must be writable by root only.

Using adb shell setprop log.tag.MyAppTag VERBOSE also work. The issue is that the property values are lost after a reboot.

一世旳自豪 2024-10-08 08:59:37

一个重要的目标是不要在生产应用程序中留下大量日志调用,从而增加其大小,甚至可能影响其性能。

为此,我的建议是将这些常量放在将进行日志调用的每个类的顶部:

static final boolean DEBUG = false;
static final String TAG = "<MyClass>"

现在在您记录日志的位置,执行以下操作:

if (DEBUG) Log.v(TAG, "Something");

通过将 DEBUG 常量更改为来打开日志真的。 (如果您愿意,您可以拥有一个包含这些静态信息的类,供您的所有应用程序代码使用......这对于小型应用程序来说很有意义,但随着事情变得越来越大,最好决定打开哪些部分进行日志记录。)

通过这样做,当您使用 DEBUG = false 构建应用程序时,所有日志记录代码不仅不会执行,而且会完全从应用程序中删除。这很好,因为它允许您在代码中保留相当广泛的日志记录,以便在需要时打开,而不必担心这将如何影响您的运输应用程序的大小。基本上只需将日志放入您需要的任何地方,而不必担心将它们留在其中。

这是许多 Android 框架采用的方法。例如,Activity ManagerService。

这些常量位于顶部,并且基于它们散布着各种日志行。 (以及它各个方面的一堆其他子调试常量,因为这个文件大得可笑。)

An important goal is to not ship a production app with a ton of log calls left in it, increasing its size, and even possibly even impacting its performance.

To do this, my recommendation is to put these constants at the top of each class that is going to have log calls:

static final boolean DEBUG = false;
static final String TAG = "<MyClass>"

Now where you log, do this:

if (DEBUG) Log.v(TAG, "Something");

Turn on your logs by changing the DEBUG constant to true. (If you want, you could have one class with these statics for all of your app's code to use... That makes sense for a small app, but as things get large it is nice to decide which parts to turn logging on.)

By doing this, when you build your app with DEBUG = false, all of your logging code not only isn't executed, but is completely stripped out of your app. This is nice because it allows you to leave fairly extensive logging in your code to be turned on when you need it, without worrying about how that will impact the size of your shipping app. Basically just throw logs in wherever you need them and don't worry about leaving them in.

This is the approach that a lot of the Android framework takes. For example, the Activity ManagerService.

This has those constants at the top, and various log lines sprinkled throughout based on them. (And a bunch of other sub-debug constants for various aspects of it, since this file is ridiculously stupidly large.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文