Android 日志记录级别
我在配置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
Try
似乎更高版本的 Android 希望
/data/local.prop
只能由 root 写入。adb push
命令似乎最初创建文件时授予每个人读/写访问权限(因为默认文件掩码为777
)。 Android 会明智地忽略/data/local.prop
,因为这可能会带来安全风险。我只尝试过Android 2.3.3和4.1.2。前者在读取全局可写的
local.prop
时没有任何问题,而后者似乎默默地忽略文件的内容。按照原始问题中的描述创建
local.prop
文件:然后按如下方式将其推送到设备上似乎可以解决问题:
您可以仔细检查以确保
local 中的值.prop
是通过执行读取的:所以总而言之:
/data/local.prop
仅在启动期间读取。/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. Theadb push
command appears to initially create files with granting everyone read/write access (because default file mask is777
). 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:And then pushing it onto the device as follows seems to do the trick:
You can double check to make sure that the values in
local.prop
were read by executing:So in summary:
/data/local.prop
is only read during boot./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.一个重要的目标是不要在生产应用程序中留下大量日志调用,从而增加其大小,甚至可能影响其性能。
为此,我的建议是将这些常量放在将进行日志调用的每个类的顶部:
现在在您记录日志的位置,执行以下操作:
通过将
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:
Now where you log, do this:
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.)