1# @ohos.faultLogger (故障日志获取) 2<!--Kit: Performance Analysis Kit--> 3<!--Subsystem: HiviewDFX--> 4<!--Owner: @chenshi51--> 5<!--Designer: @Maplestory91--> 6<!--Tester: @gcw_KuLfPSbe--> 7<!--Adviser: @foryourself--> 8 9应用可以使用faultLogger接口查询系统侧缓存的当前应用的故障日志。接口以应用包名和系统分配的UID作为唯一键值。 10系统侧保存的应用故障日志数量受系统日志的压力限制,推荐使用[@ohos.hiviewdfx.hiAppEvent](js-apis-hiviewdfx-hiappevent.md)订阅APP_CRASH及APP_FREEZE等故障事件。 11 12> **说明:** 13> 14> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15> 本模块接口从API version 18开始废弃使用, 该接口不再维护。后续版本推荐使用[@ohos.hiviewdfx.hiAppEvent](js-apis-hiviewdfx-hiappevent.md)订阅APP_CRASH,APP_FREEZE事件。 16 17## 导入模块 18 19```ts 20import { FaultLogger } from '@kit.PerformanceAnalysisKit'; 21``` 22 23## FaultType 24 25故障类型枚举。 26 27**系统能力**:SystemCapability.HiviewDFX.Hiview.FaultLogger 28 29| 名称 | 值 | 说明 | 30| -------- | -------- | -------- | 31| NO_SPECIFIC | 0 | 不区分故障类型。 | 32| CPP_CRASH | 2 | C++程序故障类型。 | 33| JS_CRASH | 3 | JS程序故障类型。 | 34| APP_FREEZE | 4 | 应用程序卡死故障类型。 | 35 36## FaultLogInfo 37 38故障信息数据结构,获取到的故障信息的数据结构。 39 40**系统能力**:SystemCapability.HiviewDFX.Hiview.FaultLogger 41 42| 名称 | 类型 | 必填 | 说明 | 43| -------- | -------- | -------- | -------- | 44| pid | number | 是 | 故障进程的进程id。 | 45| uid | number | 是 | 故障进程的用户id。 | 46| type | [FaultType](#faulttype) | 是 | 故障类型。 | 47| timestamp | number | 是 | 日志生成时的毫秒级时间戳。 | 48| reason | string | 是 | 发生故障的原因。 | 49| module | string | 是 | 发生故障的模块。 | 50| summary | string | 是 | 故障的概要。 | 51| fullLog | string | 是 | 故障日志全文。 | 52 53## FaultLogger.query<sup>9+</sup> 54 55query(faultType: FaultType, callback: AsyncCallback<Array<FaultLogInfo>>) : void 56 57获取当前应用故障信息,该方法通过回调方式获取故障信息数组,故障信息数组内最多上报10份故障信息。 58 59**系统能力**:SystemCapability.HiviewDFX.Hiview.FaultLogger 60 61**参数:** 62 63| 参数名 | 类型 | 必填 | 说明 | 64| -------- | -------- | -------- | -------- | 65| faultType | [FaultType](#faulttype) | 是 | 输入要查询的故障类型。 | 66| callback | AsyncCallback<Array<[FaultLogInfo](#faultloginfo)>> | 是 | 回调函数,在回调函数中获取故障信息数组。<br/>- value拿到故障信息数组;value为undefined表示获取过程中出现异常,error返回错误提示字符串。| 67 68**错误码:** 69 70以下错误码的详细介绍参见[faultLogger错误码](errorcode-faultlogger.md)。 71 72| 错误码ID | 错误信息 | 73| --- | --- | 74| 401 | The parameter check failed, Parameter type error. | 75| 801 | The specified SystemCapability name was not found. | 76| 10600001 | The service is not started or is faulty. | 77 78**示例:** 79 80```ts 81import { FaultLogger } from '@kit.PerformanceAnalysisKit'; 82import { BusinessError } from '@kit.BasicServicesKit'; 83 84function queryFaultLogCallback(error: BusinessError, value: Array<FaultLogger.FaultLogInfo>) { 85 if (error) { 86 console.error(`error code:${error.code}, error msg:${error.message}`); 87 } else { 88 console.info("value length is " + value.length); 89 let len: number = value.length; 90 for (let i = 0; i < len; i++) { 91 console.info(`log: ${i}`); 92 console.info(`Log pid: ${value[i].pid}`); 93 console.info(`Log uid: ${value[i].uid}`); 94 console.info(`Log type: ${value[i].type}`); 95 console.info(`Log timestamp: ${value[i].timestamp}`); 96 console.info(`Log reason: ${value[i].reason}`); 97 console.info(`Log module: ${value[i].module}`); 98 console.info(`Log summary: ${value[i].summary}`); 99 console.info(`Log text: ${value[i].fullLog}`); 100 } 101 } 102} 103try { 104 FaultLogger.query(FaultLogger.FaultType.JS_CRASH, queryFaultLogCallback); 105} catch (err) { 106 console.error(`code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`); 107} 108``` 109 110## FaultLogger.query<sup>9+</sup> 111 112query(faultType: FaultType) : Promise<Array<FaultLogInfo>> 113 114获取当前应用故障信息,该方法通过Promise方式返回故障信息数组,故障信息数组内最多上报10份故障信息。 115 116**系统能力**:SystemCapability.HiviewDFX.Hiview.FaultLogger 117 118**参数:** 119 120| 参数名 | 类型 | 必填 | 说明 | 121| -------- | -------- | -------- | -------- | 122| faultType | [FaultType](#faulttype) | 是 | 输入要查询的故障类型。 | 123 124**返回值:** 125 126| 类型 | 说明 | 127| -------- | -------- | 128| Promise<Array<[FaultLogInfo](#faultloginfo)>> | Promise实例,可以在其then()方法中获取故障信息实例,也可以使用await。 <br/>- value拿到故障信息数组;value为undefined表示获取过程中出现异常。 | 129 130**错误码:** 131 132以下错误码的详细介绍参见[faultLogger错误码](errorcode-faultlogger.md)。 133 134| 错误码ID | 错误信息 | 135| --- | --- | 136| 401 | The parameter check failed, Parameter type error. | 137| 801 | The specified SystemCapability name was not found. | 138| 10600001 | The service is not started or is faulty. | 139 140**示例:** 141 142```ts 143import { FaultLogger } from '@kit.PerformanceAnalysisKit'; 144import { BusinessError } from '@kit.BasicServicesKit'; 145 146async function getLog() { 147 try { 148 let value: Array<FaultLogger.FaultLogInfo> = await FaultLogger.query(FaultLogger.FaultType.JS_CRASH); 149 if (value) { 150 console.info(`value length: ${value.length}`); 151 let len: number = value.length; 152 for (let i = 0; i < len; i++) { 153 console.info(`log: ${i}`); 154 console.info(`Log pid: ${value[i].pid}`); 155 console.info(`Log uid: ${value[i].uid}`); 156 console.info(`Log type: ${value[i].type}`); 157 console.info(`Log timestamp: ${value[i].timestamp}`); 158 console.info(`Log reason: ${value[i].reason}`); 159 console.info(`Log module: ${value[i].module}`); 160 console.info(`Log summary: ${value[i].summary}`); 161 console.info(`Log text: ${value[i].fullLog}`); 162 } 163 } 164 } catch (err) { 165 console.error(`code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`); 166 } 167} 168``` 169 170## FaultLogger.querySelfFaultLog<sup>(deprecated)</sup> 171 172querySelfFaultLog(faultType: FaultType, callback: AsyncCallback<Array<FaultLogInfo>>) : void 173 174> **说明:** 175> 176> 从 API Version 9 开始废弃,建议使用[FaultLogger.query](#faultloggerquery9)替代。 177 178获取当前应用故障信息,该方法通过回调方式获取故障信息数组,故障信息数组内最多上报10份故障信息。 179 180**系统能力**:SystemCapability.HiviewDFX.Hiview.FaultLogger 181 182**参数:** 183 184| 参数名 | 类型 | 必填 | 说明 | 185| -------- | -------- | -------- | -------- | 186| faultType | [FaultType](#faulttype) | 是 | 输入要查询的故障类型。 | 187| callback | AsyncCallback<Array<[FaultLogInfo](#faultloginfo)>> | 是 | 回调函数,在回调函数中获取故障信息数组。<br/>- value拿到故障信息数组;value为undefined表示获取过程中出现异常,error返回错误提示字符串。| 188 189**示例:** 190 191```ts 192import { FaultLogger } from '@kit.PerformanceAnalysisKit'; 193import { BusinessError } from '@kit.BasicServicesKit'; 194 195function queryFaultLogCallback(error: BusinessError, value: Array<FaultLogger.FaultLogInfo>) { 196 if (error) { 197 console.error(`error code:${error.code}, error msg:${error.message}`); 198 } else { 199 console.info(`value length: ${value.length}`); 200 let len: number = value.length; 201 for (let i = 0; i < len; i++) { 202 console.info(`log: ${i}`); 203 console.info(`Log pid: ${value[i].pid}`); 204 console.info(`Log uid: ${value[i].uid}`); 205 console.info(`Log type: ${value[i].type}`); 206 console.info(`Log timestamp: ${value[i].timestamp}`); 207 console.info(`Log reason: ${value[i].reason}`); 208 console.info(`Log module: ${value[i].module}`); 209 console.info(`Log summary: ${value[i].summary}`); 210 console.info(`Log text: ${value[i].fullLog}`); 211 } 212 } 213} 214FaultLogger.querySelfFaultLog(FaultLogger.FaultType.JS_CRASH, queryFaultLogCallback); 215``` 216 217## FaultLogger.querySelfFaultLog<sup>(deprecated)</sup> 218 219querySelfFaultLog(faultType: FaultType) : Promise<Array<FaultLogInfo>> 220 221> **说明:** 222> 223> 从 API Version 9 开始废弃,建议使用[FaultLogger.query](#faultloggerquery9-1)替代。 224 225获取当前应用故障信息,该方法通过Promise方式返回故障信息数组,故障信息数组内最多上报10份故障信息。 226 227**系统能力**:SystemCapability.HiviewDFX.Hiview.FaultLogger 228 229**参数:** 230 231| 参数名 | 类型 | 必填 | 说明 | 232| -------- | -------- | -------- | -------- | 233| faultType | [FaultType](#faulttype) | 是 | 输入要查询的故障类型。 | 234 235**返回值:** 236 237| 类型 | 说明 | 238| -------- | -------- | 239| Promise<Array<[FaultLogInfo](#faultloginfo)>> | Promise实例,可以在其then()方法中获取故障信息实例,也可以使用await。 <br/>- value拿到故障信息数组;value为undefined表示获取过程中出现异常。 | 240 241**示例:** 242 243```ts 244import { FaultLogger } from '@kit.PerformanceAnalysisKit'; 245 246async function getLog() { 247 let value: Array<FaultLogger.FaultLogInfo> = await FaultLogger.querySelfFaultLog(FaultLogger.FaultType.JS_CRASH); 248 if (value) { 249 console.info(`value length: ${value.length}`); 250 let len: number = value.length; 251 for (let i = 0; i < len; i++) { 252 console.info(`log: ${i}`); 253 console.info(`Log pid: ${value[i].pid}`); 254 console.info(`Log uid: ${value[i].uid}`); 255 console.info(`Log type: ${value[i].type}`); 256 console.info(`Log timestamp: ${value[i].timestamp}`); 257 console.info(`Log reason: ${value[i].reason}`); 258 console.info(`Log module: ${value[i].module}`); 259 console.info(`Log summary: ${value[i].summary}`); 260 console.info(`Log text: ${value[i].fullLog}`); 261 } 262 } 263} 264``` 265