How to automatically print the class name and method name (line number) log of MainActivity.onCreate(line:37) in the Android program? Log log printing tool class

1. Look at the effect picture first, as long as we click on the content in the red box, it will automatically jump to a specific line
insert image description here
2. The content of the main activity MainActivity2 class code is as follows

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.myapplication001.R;

public class MainActivity2 extends AppCompatActivity {
    
    

    Button btn;

    @SuppressLint({
    
    "MissingInflatedId", "WrongViewCast"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        btn=findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                LogUtils.d("我在测试打印日志,自动定位到具体的类中的方法的哪一行");
            }
        });
    }

}

3. The activity_main2 layout code of the main activity MainActivity2 class is as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/refreshLa"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开启日志打印" />

</LinearLayout>

3. Log log printing tool class LogUtils, the code content is as follows

import android.util.Log;

/**
 * @作者(author): WR
 * @创建时间(date): 2020/11/26
 * 日子工具类。会自动定位到类文件所在的行数
 */
public class LogUtils {
    
    
    static String className;//类名
    static String methodName;//方法名
    static int lineNumber;//行数
    static boolean isDebug = true;
    /*获取到堆栈轨迹的两种方法
    Thread.currentThread().getStackTrace()
    new Throwable().getStackTrace()
    */

    private static String createLog(String log) {
    
    
        StringBuffer buffer = new StringBuffer();
        buffer.append(methodName);
        buffer.append("数据:");
        buffer.append("(").append(className).append(":").append(lineNumber).append("):");
        buffer.append(log);
        return buffer.toString();
    }

    /**
     * 获取文件名、方法名、所在行数
     *
     * @param sElements
     */
    private static void getMethodNames(StackTraceElement[] sElements) {
    
    
        //获取类名
        className = sElements[1].getFileName();
        //方法名
        methodName = sElements[1].getMethodName();
        //行数
        lineNumber = sElements[1].getLineNumber();
    }

    public static void e(String message) {
    
    
        if (!isDebug)
            return;
        getMethodNames(new Throwable().getStackTrace());
        Log.e(className, createLog(message));
    }

    public static void i(String message) {
    
    
        if (!isDebug)
            return;
        getMethodNames(new Throwable().getStackTrace());
        Log.i(className, createLog(message));
    }

    public static void d(String message) {
    
    
        if (!isDebug)
            return;
        getMethodNames(new Throwable().getStackTrace());
        Log.d(className, createLog(message));
    }

    public static void v(String message) {
    
    
        if (!isDebug)
            return;
        getMethodNames(new Throwable().getStackTrace());
        Log.v(className, createLog(message));
    }

    public static void w(String message) {
    
    
        if (!isDebug)
            return;
        getMethodNames(new Throwable().getStackTrace());
        Log.w(className, createLog(message));
    }

    public static void setIsDebug(boolean isDebug) {
    
    
        LogUtils.isDebug = isDebug;
    }

}

Guess you like

Origin blog.csdn.net/qq_36570506/article/details/130446652