public class SaveLogUtils {
private static volatile SaveLogUtils saveLogUtils = null;
private static long lasterTime;
private static Context context;
private static String PATH = "testsavelog.txt";
private static String savePath;
private static File saveFileTxt;
private static FileOutputStream fileOutputStream;
private static ExecutorService executorService;
private static long logPhoneInfoTime;
public SaveLogUtils(Context context) {
this.context = context;
}
public static SaveLogUtils getInstace(Context context) {
if (saveLogUtils == null) {
synchronized (SaveLogUtils.class) {
saveLogUtils = new SaveLogUtils(context);
}
}
return saveLogUtils;
}
public static void saveLog(String logStr) {
if (TextUtils.isEmpty(logStr)) {
return;
}
synchronized (SaveLogUtils.class) {
if (executorService == null) {
executorService = Executors.newSingleThreadExecutor();
}
}
executorService.submit(() -> {
// 创建新的文件
createFile();
lasterTime = System.currentTimeMillis();
// 到了第二天生成新的日志文本
if (!farmatDay(System.currentTimeMillis()).equals(farmatDay(lasterTime))) {
lasterTime = System.currentTimeMillis();
createFile();
}
try {
fileOutputStream = new FileOutputStream(saveFileTxt);
// 第一次和每隔15分钟打印一次手机所有信息
if (logPhoneInfoTime == 0 || System.currentTimeMillis() - lasterTime > 5 * 60 * 1000) {
String model = Build.MODEL;
outStreamWrite(fileOutputStream, model, false);
String version = getVersion();
outStreamWrite(fileOutputStream, version, false);
logPhoneInfoTime = lasterTime;
}
outStreamWrite(fileOutputStream, logStr, true);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
});
}
public static void shutdownThreadPool() {
if (executorService != null) {
executorService.shutdown();
}
}
private static void createFile() {
savePath = context.getFilesDir().getPath() + File.separator + "test" + File.separator + getLogTitle() + "_" + PATH;
saveFileTxt = new File(savePath);
if (!saveFileTxt.exists()) {
saveFileTxt.mkdir();
}
}
public static String getLogTitle() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
String format = simpleDateFormat.format(date);
return format;
}
public static String farmatDay(long time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Date date = new Date(time);
String format = simpleDateFormat.format(date);
return format;
}
public static String farmatLogTime(long time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_mm_ss");
Date date = new Date(time);
String format = simpleDateFormat.format(date);
return format;
}
public static String getVersion() {
try {
PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
String version = info.versionName;
return version;
} catch (Exception e) {
e.printStackTrace();
return "无法获取到版本号";
}
}
public static void outStreamWrite(FileOutputStream fileOutputStream, String str, boolean isLogTxt) {
try {
if (!isLogTxt) {
fileOutputStream.write(str.getBytes());
fileOutputStream.write("\\r\\n".getBytes());
} else {
fileOutputStream.write(farmatLogTime(System.currentTimeMillis()).getBytes());
fileOutputStream.write("\\u0020".getBytes());
fileOutputStream.write("\\u0020".getBytes());
fileOutputStream.write(str.getBytes());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
日志输出
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 默认情况下 Spring Boot 框架使用 Logback 记录日志。 在 application.yml 文件...
- 看下spring boot配置文件中的mybatis部分 一般配置debug级别日志,基本日志都能输出了,之前配置...
- 如何能简洁明了的来查看k8s日志呢?我们这里使用上文说的最后一种方案:logPilot+ElasticSearch...
- 在工作时会遇到log4j日志输出,后台进程中,只有通过日志来分析程序中的问题,日志分为系统日志和业务日志,一般lo...
- 1、新建 logback-spring.xml2、修改logback-spring.xml 内容为下面内容 3、在...