如何将输出写入 Android 中的日志?

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

How do I write outputs to the Log in Android?

androidlogginglogcat

提问by Janusz

I want to write some debugging output to the log to review it with logcat.

我想将一些调试输出写入日志以使用 logcat 进行查看。

If I write something to System.out this is already displayed in logcat.

如果我向 System.out 写一些东西,它已经显示在 logcat 中。

What is the clean way to write to the log and add levels and tags to my output?

写入日志并向输出添加级别和标签的干净方法是什么?

回答by Erich Douglass

Look into android.util.Log. It lets you write to the log with various log levels, and you can specify different tags to group the output. For example

调查一下android.util.Log。它允许您使用各种日志级别写入日志,并且您可以指定不同的标签来对输出进行分组。例如

Log.w("myApp", "no network");

will output a warning with the tag myApp and the message no network.

将输出带有标签 myApp 和消息 no network 的警告。

回答by user1767754

The Tag is just used to easily find your output, because the Output of LogCatcan be sometimes very long. You can define somewhere in your class:

Tag 只是用来轻松找到您的输出,因为LogCat的输出有时可能很长。您可以在类中的某处定义:

private static final String TAG = "myApp";

private static final String TAG = "myApp";

and use it when debugging

并在调试时使用它

Log.v(TAG, "did something");

Log.v(TAG, "做了某事");

enter image description here

在此处输入图片说明

You can apply as well a Filter to only search for the tag.

您也可以应用过滤器来仅搜索标签。

回答by CommonsWare

Use android.util.Logand the static methods defined there (e.g., e(), w()).

使用android.util.Log和那里定义的静态方法(例如e(),,w())。

回答by Atreya Rath

import android.util.Log;

and then

进而

Log.i("the your message will go here"); 

回答by Dnyaneshwar Panchal

Please see the logs as this way,

请以这种方式查看日志,

Log.e("ApiUrl = ", "MyApiUrl") (error)
Log.w("ApiUrl = ", "MyApiUrl") (warning)
Log.i("ApiUrl = ", "MyApiUrl") (information)
Log.d("ApiUrl = ", "MyApiUrl") (debug)
Log.v("ApiUrl = ", "MyApiUrl") (verbose)

回答by Arda

You can use my libary called RDALogger. Here is github link.

您可以使用我的名为 RDALogger 的库。这是 github链接

With this library, you can log your message with method name/class name/line number and anchor link. With this link, when you click log, screen goes to this line of code.

使用此库,您可以使用方法名称/类名称/行号和锚链接记录您的消息。通过此链接,当您单击日志时,屏幕将转到这行代码。

To use library, you must do implementations below.

要使用库,您必须在下面进行实现。

in root level gradle

在根级gradle

allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

in app level gradle

在应用程序级别的gradle

dependencies {
            implementation 'com.github.ardakaplan:RDALogger:1.0.0'
    }

For initializing library, you should start like this (in Application.class or before first use)

对于初始化库,你应该这样开始(在 Application.class 或第一次使用之前)

RDALogger.start("TAG NAME").enableLogging(true);

And than you can log whatever you want;

而且你可以记录任何你想要的东西;

    RDALogger.info("info");
    RDALogger.debug("debug");
    RDALogger.verbose("verbose");
    RDALogger.warn("warn");
    RDALogger.error("error");
    RDALogger.error(new Throwable());
    RDALogger.error("error", new Throwable());

And finally output shows you all you want (class name, method name, anchor link, message)

最后输出显示您想要的所有内容(类名、方法名、锚链接、消息)

08-09 11:13:06.023 20025-20025/com.ardakaplan.application I/Application: IN CLASS : (ENApplication.java:29)   ///   IN METHOD : onCreate
    info

回答by Vickie Kangare

String one = object.getdata();
Log.d(one,"");

回答by Tushar Pandey

Recently I found this approach to writing logs in android, which I think is super awesome.

最近我发现了这种在android中写日志的方法,我觉得超级棒。

public static final boolean FORCED_LOGGING = true;
private static final int CALLER_STACK_INDEX = 3;

public static void showLogs(String message) {
        if (FORCED_LOGGING) {
            StackTraceElement caller = Thread.currentThread().getStackTrace()[CALLER_STACK_INDEX];

            String fullClassName = caller.getClassName();
            String className = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
            String methodName = caller.getMethodName();
            int lineNumber = caller.getLineNumber();

            Log.i("*** " + className + "." + methodName + "():" + lineNumber + "\n" , message);
        }
    }