程序员潇然 发表于 2022-7-28 20:49:45

Android 的日志工具Log logcat说明 使用

不管是什么语言,不管是开发什么项目,日志是必须的。

Android 中的日志工具类是Log(android.util.Log),这个类中提供了如下5 个方法来供我们打印日志。

Android中的日志,与java中的并无二致,也都是这样几个级别,无外乎实现不同而已。

### 分类

#### `Log.v()`

用于打印那些最为琐碎的、意义最小的日志信息。对应级别verbose ,是Android 日志里面级别最低的一种。

#### `Log.d()`

用于打印一些调试信息,这些信息对你调试程序和分析问题应该是有帮助的。

对应级别debug ,比verbose 高一级。

#### `Log.i()`

用于打印一些比较重要的数据,这些数据应该是你非常想看到的、可以帮你分析用户行为的数据。对应级别info ,比debug 高一级。

#### `Log.w()`

用于打印一些警告信息,提示程序在这个地方可能会有潜在的风险,最好去修复一下这些出现警告的地方。

对应级别warn ,比info 高一级。

#### `Log.e()`

用于打印程序中的错误信息,比如程序进入了catch语句中。

当有错误信息打印出来的时候,一般代表你的程序出现严重问题了,必须尽快修复。对应级别error ,比

warn 高一级。

当然,还有些他们自身的方法重载。

```java
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package android.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.UnknownHostException;

/**
* Mock Log implementation for testing on non android host.
*/
public final class Log {

    /**
   * Priority constant for the println method; use Log.v.
   */
    public static final int VERBOSE = 2;

    /**
   * Priority constant for the println method; use Log.d.
   */
    public static final int DEBUG = 3;

    /**
   * Priority constant for the println method; use Log.i.
   */
    public static final int INFO = 4;

    /**
   * Priority constant for the println method; use Log.w.
   */
    public static final int WARN = 5;

    /**
   * Priority constant for the println method; use Log.e.
   */
    public static final int ERROR = 6;

    /**
   * Priority constant for the println method.
   */
    public static final int ASSERT = 7;

    private Log() {
    }

    /**
   * Send a {@link #VERBOSE} log message.
   * @param tag Used to identify the source of a log message.It usually identifies
   *      the class or activity where the log call occurs.
   * @param msg The message you would like logged.
   */
    public static int v(String tag, String msg) {
      return println(LOG_ID_MAIN, VERBOSE, tag, msg);
    }

    /**
   * Send a {@link #VERBOSE} log message and log the exception.
   * @param tag Used to identify the source of a log message.It usually identifies
   *      the class or activity where the log call occurs.
   * @param msg The message you would like logged.
   * @param tr An exception to log
   */
    public static int v(String tag, String msg, Throwable tr) {
      return println(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr));
    }

    /**
   * Send a {@link #DEBUG} log message.
   * @param tag Used to identify the source of a log message.It usually identifies
   *      the class or activity where the log call occurs.
   * @param msg The message you would like logged.
   */
    public static int d(String tag, String msg) {
      return println(LOG_ID_MAIN, DEBUG, tag, msg);
    }

    /**
   * Send a {@link #DEBUG} log message and log the exception.
   * @param tag Used to identify the source of a log message.It usually identifies
   *      the class or activity where the log call occurs.
   * @param msg The message you would like logged.
   * @param tr An exception to log
   */
    public static int d(String tag, String msg, Throwable tr) {
      return println(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr));
    }

    /**
   * Send an {@link #INFO} log message.
   * @param tag Used to identify the source of a log message.It usually identifies
   *      the class or activity where the log call occurs.
   * @param msg The message you would like logged.
   */
    public static int i(String tag, String msg) {
      return println(LOG_ID_MAIN, INFO, tag, msg);
    }

    /**
   * Send a {@link #INFO} log message and log the exception.
   * @param tag Used to identify the source of a log message.It usually identifies
   *      the class or activity where the log call occurs.
   * @param msg The message you would like logged.
   * @param tr An exception to log
   */
    public static int i(String tag, String msg, Throwable tr) {
      return println(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr));
    }

    /**
   * Send a {@link #WARN} log message.
   * @param tag Used to identify the source of a log message.It usually identifies
   *      the class or activity where the log call occurs.
   * @param msg The message you would like logged.
   */
    public static int w(String tag, String msg) {
      return println(LOG_ID_MAIN, WARN, tag, msg);
    }

    /**
   * Send a {@link #WARN} log message and log the exception.
   * @param tag Used to identify the source of a log message.It usually identifies
   *      the class or activity where the log call occurs.
   * @param msg The message you would like logged.
   * @param tr An exception to log
   */
    public static int w(String tag, String msg, Throwable tr) {
      return println(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr));
    }

    /*
   * Send a {@link #WARN} log message and log the exception.
   * @param tag Used to identify the source of a log message.It usually identifies
   *      the class or activity where the log call occurs.
   * @param tr An exception to log
   */
    public static int w(String tag, Throwable tr) {
      return println(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));
    }

    /**
   * Send an {@link #ERROR} log message.
   * @param tag Used to identify the source of a log message.It usually identifies
   *      the class or activity where the log call occurs.
   * @param msg The message you would like logged.
   */
    public static int e(String tag, String msg) {
      return println(LOG_ID_MAIN, ERROR, tag, msg);
    }

    /**
   * Send a {@link #ERROR} log message and log the exception.
   * @param tag Used to identify the source of a log message.It usually identifies
   *      the class or activity where the log call occurs.
   * @param msg The message you would like logged.
   * @param tr An exception to log
   */
    public static int e(String tag, String msg, Throwable tr) {
      return println(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr));
    }

    /**
   * Handy function to get a loggable stack trace from a Throwable
   * @param tr An exception to log
   */
    public static String getStackTraceString(Throwable tr) {
      if (tr == null) {
            return "";
      }

      // This is to reduce the amount of log spew that apps do in the non-error
      // condition of the network being unavailable.
      Throwable t = tr;
      while (t != null) {
            if (t instanceof UnknownHostException) {
                return "";
            }
            t = t.getCause();
      }

      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      tr.printStackTrace(pw);
      pw.flush();
      return sw.toString();
    }

    /**
   * Low-level logging call.
   * @param priority The priority/type of this log message
   * @param tag Used to identify the source of a log message.It usually identifies
   *      the class or activity where the log call occurs.
   * @param msg The message you would like logged.
   * @return The number of bytes written.
   */
    public static int println(int priority, String tag, String msg) {
      return println(LOG_ID_MAIN, priority, tag, msg);
    }

    /** @hide */ public static final int LOG_ID_MAIN = 0;
    /** @hide */ public static final int LOG_ID_RADIO = 1;
    /** @hide */ public static final int LOG_ID_EVENTS = 2;
    /** @hide */ public static final int LOG_ID_SYSTEM = 3;
    /** @hide */ public static final int LOG_ID_CRASH = 4;

    /** @hide */ @SuppressWarnings("unused")
    public static int println(int bufID,
            int priority, String tag, String msg) {
      return 0;
    }
}

```

### tag

通过上面的log代码可以看出来,都有一个`String` 的参数,`tag`

这其实就是一个标记,写java的时候,经常可能会自己加一些字符串头,用来标记识别,这个tag就是这个作用。

### LogCat

!(data/attachment/forum/202207/28/204645dy8peoaot44g0fy3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")

logcat提供了比较方面的检索工具的形式。

可以筛选模拟器;

可以筛选tag标签;

可以筛选日志级别;

可以自定义关键词、正则;

可以设置过滤器;

#### 过滤器

Show only selected application 表示只显示当前选中程序的日志;

Firebase 是Google 提供的一个开发者工具和基础架构平台;

No Filters 没有过滤器,会把所有的日志都显示出来;

还可以自定义过滤器,点击`Edit Filter Configuration`

!(data/attachment/forum/202207/28/204844cb323ashdxq2y09x.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")


!(data/attachment/forum/202206/16/141330jha7st9soow8772i.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "common_log.png")
`转载务必注明出处:程序员潇然,疯狂的字节X,https://crazybytex.com/thread-99-1-1.html `
页: [1]
查看完整版本: Android 的日志工具Log logcat说明 使用