一、背景
對(duì)于有些公司對(duì)安全性要求比較高,可能不會(huì)使用第三方(例如 Bugly)柱恤。這個(gè)是需要自己記錄日志排查現(xiàn)場(chǎng)問(wèn)題,而且有時(shí)候客戶是內(nèi)網(wǎng)下使用找爱,沒(méi)法把信息發(fā)送到自己的服務(wù)器梗顺。那么,本地日志文件保存是很有必要的车摄。
二寺谤、實(shí)現(xiàn)
思路:就是可以同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 {
//保存到應(yīng)用目錄下 todo 需要測(cè)試
pathLogcat = context.getFilesDir().getAbsolutePath() + "/logInfo/";
}
File file = new File(pathLogcat);
if (!file.exists()) {
file.mkdirs();
LogUtil.e(TAG, "創(chuàng)建文件夾");
}
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();
//錯(cuò)誤日志文件名稱
String fileName = "hikvison-log" + timeMillis + ".log";
try {
outputStream = new FileOutputStream(new File(dir,fileName ));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//顯示當(dāng)前mPid程序的日志等級(jí) 日志等級(jí):*:v , *:d , *:w , *:e , *:f , *:s
// cmds = "logcat *:e *:w | grep \"(" + mPid + ")\"";
cmds = "logcat | grep \"(" + mPid + ")\"";//打印所有日志信息
// cmds = "logcat -s way";//打印標(biāo)簽過(guò)濾信息
// 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;
}
}
}
}
}