为 Android 开发时如何将消息输出到 Eclipse 控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3007644/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:14:59  来源:igfitidea点击:

How to output messages to the Eclipse console when developing for Android

androideclipse

提问by Emanuil Rusev

How can I print messages (like a flag) to the Eclipse console (or log) when developing and debugging an Android app

开发和调试 Android 应用程序时,如何将消息(如标志)打印到 Eclipse 控制台(或日志)

采纳答案by m6tt

Rather than trying to output to the console, Logwill output to LogCat which you can find in Eclipse by going to: Window->Show View->Other…->Android->LogCat

不是尝试输出到控制台,而是Log输出到 LogCat,您可以通过以下方式在 Eclipse 中找到它:Window->Show View->Other...->Android->LogCat

Have a look at the reference for Log.

看看参考资料Log

The benefits of using LogCat are that you can print different colours depending on your log type, e.g.: Log.dprints blue, Log.eprints orange. Also you can filter by log tag, log message, process idand/or by application name. This is really useful when you just want to see your app's logs and keep the other system stuff separate.

使用 LogCat 的好处是您可以根据日志类型打印不同的颜色,例如:Log.d打印蓝色,Log.e打印橙色。您还可以按日志标签日志消息进程 ID和/或应用程序名称进行过滤。当您只想查看应用程序的日志并将其他系统内容分开时,这非常有用。

回答by Alex

Log.v("blah", "blah blah");

You need to add the android Log view in eclipse to see them. There are also other methods depending on the severity of the message (error, verbose, warning, etc..).

您需要在 eclipse 中添加 android Log 视图才能看到它们。还有其他方法取决于消息的严重性(错误、详细、警告等)。

回答by Tirtha

System.out.println()also outputs to LogCat. The benefit of using good old System.out.println()is that you can print an object like System.out.println(object)to the console if you need to check if a variable is initialized or not.

System.out.println()还输出到 LogCat。使用 good old 的好处System.out.println()是,System.out.println(object)如果您需要检查变量是否已初始化,您可以像控制台一样打印一个对象。

Log.d, Log.v, Log.wetc methods only allow you to print strings to the console and not objects. To circumvent this (if you desire), you must use String.format.

Log.d, Log.v,Log.w等方法只允许您将字符串打印到控制台而不是对象。要避免这种情况(如果您愿意),您必须使用String.format.

回答by Mahmud Ahsan

I use Log.d method also please import import android.util.Log;

我使用 Log.d 方法也请导入 import android.util.Log;

Log.d("TAG", "Message");

But please keep in mind that, when you want to see the debug messages then don't use Run As rather use "Debug As"then select Android Application. Otherwise you'll not see the debug messages.

但请记住,当您想查看调试消息时,不要使用运行方式,而是使用“调试方式”,然后选择 Android 应用程序。否则,您将看不到调试消息。

回答by Madhu

i use below log format for print my content in logCat

我使用以下日志格式在 logCat 中打印我的内容

Log.e("Msg","What you have to print");