日志级别:error > warn > information > debug > verbose
日志类型
//Log.e():error级别
Log.e("LogActivity","我是错误日志");
//Log.w():warn级别
Log.w("LogActivity","我是警告日志");
//Log.i():information级别
Log.i("LogActivity","我是信息日志");
//Log.d():debug级别
Log.d("LogActivity","我是调试日志");
//Log.v():verbose级别
Log.v("LogActivity","我是冗余日志");
查看日志:控制台下导航栏Logcat
[例子]
log_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/l_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/l_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" />
<Button
android:id="@+id/l_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
LogActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LogActivity extends AppCompatActivity {
//定义变量
EditText lUser;
EditText lPwd;
Button lBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log);
//初始化变量
lUser=findViewById(R.id.l_user);
lPwd=findViewById(R.id.l_pwd);
lBtn=findViewById(R.id.l_btn);
//监听按钮点击事件
lBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取信息
String user =lUser.getText().toString().trim();
String passwd =lPwd.getText().toString().trim();
try {
int a=2/0;
}catch (Exception e){
//Log.e():error级别
Log.e("LogActivity",e.toString());
}
}
});
}
}