全量日志采集系统改为保存三天内的

This commit is contained in:
zlzw 2022-11-12 11:32:11 +08:00
parent e9a8a4dc02
commit 3f9cf21b8f
4 changed files with 53 additions and 19 deletions

View File

@ -80,6 +80,7 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
<!-- Android11新增 -->
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<!-- <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />-->
<application

View File

@ -138,7 +138,7 @@ public class AppContext extends CommonAppContext {
CrashSaveBean.getInstance().setStartTime(System.currentTimeMillis());
registerError();
registerFirebaseCrash();
LogUtils.start(this);
// LogUtils.start(this);
sInstance = this;
L.setDeBug(BuildConfig.DEBUG);
AppEventsLogger.activateApp(this);

View File

@ -11,40 +11,42 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;
public class LogUtils {
/**
* 采集所有日志
*/
public static void start(Context context) {
new Thread(new Runnable() {
@Override
public void run() {
try {
String[] exec = new String[]{
"logcat",
"-c"
};
String[] exec = new String[]{"logcat", "-c"};
Runtime.getRuntime().exec(exec).waitFor();
exec = new String[]{
"logcat",
"-v",
"-d",
"time"
};
exec = new String[]{"logcat", "-v", "-d", "time"};
Process process = Runtime.getRuntime().exec(exec);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
PrintWriter writer = null;
String today ="[PDLIVE]"+CommonAppConfig.getInstance().getUid()+"_"+ SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).format(new Date());
String title = "[PDLIVE]" + CommonAppConfig.getInstance().getUid() + "_";
String today = title + SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).format(new Date());
File dir = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator);
if (dir.listFiles() != null) {
for (File file : dir.listFiles()) {
if (!file.getName().endsWith(today+".log") && !file.getName().endsWith("error.log")) {
file.delete();
if (!file.getName().endsWith(today + ".log") && !"error.log".equals(file.getName())) {
String fileName = file.getName().replace(title, "");
if (isDelLog(fileName)) {
file.delete();
}
}
}
}
@ -56,10 +58,7 @@ public class LogUtils {
}
writer.flush();
writer.close();
exec = new String[]{
"logcat",
"-c"
};
exec = new String[]{"logcat", "-c"};
Runtime.getRuntime().exec(exec).waitFor();
bufferedReader.close();
start(context);
@ -71,4 +70,20 @@ public class LogUtils {
}
/**
* 判断是否是要删除的日志
**/
private static boolean isDelLog(String time) {
try {
Calendar timeCal = Calendar.getInstance(Locale.CHINA);
timeCal.setTime(Objects.requireNonNull(SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).parse(time)));
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.setTime(new Date());
calendar.add(Calendar.DATE, -3);
return calendar.after(timeCal);
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
}

View File

@ -1,5 +1,6 @@
package com.yunbao.common.utils;
import static android.provider.Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION;
import static androidx.core.content.PermissionChecker.PERMISSION_GRANTED;
import static java.text.DateFormat.DEFAULT;
@ -29,6 +30,8 @@ import com.yunbao.common.CommonAppConfig;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
@ -144,7 +147,6 @@ public class LogUtil {
}
String today ="[PDLIVE]"+CommonAppConfig.getInstance().getUid()+"_"+ SimpleDateFormat.getDateInstance(DEFAULT, Locale.CHINA).format(new Date());
File file = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator + today + ".log");
file.renameTo(new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+file.getName()));
if (file.exists()) {
Intent share = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
@ -159,6 +161,22 @@ public class LogUtil {
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(share, "分享文件"));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if(!Environment.isExternalStorageManager()){
Intent intent=new Intent(ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
context.startActivity(intent);
}
}
File out = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + file.getName());
file.renameTo(out);
Log.i("文件", "shareFile: "+out.getAbsolutePath()+" "+out.exists());
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Files.copy(file.toPath(),out.toPath());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}