【Android开发入门】androidstudio控制台打印输出日志

更新时间:2023-07-08 14:45:32 阅读: 评论:0

【Android开发⼊门】androidstudio控制台打印输出⽇志
有些情况下,不⽅便使⽤断点的⽅式来调试,⽽是希望在控制台打印输出⽇志,使⽤过Eclip的同学都知道Java可以使⽤
System.out.println(""); 来在控制台打印输出⽇志,但是在android studio中却是不⾏的,还是有差别的,那应该⽤什么呢?
android.util.Log
在调试代码的时候我们需要查看调试信息,那我们就需要⽤ Log类。
android.util.Log常⽤的⽅法有以下5个:Log.v() Log.d()Log.i() Log.w() 以及Log.e()。根据⾸字母对
应VERBOSE,DEBUG,INFO, WARN,ERROR。
春天来1、Log.v 的调试颜⾊为⿊⾊的,任何消息都会输出,这⾥的v代表verbo啰嗦的意思,平时使⽤就是Log.v("","");
2、Log.d的输出颜⾊是蓝⾊的,仅输出debug调试的意思,但他会输出上层的信息,过滤起来可以通过DDMS的Logcat标签来选择.
3、Log.i的输出为绿⾊,⼀般提⽰性的消息information,它不会输出Log.v和Log.d的信息,但会显⽰i、w和e的信息
4、Log.w的意思为橙⾊,可以看作为warning警告,⼀般需要我们注意优化Android代码,同时选择它后还会输出Log.e的信息。
5、Log.e为红⾊,可以想到error错误,这⾥仅显⽰红⾊的错误信息,这些错误就需要我们认真的分析,查看栈的信息了。
注意:不同的打印⽅法在使⽤时都是某个⽅法带上(String tag, String msg)参数,tag表⽰的是打印信息的标签,msg表⽰的是需要打印的信息。
Log.java类
禁止燃放烟花爆竹/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licend under the Apache Licen, Version 2.0 (the "Licen");
* you may not u this file except in compliance with the Licen.
* You may obtain a copy of the Licen at
*
*      www.apache/licens/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licen is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licen for the specific language governing permissions and
* limitations under the Licen.
*/
package android.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.UnknownHostException;
/**
* Mock Log implementation for testing on non android host.
*/
public final class Log {
/**
黄盖字
* Priority constant for the println method; u Log.v.
*/
public static final int VERBOSE = 2;
/**
* Priority constant for the println method; u Log.d.
*/
public static final int DEBUG = 3;
* Priority constant for the println method; u Log.i.
*/
public static final int INFO = 4;
关于重阳节的古诗/**
* Priority constant for the println method; u Log.w.
*/
public static final int WARN = 5;
/**
* Priority constant for the println method;
*/
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 Ud 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 Ud 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 Ud 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 Ud 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 Ud 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 Ud 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 Ud 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 Ud 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 Ud 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
直5
*/
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 Ud 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 Ud 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.getCau();
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
tr.printStackTrace(pw);
pw.flush();
String();
}
/**
* Low-level logging call.
* @param priority The priority/type of this log message
* @param tag Ud 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("unud")
public static int println(int bufID,
int priority, String tag, String msg) {
return 0;
}
}
有⼩伙伴问我怎么在Android Studio中查看Log类?
在代码编辑器中,将光标定位在Log上,然后按下快捷键:Ctrl+B,就可以打开类⽂件了。Logcat窗⼝
我还真不知道这个窗⼝能不能完全关闭掉,它好像是和Android Monitor 是⼀体的。如下图:
我们可以在logcat上按下然后拖动⿏标,把它拉出来,弹出⼀个单独的窗⼝。

本文发布于:2023-07-08 14:45:32,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1085672.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:需要   代码   信息   输出   关闭   调试
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图