当前位置: 首页 > wzjs >正文

第三方网站宣传怎么做计算机专业吃香吗

第三方网站宣传怎么做,计算机专业吃香吗,做视频网站要多大的主机,做微信网站公司名称Android 日志输出模块 本文主要记录下封装的日志输出模块. 1: 主要功能 日志模块初始化,并设置日志级别支持将日志写入文件日志文件单个限制200M,按天记录到指定文件,文件达到阈值后,记录新的日志文件.支持导出日志文件zip. 2: 具体实现 日志整体初始化使用静态内部类的方式…

Android 日志输出模块

本文主要记录下封装的日志输出模块.

1: 主要功能

  1. 日志模块初始化,并设置日志级别
  2. 支持将日志写入文件
  3. 日志文件单个限制200M,按天记录到指定文件,文件达到阈值后,记录新的日志文件.
  4. 支持导出日志文件zip.

2: 具体实现

  1. 日志整体初始化使用静态内部类的方式

    private Context context;// 单例模式:静态内部类实现
    private static class SingletonHolder {private static final LogManager INSTANCE = new LogManager();
    }public static LogManager getInstance(Context context) {if (SingletonHolder.INSTANCE.context == null) {SingletonHolder.INSTANCE.context = context.getApplicationContext();}return SingletonHolder.INSTANCE;
    }private LogManager() {
    }
    
  2. 日志级别在manager中定义枚举级别.

    // 定义日志级别枚举
    public enum LogLevel {DEBUG, INFO, WARN, ERROR
    }private LogLevel currentLogLevel = LogLevel.DEBUG; // 默认日志级别为 DEBUG// 设置日志级别方法
    public void setLogLevel(LogLevel logLevel) {this.currentLogLevel = logLevel;
    }
    
  3. 记录日志到文件

    /*** 记录日志到文件** @param tag 日志标签* @param message 日志内容* @param logLevel 日志级别*/
    public void log(String tag, String message, LogLevel logLevel) {String logFilePath = getCurrentLogFilePath();File logFile = new File(logFilePath);if (logFile.exists() && logFile.length() >= MAX_LOG_FILE_SIZE) {// 如果当前文件已满,创建新文件logFilePath = getCurrentLogFilePath();}try (FileOutputStream fos = new FileOutputStream(logFilePath, true);BufferedOutputStream bos = new BufferedOutputStream(fos)) {String logContent = String.format("[%s] [%s] [PID:%d] [TID:%d] [%s] %s\n",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),tag,android.os.Process.myPid(), // 进程号Thread.currentThread().getId(), // 线程号logLevel.name(), // 打印传入的日志级别message);bos.write(logContent.getBytes());// 根据传入的日志级别输出到控制台switch (logLevel) {case DEBUG:Log.d(tag, logContent.trim());break;case INFO:Log.i(tag, logContent.trim());break;case WARN:Log.w(tag, logContent.trim());break;case ERROR:Log.e(tag, logContent.trim());break;}} catch (IOException e) {Log.e("LogManager", "Failed to write log to file", e);}
    }
    

    主要判断当前日志的大小,如果达到阈值200M.创建新的文件.

    /*** 获取当前日志文件路径** @return 当前日志文件路径*/
    private String getCurrentLogFilePath() {String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date());File logDir = new File(context.getExternalFilesDir(null), LOG_DIR);if (!logDir.exists()) {logDir.mkdirs();}int fileIndex = 1;File logFile;do {String fileName = LOG_FILE_PREFIX + currentDate + "_" + String.format("%03d", fileIndex) + LOG_FILE_EXTENSION;logFile = new File(logDir, fileName);fileIndex++;} while (logFile.exists() && logFile.length() >= MAX_LOG_FILE_SIZE);return logFile.getAbsolutePath();
    }
    

    4: 导出日志

    主要操作为导出所有日志文件为 ZIP 包,获取到文件列表将文件添加到 ZIP 包中.

    /*** 导出所有日志文件为 ZIP 包** @return 导出的 ZIP 文件路径*/
    public String exportLogsToZip() {File logDir = new File(context.getExternalFilesDir(null), LOG_DIR);File exportDir = new File(context.getExternalFilesDir(null), EXPORT_DIR);if (!exportDir.exists()) {exportDir.mkdirs();}File zipFile = new File(exportDir, EXPORT_FILE_NAME);try (FileOutputStream fos = new FileOutputStream(zipFile);ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos))) {File[] logFiles = logDir.listFiles();if (logFiles != null) {int totalFiles = logFiles.length;for (int i = 0; i < totalFiles; i++) {addToZipFile(logFiles[i], zos);if (exportProgressListener != null) {int progress = (int) (((double) (i + 1) / totalFiles) * 100);exportProgressListener.onProgress(progress);}}}} catch (IOException e) {Log.e("LogManager", "Failed to export logs to ZIP", e);return null;}if (exportProgressListener != null) {exportProgressListener.onComplete(zipFile.getAbsolutePath());}return zipFile.getAbsolutePath();
    }
    
    /*** 将文件添加到 ZIP 包中** @param file 文件* @param zos ZIP 输出流* @throws IOException 如果发生 I/O 错误*/
    private void addToZipFile(File file, ZipOutputStream zos) throws IOException {try (FileInputStream fis = new FileInputStream(file);BufferedInputStream bis = new BufferedInputStream(fis)) {ZipEntry zipEntry = new ZipEntry(file.getName());zos.putNextEntry(zipEntry);byte[] buffer = new byte[1024];int length;while ((length = bis.read(buffer)) > 0) {zos.write(buffer, 0, length);}zos.closeEntry();}
    }
    

    添加导出日志的回调.可根据回调进度自定义进度条等效果.

    public interface OnExportProgressListener {void onProgress(int progress);void onComplete(String exportPath);
    }private OnExportProgressListener exportProgressListener;public void setOnExportProgressListener(OnExportProgressListener listener) {this.exportProgressListener = listener;
    }
    

    下面附上LogManager的代码:

    package com.zh.logmanager;import android.content.Context;
    import android.util.Log;import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;/*** @Author: zh* @Time: 25-4-5.* @Email:* @Describe:*/
    public class LogManager {private static final String LOG_DIR = "logs";private static final String EXPORT_DIR = "exports";private static final String LOG_FILE_PREFIX = "app_log_";private static final String LOG_FILE_EXTENSION = ".txt";private static final String EXPORT_FILE_NAME = "logs_export.zip";private static final long MAX_LOG_FILE_SIZE = 200 * 1024 * 1024; // 200MBprivate Context context;// 单例模式:静态内部类实现private static class SingletonHolder {private static final LogManager INSTANCE = new LogManager();}public static LogManager getInstance(Context context) {if (SingletonHolder.INSTANCE.context == null) {SingletonHolder.INSTANCE.context = context.getApplicationContext();}return SingletonHolder.INSTANCE;}private LogManager() {}/*** 获取当前日志文件路径** @return 当前日志文件路径*/private String getCurrentLogFilePath() {String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date());File logDir = new File(context.getExternalFilesDir(null), LOG_DIR);if (!logDir.exists()) {logDir.mkdirs();}int fileIndex = 1;File logFile;do {String fileName = LOG_FILE_PREFIX + currentDate + "_" + String.format("%03d", fileIndex) + LOG_FILE_EXTENSION;logFile = new File(logDir, fileName);fileIndex++;} while (logFile.exists() && logFile.length() >= MAX_LOG_FILE_SIZE);return logFile.getAbsolutePath();}// 定义日志级别枚举public enum LogLevel {DEBUG, INFO, WARN, ERROR}private LogLevel currentLogLevel = LogLevel.DEBUG; // 默认日志级别为 DEBUG// 设置日志级别方法public void setLogLevel(LogLevel logLevel) {this.currentLogLevel = logLevel;}public void d(String tag, String message){this.log(tag, message,LogLevel.DEBUG);}public void i(String tag, String message){this.log(tag, message,LogLevel.INFO);}public void w(String tag, String message){this.log(tag, message,LogLevel.WARN);}public void e(String tag, String message){this.log(tag, message,LogLevel.ERROR);}public void log(String tag, String message){this.log(tag, message,LogLevel.DEBUG);}/*** 记录日志到文件** @param tag 日志标签* @param message 日志内容* @param logLevel 日志级别*/public void log(String tag, String message, LogLevel logLevel) {String logFilePath = getCurrentLogFilePath();File logFile = new File(logFilePath);if (logFile.exists() && logFile.length() >= MAX_LOG_FILE_SIZE) {// 如果当前文件已满,创建新文件logFilePath = getCurrentLogFilePath();}try (FileOutputStream fos = new FileOutputStream(logFilePath, true);BufferedOutputStream bos = new BufferedOutputStream(fos)) {String logContent = String.format("[%s] [%s] [PID:%d] [TID:%d] [%s] %s\n",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),tag,android.os.Process.myPid(), // 进程号Thread.currentThread().getId(), // 线程号logLevel.name(), // 打印传入的日志级别message);bos.write(logContent.getBytes());// 根据传入的日志级别输出到控制台switch (logLevel) {case DEBUG:Log.d(tag, logContent.trim());break;case INFO:Log.i(tag, logContent.trim());break;case WARN:Log.w(tag, logContent.trim());break;case ERROR:Log.e(tag, logContent.trim());break;}} catch (IOException e) {Log.e("LogManager", "Failed to write log to file", e);}}public interface OnExportProgressListener {void onProgress(int progress);void onComplete(String exportPath);}private OnExportProgressListener exportProgressListener;public void setOnExportProgressListener(OnExportProgressListener listener) {this.exportProgressListener = listener;}/*** 导出所有日志文件为 ZIP 包** @return 导出的 ZIP 文件路径*/public String exportLogsToZip() {File logDir = new File(context.getExternalFilesDir(null), LOG_DIR);File exportDir = new File(context.getExternalFilesDir(null), EXPORT_DIR);if (!exportDir.exists()) {exportDir.mkdirs();}File zipFile = new File(exportDir, EXPORT_FILE_NAME);try (FileOutputStream fos = new FileOutputStream(zipFile);ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos))) {File[] logFiles = logDir.listFiles();if (logFiles != null) {int totalFiles = logFiles.length;for (int i = 0; i < totalFiles; i++) {addToZipFile(logFiles[i], zos);if (exportProgressListener != null) {int progress = (int) (((double) (i + 1) / totalFiles) * 100);exportProgressListener.onProgress(progress);}}}} catch (IOException e) {Log.e("LogManager", "Failed to export logs to ZIP", e);return null;}if (exportProgressListener != null) {exportProgressListener.onComplete(zipFile.getAbsolutePath());}return zipFile.getAbsolutePath();}/*** 将文件添加到 ZIP 包中** @param file 文件* @param zos ZIP 输出流* @throws IOException 如果发生 I/O 错误*/private void addToZipFile(File file, ZipOutputStream zos) throws IOException {try (FileInputStream fis = new FileInputStream(file);BufferedInputStream bis = new BufferedInputStream(fis)) {ZipEntry zipEntry = new ZipEntry(file.getName());zos.putNextEntry(zipEntry);byte[] buffer = new byte[1024];int length;while ((length = bis.read(buffer)) > 0) {zos.write(buffer, 0, length);}zos.closeEntry();}}
    }
    

3: 调用+结果

​ 初始化很简单,如下:

LogManager logManager = LogManager.getInstance(this);
Button logButton = findViewById(R.id.log_button);
Button exportButton = findViewById(R.id.export_button);logButton.setOnClickListener(v -> {logManager.log("MainActivity", "This is a test log message");logManager.i("MainActivity", "This is a test log message");logManager.w("MainActivity", "This is a test log message");logManager.e("MainActivity", "This is a test log message");
});

可以看到/storage/emulated/0/Android/data/com.zh.logmanager/files/logs/app_log_20250405_001.txt 路径下产生了新的日志文件.

文件内容:

[2025-04-05 17:21:50] [MainActivity] [PID:29019] [TID:2] [DEBUG] This is a test log message
[2025-04-05 17:21:50] [MainActivity] [PID:29019] [TID:2] [INFO] This is a test log message
[2025-04-05 17:21:50] [MainActivity] [PID:29019] [TID:2] [WARN] This is a test log message
[2025-04-05 17:21:50] [MainActivity] [PID:29019] [TID:2] [ERROR] This is a test log message

导出日志如下: /storage/emulated/0/Android/data/com.zh.logmanager/files/exports/logs_export.zip


文章转载自:

http://hH3dFIB6.xbbrh.cn
http://tdwOR1o8.xbbrh.cn
http://aAG59hjZ.xbbrh.cn
http://d5lPYKBp.xbbrh.cn
http://aTzlyv8M.xbbrh.cn
http://P6ec1uuQ.xbbrh.cn
http://7LqsHwFr.xbbrh.cn
http://9qOWjGVq.xbbrh.cn
http://PLxS0Mxc.xbbrh.cn
http://OgEO1u40.xbbrh.cn
http://IiwSd3Fx.xbbrh.cn
http://x3Iw0B5e.xbbrh.cn
http://XdT05eOV.xbbrh.cn
http://sRuQjmLG.xbbrh.cn
http://cVZIkb6T.xbbrh.cn
http://rhIwgCju.xbbrh.cn
http://hso5sGUQ.xbbrh.cn
http://D1z2H7GZ.xbbrh.cn
http://2JB7KTbk.xbbrh.cn
http://vZkm1CEk.xbbrh.cn
http://ko4Ikdnt.xbbrh.cn
http://KMmzrNF0.xbbrh.cn
http://zXew3VES.xbbrh.cn
http://PL3DTNVW.xbbrh.cn
http://3YaIgMEz.xbbrh.cn
http://Xuq7eS8l.xbbrh.cn
http://QKt8Y4EC.xbbrh.cn
http://kywXAmhk.xbbrh.cn
http://J9j0se1c.xbbrh.cn
http://wFOdHxDo.xbbrh.cn
http://www.dtcms.com/wzjs/685235.html

相关文章:

  • 沈阳网站托管公司廊坊 网站
  • 国外网站引流如何做线上销售的方法和技巧
  • 手机版传奇发布网站购物网站排名女装
  • 做生存曲线的网站无锡网站设计
  • 做泥软件下载官方网站外贸网站电子建设
  • 建设公司官方网站首页门户网站程序
  • 购物手机网站怎么做搜狐酒业峰会
  • 建设银行学习网站手机网站开发工具6
  • 网站开发工程师招聘要求网站建设 蔬菜配送
  • 网站运行速度慢iis搭建网站404
  • 盐城网站app建设it人力开发外包服务
  • 前端学校网站开发视频教程设计公司介绍模板
  • 南京平台网站建设咨询公司前景好不好
  • 统计局门户网站建设背景淘宝做网站价格
  • 专门做婚姻法的网站宝和网站建设
  • 住房城乡住房和城乡建设部网站网站上面的内容里面放照片怎么做的
  • 郑州网站建设方案佛山网站建设哪家评价高
  • 布吉网站建设哪家便宜php网站开发作业
  • 青岛网站运营庄行网站建设
  • 杭州建设工程招标平台官网网站优化升级怎么做
  • 安徽网新科技有限公司 网站开发个人注册企业查询
  • 做的网站放在阿里云h5网站制作报价
  • 青岛中小企业建设网站有扶持资金吗网站建设工作室创业计划书
  • 做一个网站需要多少钱大概360客户如何做网站推广
  • 如何做网站流量报告做网站哪个语言快
  • 常用的网站开发技术有哪几种海外网络推广收费
  • 网站设计的必要性瑞安网站制作
  • pc建站网站做资讯需要获取许可证吗
  • 医疗类网站源码wordpress店铺
  • 南京电商网站开发公司wordpress个人建站教程