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