在uni-app中引入本地日志插件

发布于:2025-08-06 ⋅ 阅读:(12) ⋅ 点赞:(0)

src/js_sdk/FXX-APP-LOG/FXX-APP-LOG.js

// 条件编译:仅在 APP-PLUS 平台(5+ App)下执行
// #ifdef APP-PLUS
const LogCat = {
  // 获取主Activity上下文
  main: plus.android.runtimeMainActivity(),

  // 导入Android原生类
  Environment: plus.android.importClass('android.os.Environment'),
  BufferedWriter: plus.android.importClass('java.io.BufferedWriter'),
  BufferedReader: plus.android.importClass('java.io.BufferedReader'),
  File: plus.android.importClass('java.io.File'),
  FileOutputStream: plus.android.importClass('java.io.FileOutputStream'),
  FileInputStream: plus.android.importClass('java.io.FileInputStream'),
  OutputStreamWriter: plus.android.importClass('java.io.OutputStreamWriter'),
  InputStreamReader: plus.android.importClass('java.io.InputStreamReader'),

  // 日志存储路径
  LogPath: '',

  /**
   * 初始化日志存储路径
   * 优先使用外部存储,不可用时回退到内部存储
   */
  init: function () {
    if (
      this.Environment.MEDIA_MOUNTED ||
      !this.Environment.isExternalStorageRemovable()
    ) {
      this.LogPath = this.main.getExternalFilesDir(null).getPath() + '/fxx_log'
    } else {
      this.LogPath = this.main.getFilesDir().getPath() + '/fxx_log'
    }
    console.log('日志文件存放路径->', this.LogPath)
  },

  /**
   * 将参数转换为字符串
   * @param {...any} args - 任意数量的参数
   * @returns {string} 拼接后的字符串
   */
  argsToString: function (...args) {
    return args
      .map(arg => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))
      .join(',')
  },

  /**
   * 写入日志到文件
   * @param {...any} args - 要记录的日志内容
   */
  writeLog: function (...args) {
    let date = getTime('YYYY-MM-DD') // 获取当前日期
    let datetime = getTime() // 获取完整的时间格式

    const msg = this.argsToString(...args) // 转换参数为字符串

    // 按日期分割日志文件
    let fileName = this.LogPath + '/log_' + date + '.txt'
    let content = `\n${datetime}: ${msg}\n` // 日志内容格式

    // 确保日志目录存在
    let file = new this.File(this.LogPath)
    if (!file.exists()) {
      file.mkdirs() // 递归创建目录
    }

    // 使用缓冲流写入文件
    let fos = null
    let bw = null
    try {
      fos = new this.FileOutputStream(fileName, true) // true表示追加模式
      bw = new this.BufferedWriter(new this.OutputStreamWriter(fos))
      bw.append(content) // 追加内容
    } catch (e) {
      console.log('写入日志失败->', e)
    } finally {
      try {
        if (bw != null) bw.close() // 关闭流
        if (fos != null) fos.close()
      } catch (closeEx) {
        console.log('关闭流失败->', closeEx)
      }
    }
  },

  /**
   * 删除指定日志文件
   * @param {string} fileName - 要删除的文件路径
   */
  removeLogFile: function (fileName) {
    let file = new this.File(fileName)
    let fos = new this.FileOutputStream(fileName, true)
    fos.close()
    file.delete()
    console.log('删除日志文件->' + fileName)
  },

  /**
   * 获取日志文件列表
   * @returns {Array} 返回包含文件名和路径的对象数组
   */
  getLogFileList: function () {
    let list = []
    let file = new this.File(this.LogPath)
    let tempList = file.listFiles()

    for (let i = 0; i < tempList.length; i++) {
      list.push({
        name: tempList[i].getName(),
        filePath: tempList[i].getPath()
      })
    }
    return list
  },

  /**
   * 读取日志文件内容
   * @param {string} path - 文件路径
   * @returns {Array} 按行分割的日志内容数组
   */
  readLog: function (path) {
    const charset = 'utf-8'
    let file = new this.File(path)
    let list = []

    try {
      if (!file.exists()) return list

      let inputStreamReader = new this.InputStreamReader(
        new this.FileInputStream(file),
        charset
      )
      let bufferedReader = new this.BufferedReader(inputStreamReader)

      let line = ''
      while ((line = bufferedReader.readLine()) != null) {
        if (line) list.push(line)
      }

      bufferedReader.close()
      inputStreamReader.close()
    } catch (e) {
      console.error('读取日志失败->', e)
      // 确保资源关闭
      if (bufferedReader) bufferedReader.close()
      if (inputStreamReader) inputStreamReader.close()
    }
    return list
  }
}

// 初始化日志系统
LogCat.init()
// #endif

// 非APP-PLUS平台的空实现
// #ifndef APP-PLUS
const LogCat = {}
// #endif

/**
 * 重写console.log方法
 * 保持原始功能同时写入日志文件
 */
console.writeLog = function (...args) {
  console.log(...args) // 保持原始控制台输出
  // #ifdef APP-PLUS
  LogCat.writeLog(...args) // 写入文件
  // #endif
}

/**
 * 时间格式化函数
 * @param {string} [style] - 格式类型(可选),默认为 'YYYY-MM-DD hh:mm:ss'
 *  可选值:
 *    - 'hh:mm:ss'        : 时:分:秒
 *    - 'YYYY-MM-DD'      : 年-月-日
 *    - 'YYYYMMDD'        : 年月日
 *    - 'YYYY-MM-DD hh:mm:ss' : 年-月-日 时:分:秒 (默认)
 *    - 'YYYYMMDDhhmmss'  : 年月日时分秒
 *    - 'YYYY'            : 年
 *    - 'MM'              : 月
 *    - 'DD'              : 日
 *    - 'getDay'          : 星期几(0-6)
 * @returns {string} 格式化后的时间字符串
 */
const getTime = (style = 'YYYY-MM-DD hh:mm:ss') => {
  const time = new Date()

  // 时间组件补零处理
  const pad = num => (num < 10 ? `0${num}` : num)
  const yyyy = time.getFullYear()
  const mm = pad(time.getMonth() + 1) // 月份从0开始
  const dd = pad(time.getDate())
  const h = pad(time.getHours())
  const m = pad(time.getMinutes())
  const s = pad(time.getSeconds())
  const z = time.getDay() // 星期几(0-6)

  // 格式映射表
  const formats = {
    'hh:mm:ss': `${h}:${m}:${s}`,
    'YYYY-MM-DD': `${yyyy}-${mm}-${dd}`,
    YYYYMMDD: `${yyyy}${mm}${dd}`,
    'YYYY-MM-DD hh:mm:ss': `${yyyy}-${mm}-${dd} ${h}:${m}:${s}`,
    YYYYMMDDhhmmss: `${yyyy}${mm}${dd}${h}${m}${s}`,
    YYYY: yyyy,
    MM: mm,
    DD: dd,
    getDay: z
  }

  return formats[style] || formats['YYYY-MM-DD hh:mm:ss']
}

export default LogCat

使用示例:

// 基本使用
LogCat.writeLog('用户操作', {action: 'login', time: new Date()})

// 通过重写的console方法
console.writeLog('API响应数据:', responseData)

// 读取日志
const logs = LogCat.readLog('/path/to/logfile.txt')

// 获取日志列表
const logFiles = LogCat.getLogFileList()

注意事项:

  1. 需要真机运行才能测试完整功能
  2. Android需要存储权限(Android 6.0+需要动态申请)
  3. 大量日志写入建议使用队列机制
  4. 敏感信息记录前应做脱敏处理

网站公告

今日签到

点亮在社区的每一天
去签到