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

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="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/> <uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
<!-- Android11新增 --> <!-- Android11新增 -->
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<!-- <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />--> <!-- <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />-->
<application <application

View File

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

View File

@ -11,43 +11,45 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
public class LogUtils { public class LogUtils {
/**
* 采集所有日志
*/
public static void start(Context context) { public static void start(Context context) {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
String[] exec = new String[]{ String[] exec = new String[]{"logcat", "-c"};
"logcat",
"-c"
};
Runtime.getRuntime().exec(exec).waitFor(); Runtime.getRuntime().exec(exec).waitFor();
exec = new String[]{ exec = new String[]{"logcat", "-v", "-d", "time"};
"logcat",
"-v",
"-d",
"time"
};
Process process = Runtime.getRuntime().exec(exec); Process process = Runtime.getRuntime().exec(exec);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line; String line;
PrintWriter writer = null; 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); File dir = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator);
if (dir.listFiles() != null) { if (dir.listFiles() != null) {
for (File file : dir.listFiles()) { for (File file : dir.listFiles()) {
if (!file.getName().endsWith(today+".log") && !file.getName().endsWith("error.log")) { if (!file.getName().endsWith(today + ".log") && !"error.log".equals(file.getName())) {
String fileName = file.getName().replace(title, "");
if (isDelLog(fileName)) {
file.delete(); file.delete();
} }
} }
} }
}
File saveFile = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator + today + ".log"); File saveFile = new File(context.getDir("files", Context.MODE_PRIVATE).getAbsolutePath() + File.separator + today + ".log");
FileOutputStream os = new FileOutputStream(saveFile); FileOutputStream os = new FileOutputStream(saveFile);
writer = new PrintWriter(os); writer = new PrintWriter(os);
@ -56,10 +58,7 @@ public class LogUtils {
} }
writer.flush(); writer.flush();
writer.close(); writer.close();
exec = new String[]{ exec = new String[]{"logcat", "-c"};
"logcat",
"-c"
};
Runtime.getRuntime().exec(exec).waitFor(); Runtime.getRuntime().exec(exec).waitFor();
bufferedReader.close(); bufferedReader.close();
start(context); 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; 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 androidx.core.content.PermissionChecker.PERMISSION_GRANTED;
import static java.text.DateFormat.DEFAULT; import static java.text.DateFormat.DEFAULT;
@ -29,6 +30,8 @@ import com.yunbao.common.CommonAppConfig;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; 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()); 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 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()) { if (file.exists()) {
Intent share = new Intent(Intent.ACTION_SEND); Intent share = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
@ -159,6 +161,22 @@ public class LogUtil {
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(share, "分享文件")); 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();
}
} }
} }