美文网首页
日志输出

日志输出

作者: 皓皓amous | 来源:发表于2023-11-19 17:41 被阅读0次
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);
       }
   }

}

相关文章

  • 03-pch文件

    pch文件 pch的作用 日志输出——发布和调试下NSlog的输出 为什么要管理日志输出?因为日志输出非常耗性能,...

  • GETH 日志模块的使用

    package log import "testing" //日志按级别输出 /** 可以按照日志级别输出日志==...

  • Unreal 日志系统

    1. 在Editor的【输出日志】窗口输出日志 UE_LOG(日志种类Category, 日志级别Verbosi...

  • Logback日志框架的使用与配置

    什么是日志框架 日志框架是一套能够实现日志输出的工具包 使用日志框架的好处 可以定制日志输出目标 可以定制输出格式...

  • RabbitMQ HelloWorld 示例

    Maven依赖 生产端代码 运行输出日志: 消费端代码 输出日志

  • F-Log

    简介 方便真机查看日志 待完善:日志读写 LogScreen 日志滚动输出屏幕 滑动查看 清空重新打印 日志输出类...

  • JVM - GC日志

    JVM - GC日志 -XX:+PrintGC 输出GC日志 -XX:+PrintGCDetails 输出GC的详...

  • 查看GC日志

    -XX:+PrintGC 输出GC日志-XX:+PrintGCDetails 输出GC的详细日志-XX:+Prin...

  • 日志模块

    一,日志模块介绍 1,默认日志级别为30(输出到屏幕) 2, 日志指定输出到文件 二,日志模块的四种角色 三,日志...

  • Java 如何正确地输出日志

    几个错误的打日志方式 不要使用 System.out.print..输出日志的时候只能通过日志框架来输出日志,而不...

网友评论

      本文标题:日志输出

      本文链接:https://www.haomeiwen.com/subject/tdxmwdtx.html