如何保存faultLogger ,有人知道吗?

如何保存faultLogger


HarmonyOS
2024-06-13 00:20:58
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
milchcow

参考代码如下:

import fs from '@ohos.file.fs'; 
import { BusinessError } from '@ohos.base'; 
import hilog from '@ohos.hilog'; 
import common from '@ohos.app.ability.common'; 
import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 
import FaultLogger from '@ohos.faultLogger'; 
import preferences from '@ohos.data.preferences'; 
 
const TAG: string = 'testTag' 
 
export class LogUtils { 
  // import faultLogger from '@ohos.faultLogger' 
  static async queryAndUploadFaultLog(context: common.UIAbilityContext, launchParam: AbilityConstant.LaunchParam) { 
    if (launchParam.lastExitReason == AbilityConstant.LastExitReason.NORMAL) { 
      hilog.info(0x00000, TAG, '上次应用退出时未发生异常'); 
      return; 
    } 
    let value: Array<FaultLogger.FaultLogInfo> = await FaultLogger.query(FaultLogger.FaultType.NO_SPECIFIC) 
    hilog.info(0x00000, TAG, '报错日志:' + value.toString()); 
    if (value) { 
      let len = value.length 
      hilog.info(0x00000, TAG, '报错日志数量:' + len); 
      if (len === 0) { 
        return; 
      } 
      //取得实例 
      let preference: preferences.Preferences = await preferences.getPreferences(context, "STLiveness"); 
      // 查询上一次的处理的时间戳 
      let lastFaultHandleTime = preference.getSync('faultHandleTime', 0) 
      hilog.info(0x0000, TAG, "lastFaultHandleTime:" + lastFaultHandleTime); 
      // let innerContext = context; 
      for (let i = 0; i < len; i++) { 
        let timestamp = value[i].timestamp 
        hilog.info(0x00000, TAG, '日志文件名称#' + timestamp); 
        if (lastFaultHandleTime >= timestamp) { 
          hilog.error(0x00000, TAG, "Maple No New Logs."); 
          return; 
        } 
        // 将日志保存到应用沙箱的目录 保存文件名为 时间戳.log 
        await LogUtils.save(value[i].fullLog, context.filesDir + "/crash", timestamp + ".log") 
        await preference.put("faultHandleTime", timestamp); 
        await preference.flush() 
      } 
    } 
  } 
  static async save(buffer: ArrayBuffer | string, destFilePath: string, name: string): Promise<string> { 
    await LogUtils.mkdir(destFilePath); 
    hilog.info(0x00000, TAG, '写入日志的内容为:' + buffer); 
    hilog.info(0x00000, TAG, '被写入的日志文件路径为:' + destFilePath); 
    if (buffer) { 
      try { 
        let file = fs.openSync(destFilePath + "/" + name, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
        fs.writeSync(file.fd, buffer); 
        fs.closeSync(file) 
      } catch (error) { 
        let e: BusinessError = error as BusinessError; 
        hilog.info(0x0000, TAG, "FileUtils:save:::" + e.message); 
      } 
    } 
    return destFilePath; 
  } 
 
  static async mkdir(destFilePath: string) { 
    hilog.info(0x00000, TAG, '开始创建目录:' + destFilePath); 
    if (fs.accessSync(destFilePath)) { 
      hilog.info(0x00000, TAG, '目录已经存在,无需创建'); 
      return 
    } 
    await fs.mkdir(destFilePath) 
    hilog.info(0x00000, TAG, '创建完成'); 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
分享
微博
QQ
微信
回复
2024-06-13 20:32:10


相关问题
图片压缩并保存方法,有人知道吗
1101浏览 • 0回复 待解决
如何实现振动,有人知道吗
1670浏览 • 2回复 待解决
如何获取windowStage,有人知道吗
1187浏览 • 1回复 待解决
如何跳出ForEach,有人知道吗
2560浏览 • 1回复 待解决
$$语法如何使用?有人知道吗
1191浏览 • 1回复 待解决
如何发送短信,有人知道吗?
2468浏览 • 1回复 待解决
如何获取系统电量,有人知道吗
2487浏览 • 1回复 待解决
IP地址如何转化,有人知道吗
976浏览 • 1回复 待解决
ArkTS要如何使用this,有人知道吗
1049浏览 • 1回复 待解决
如何实现镂空效果,有人知道吗?
757浏览 • 1回复 待解决
如何获取组件高度,有人知道吗
2644浏览 • 1回复 待解决
如何查询设备类型?有人知道吗
845浏览 • 1回复 待解决
导航栏如何适配,有人知道吗?
2166浏览 • 0回复 待解决
如何使用快速修复,有人知道吗
1138浏览 • 1回复 待解决
如何引用HSP库,有人知道吗?
2077浏览 • 1回复 待解决
IDE如何开启ASAN,有人知道吗
694浏览 • 1回复 待解决
深色模式如何屏蔽?有人知道吗
703浏览 • 0回复 待解决
如何实现翻页功能,有人知道吗
2424浏览 • 1回复 待解决
如何获取wifi列表,有人知道吗
1249浏览 • 1回复 待解决
如何实现图片预览,有人知道吗
1056浏览 • 1回复 待解决
如何定义dialog动画,有人知道吗?
2494浏览 • 1回复 待解决
clientid相关问题,有人知道吗
2333浏览 • 1回复 待解决
webview组件demo ,有人知道吗
1314浏览 • 1回复 待解决
taskpool 使用问题,有人知道吗
1614浏览 • 1回复 待解决
有人知道吗
797浏览 • 1回复 待解决