如何在 Android 中启用/禁用日志级别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2018263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I enable/disable log levels in Android?
提问by d-man
I am having lots of logging statements to debug for example.
例如,我有很多日志语句要调试。
Log.v(TAG, "Message here");
Log.w(TAG, " WARNING HERE");
while deploying this application on device phone i want to turn off the verbose logging from where i can enable/disable logging.
在设备手机上部署此应用程序时,我想关闭详细日志记录,从那里我可以启用/禁用日志记录。
采纳答案by Cytown
A common way is to make an int named loglevel, and define its debug level based on loglevel.
一种常见的方法是创建一个名为 loglevel 的 int,并根据 loglevel 定义其调试级别。
public static int LOGLEVEL = 2;
public static boolean ERROR = LOGLEVEL > 0;
public static boolean WARN = LOGLEVEL > 1;
...
public static boolean VERBOSE = LOGLEVEL > 4;
if (VERBOSE) Log.v(TAG, "Message here"); // Won't be shown
if (WARN) Log.w(TAG, "WARNING HERE"); // Still goes through
Later, you can just change the LOGLEVEL for all debug output level.
稍后,您可以更改所有调试输出级别的 LOGLEVEL。
回答by Dave Webb
The Android Documentation says the following about Log Levels:
Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.
除非在开发过程中,否则不应将 Verbose 编译到应用程序中。调试日志被编译但在运行时被剥离。错误、警告和信息日志始终保留。
So you may want to consider stripping the log Verbose logging statements out, possibly using ProGuard as suggested in another answer.
因此,您可能需要考虑删除 log Verbose 日志语句,可能按照另一个答案中的建议使用 ProGuard。
According to the documentation, you can configure logging on a development device using System Properties. The property to set is log.tag.<YourTag>and it should be set to one of the following values: VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. More information on this is available in the documentation for the isLoggable()method.
根据文档,您可以使用系统属性在开发设备上配置日志记录。要设置的属性是log.tag.<YourTag>它应该设置为以下值之一:VERBOSE,DEBUG,INFO,WARN,ERROR,ASSERT,或SUPPRESS。 方法文档中提供了有关这方面的更多信息isLoggable()。
You can set properties temporarily using the setpropcommand. For example:
您可以使用该setprop命令临时设置属性。例如:
C:\android>adb shell setprop log.tag.MyAppTag WARN
C:\android>adb shell getprop log.tag.MyAppTag
WARN
Alternatively, you can specify them in the file '/data/local.prop' as follows:
或者,您可以在文件“/data/local.prop”中指定它们,如下所示:
log.tag.MyAppTag=WARN
Later versions of Android appear to require that /data/local.prop be read only. This file is read at boot time so you'll need to restart after updating it. If /data/local.propis world writable, it will likely be ignored.
更高版本的 Android似乎要求 /data/local.prop 是只读的。此文件在启动时读取,因此您需要在更新后重新启动。如果/data/local.prop是世界可写的,它很可能会被忽略。
Finally, you can set them programmatically using the System.setProperty()method.
最后,您可以使用System.setProperty()方法以编程方式设置它们。
回答by Christopher Orr
The easiest way is probably to run your compiled JAR through ProGuardbefore deployment, with a config like:
最简单的方法可能是在部署之前通过ProGuard运行编译的 JAR ,配置如下:
-assumenosideeffects class android.util.Log {
public static int v(...);
}
That will — aside from all the other ProGuard optimisations — remove any verbose log statements directly from the bytecode.
除了所有其他 ProGuard 优化之外,这将直接从字节码中删除任何冗长的日志语句。
回答by kdahlhaus
I took a simple route - creating a wrapper class that also makes use of variable parameter lists.
我采取了一条简单的路线——创建一个也使用可变参数列表的包装类。
public class Log{
public static int LEVEL = android.util.Log.WARN;
static public void d(String tag, String msgFormat, Object...args)
{
if (LEVEL<=android.util.Log.DEBUG)
{
android.util.Log.d(tag, String.format(msgFormat, args));
}
}
static public void d(String tag, Throwable t, String msgFormat, Object...args)
{
if (LEVEL<=android.util.Log.DEBUG)
{
android.util.Log.d(tag, String.format(msgFormat, args), t);
}
}
//...other level logging functions snipped
回答by Fortess Nsk
The better way is to use SLF4J API + some of its implementation.
更好的方法是使用 SLF4J API + 一些它的实现。
For Android applications you can use the following:
对于 Android 应用程序,您可以使用以下内容:
- Android Loggeris the lightweight but easy-to-configure SLF4J implementation (< 50 Kb).
- LOGBack is the most powerful and optimized implementation but its size is about 1 Mb.
- Any other by your taste: slf4j-android, slf4android.
- Android Logger是轻量级但易于配置的 SLF4J 实现(< 50 Kb)。
- LOGBack 是最强大和优化的实现,但它的大小约为 1 Mb。
- 其他任何你喜欢的:slf4j-android、slf4android。
回答by Diego Torres Milano
You should use
你应该使用
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "my log message");
}
回答by larham1
Stripping out the logging with proguard (see answer from @Christopher ) was easy and fast, but it caused stack traces from production to mismatch the source if there was any debug logging in the file.
使用 proguard 删除日志记录(请参阅@Christopher 的回答)既简单又快速,但如果文件中有任何调试日志记录,它会导致来自生产的堆栈跟踪与源不匹配。
Instead, here's a technique that uses different logging levels in development vs. production, assuming that proguard is used only in production. It recognizes production by seeing if proguard has renamed a given class name (in the example, I use "com.foo.Bar"--you would replace this with a fully-qualified class name that you know will be renamed by proguard).
相反,这里有一种技术,它在开发和生产中使用不同的日志记录级别,假设 proguard 仅在生产中使用。它通过查看 proguard 是否已重命名给定的类名来识别生产(在示例中,我使用“com.foo.Bar”——您将使用完全限定的类名替换它,您知道该类名将被 proguard 重命名)。
This technique makes use of commons logging.
这种技术利用了公共日志记录。
private void initLogging() {
Level level = Level.WARNING;
try {
// in production, the shrinker/obfuscator proguard will change the
// name of this class (and many others) so in development, this
// class WILL exist as named, and we will have debug level
Class.forName("com.foo.Bar");
level = Level.FINE;
} catch (Throwable t) {
// no problem, we are in production mode
}
Handler[] handlers = Logger.getLogger("").getHandlers();
for (Handler handler : handlers) {
Log.d("log init", "handler: " + handler.getClass().getName());
handler.setLevel(level);
}
}
回答by Rolf Kulemann
Log4j or slf4j can also be used as logging frameworks in Android together with logcat. See the project android-logging-log4jor log4j support in android
Log4j 或 slf4j 也可以与 logcat 一起用作 Android 中的日志框架。在android中查看项目android-logging-log4j或log4j support
回答by Eric Weyant
There is a tiny drop-in replacement for the standard android Log class - https://github.com/zserge/log
标准 android Log 类有一个很小的替代品 - https://github.com/zserge/log
Basically all you have to do is to replace imports from android.util.Logto trikita.log.Log. Then in your Application.onCreate()or in some static initalizer check for the BuilConfig.DEBUGor any other flag and use Log.level(Log.D)or Log.level(Log.E)to change the minimal log level. You can use Log.useLog(false)to disable logging at all.
基本上你所要做的就是替换从android.util.Logto 的导入trikita.log.Log。然后在您的Application.onCreate()或某些静态初始化程序中检查BuilConfig.DEBUG或任何其他标志并使用Log.level(Log.D)或Log.level(Log.E)更改最小日志级别。您可以Log.useLog(false)完全禁用日志记录。
回答by Donkey
May be you can see this Log extension class: https://github.com/dbauduin/Android-Tools/tree/master/logs.
也许你可以看到这个日志扩展类:https: //github.com/dbauduin/Android-Tools/tree/master/logs。
It enables you to have a fine control on logs. You can for example disable all logs or just the logs of some packages or classes.
它使您可以对日志进行精细控制。例如,您可以禁用所有日志或仅禁用某些包或类的日志。
Moreover, it adds some useful functionalities (for instance you don't have to pass a tag for each log).
此外,它还添加了一些有用的功能(例如,您不必为每个日志传递标签)。

