Android本地文件日志记录

2020-12-24  本文已影响0人  旺仔_100

一、背景
对于有些公司对安全性要求比较高,可能不会使用第三方(例如 Bugly)。这个是需要自己记录日志排查现场问题,而且有时候客户是内网下使用,没法把信息发送到自己的服务器。那么,本地日志文件保存是很有必要的。

二、实现
思路:就是可以同cmd把Logcat所有的日志都写到文件中。它可以直接把所有的信息都写入到文件中,包括崩溃信息,Android Log信息,也包括flutter的崩溃信息,print信息等,只要是在Android Studio中Logcat输出的信息都能保存都本地。

三、代码

package hik.fp.baseline.port.common.util;

import android.content.Context;
import android.os.Environment;

import com.ezviz.stream.LogUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Description: 把日志写到本地
 * @date 2020/12/23 
 */
public class LogcatHelper {
    public static final String TAG = "LogcatHelper";
    private static String pathLogcat;
    private static volatile LogcatHelper mInstance;
    String cmds = null;
    private Process logcatProc;
    private LogDumper mLogDumper;
    private int mPid;

    private LogcatHelper(Context context) {
      init(context);
      mPid = android.os.Process.myPid();
    }

    public static LogcatHelper newInstance(Context context) {
        if (mInstance == null) {
            synchronized (LogcatHelper.class) {
                if (mInstance == null) {
                    mInstance = new LogcatHelper(context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 初始化目录
     */
    private void init(Context context) {

//        String path = "";
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //保存在SD卡中
            pathLogcat = Environment.getExternalStorageDirectory().getPath() + "/logInfo/";
        } else {
            //保存到应用目录下 todo 需要测试
            pathLogcat = context.getFilesDir().getAbsolutePath() + "/logInfo/";
        }

        File file = new File(pathLogcat);
        if (!file.exists()) {
            file.mkdirs();
            LogUtil.e(TAG, "创建文件夹");
        }
        LogUtil.e(TAG, pathLogcat);
    }

    public void start(){
        if (mLogDumper == null){
            mLogDumper = new LogDumper(String.valueOf(mPid),pathLogcat);
        }
        mLogDumper.start();
    }

    public void stop(){
        if (mLogDumper != null){
            mLogDumper.stopLogs();
            mLogDumper = null;
        }
    }

    private class LogDumper extends Thread {
        private String mPid;
        private FileOutputStream outputStream = null;
        private BufferedReader mReader = null;
        private boolean mIsRunning = true;

        public LogDumper(String pid, String dir) {
            mPid = pid;
            long timeMillis = System.currentTimeMillis();
            //错误日志文件名称
            String fileName = "hikvison-log" + timeMillis + ".log";
            try {
                outputStream = new FileOutputStream(new File(dir,fileName ));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            //显示当前mPid程序的日志等级  日志等级:*:v , *:d , *:w , *:e , *:f , *:s
            // cmds = "logcat *:e *:w | grep \"(" + mPid + ")\"";
             cmds = "logcat  | grep \"(" + mPid + ")\"";//打印所有日志信息
            // cmds = "logcat -s way";//打印标签过滤信息
//            cmds = "logcat *:e *:i | grep \"(" + mPid + ")\"";
        }

        public void stopLogs(){
            mIsRunning = false;
        }

        @Override
        public void run() {
            try {
                logcatProc = Runtime.getRuntime().exec(cmds);
                mReader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()),1024);
                String line = null;
                while (mIsRunning && (line = mReader.readLine()) != null){
                    if (!mIsRunning){
                        break;
                    }
                    if (line.length() == 0){
                        continue;
                    }
                    if (outputStream != null && line.contains(mPid)){
                        outputStream.write((line+"\n").getBytes());
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (logcatProc != null){
                    logcatProc.destroy();
                    logcatProc = null;
                }
                if (mReader != null){
                    try {
                        mReader.close();
                        mReader = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (outputStream != null){
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    outputStream = null;
                }
            }
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读