1# @ohos.faultLogger (FaultLogger) 2 3> **NOTE** 4> 5> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. 6 7## Modules to Import 8 9```ts 10import faultLogger from '@ohos.faultLogger' 11``` 12 13## FaultType 14 15Enumerates the fault types. 16 17**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger 18 19| Name| Value| Description| 20| -------- | -------- | -------- | 21| NO_SPECIFIC | 0 | No specific fault type.| 22| CPP_CRASH | 2 | C++ program crash.| 23| JS_CRASH | 3 | JS program crash.| 24| APP_FREEZE | 4 | Application freezing.| 25 26## FaultLogInfo 27 28Defines the data structure of the fault log information. 29 30**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger 31 32| Name| Type| Mandatory| Description| 33| -------- | -------- | -------- | -------- | 34| pid | number | Yes| Process ID of the faulty process.| 35| uid | number | Yes| User ID of the faulty process.| 36| type | [FaultType](#faulttype) | Yes| Fault type.| 37| timestamp | number | Yes| Second-level timestamp when the log was generated.| 38| reason | string | Yes| Reason for the fault.| 39| module | string | Yes| Module on which the fault occurred.| 40| summary | string | Yes| Summary of the fault.| 41| fullLog | string | Yes| Full log text.| 42 43## faultLogger.query<sup>9+</sup> 44 45query(faultType: FaultType, callback: AsyncCallback<Array<FaultLogInfo>>) : void 46 47Obtains the fault information about the current process. This API uses an asynchronous callback to return the fault information array obtained, which contains a maximum of 10 pieces of fault information. 48 49**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger 50 51**Parameters** 52 53| Name| Type| Mandatory| Description| 54| -------- | -------- | -------- | -------- | 55| faultType | [FaultType](#faulttype) | Yes| Fault type.| 56| callback | AsyncCallback<Array<[FaultLogInfo](#faultloginfo)>> | Yes| Callback used to return the fault information array.<br>**value** is the fault information array obtained. If **value** is **undefined**, an exception occurs during the information retrieval. In this case, an error string will be returned. 57 58**Error codes** 59 60For details about the error codes, see [FaultLogger Error Codes](../errorcodes/errorcode-faultlogger.md). 61 62| ID| Error Message| 63| --- | --- | 64| 10600001 | The service is not started or is faulty | 65 66**Example** 67 68```ts 69import faultLogger from '@ohos.faultLogger' 70import { BusinessError } from '@ohos.base' 71 72function queryFaultLogCallback(error: BusinessError, value: Array<faultLogger.FaultLogInfo>) { 73 if (error) { 74 console.info('error is ' + error); 75 } else { 76 console.info("value length is " + value.length); 77 let len: number = value.length; 78 for (let i = 0; i < len; i++) { 79 console.info("log: " + i); 80 console.info("Log pid: " + value[i].pid); 81 console.info("Log uid: " + value[i].uid); 82 console.info("Log type: " + value[i].type); 83 console.info("Log timestamp: " + value[i].timestamp); 84 console.info("Log reason: " + value[i].reason); 85 console.info("Log module: " + value[i].module); 86 console.info("Log summary: " + value[i].summary); 87 console.info("Log text: " + value[i].fullLog); 88 } 89 } 90} 91try { 92 faultLogger.query(faultLogger.FaultType.JS_CRASH, queryFaultLogCallback); 93} catch (err) { 94 console.error(`code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`); 95} 96``` 97 98## faultLogger.query<sup>9+</sup> 99 100query(faultType: FaultType) : Promise<Array<FaultLogInfo>> 101 102Obtains the fault information about the current process. This API uses a promise to return the fault information array obtained, which contains a maximum of 10 pieces of fault information. 103 104**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger 105 106**Parameters** 107 108| Name| Type| Mandatory| Description| 109| -------- | -------- | -------- | -------- | 110| faultType | [FaultType](#faulttype) | Yes| Fault type.| 111 112**Return value** 113 114| Type| Description| 115| -------- | -------- | 116| Promise<Array<[FaultLogInfo](#faultloginfo)>> | Promise used to return the fault information array. You can obtain the fault information instance in its **then()** method or use **await**.<br>**value** is the fault information array obtained. If **value** is **undefined**, an exception occurs during the information retrieval.| 117 118**Error codes** 119 120For details about the error codes, see [FaultLogger Error Codes](../errorcodes/errorcode-faultlogger.md). 121 122| ID| Error Message| 123| --- | --- | 124| 10600001 | The service is not started or is faulty | 125 126**Example** 127 128```ts 129import faultLogger from '@ohos.faultLogger' 130import { BusinessError } from '@ohos.base' 131 132async function getLog() { 133 try { 134 let value: Array<faultLogger.FaultLogInfo> = await faultLogger.query(faultLogger.FaultType.JS_CRASH); 135 if (value) { 136 console.info("value length is " + value.length); 137 let len: number = value.length; 138 for (let i = 0; i < len; i++) { 139 console.info("log: " + i); 140 console.info("Log pid: " + value[i].pid); 141 console.info("Log uid: " + value[i].uid); 142 console.info("Log type: " + value[i].type); 143 console.info("Log timestamp: " + value[i].timestamp); 144 console.info("Log reason: " + value[i].reason); 145 console.info("Log module: " + value[i].module); 146 console.info("Log summary: " + value[i].summary); 147 console.info("Log text: " + value[i].fullLog); 148 } 149 } 150 } catch (err) { 151 console.error(`code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`); 152 } 153} 154``` 155 156## faultLogger.querySelfFaultLog<sup>(deprecated)</sup> 157 158querySelfFaultLog(faultType: FaultType, callback: AsyncCallback<Array<FaultLogInfo>>) : void 159 160> **NOTE** 161> 162> This API is deprecated since API version 9. You are advised to use [faultLogger.query](#faultloggerquery9) instead. 163 164Obtains the fault information about the current process. This API uses an asynchronous callback to return the fault information array obtained, which contains a maximum of 10 pieces of fault information. 165 166**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger 167 168**Parameters** 169 170| Name| Type| Mandatory| Description| 171| -------- | -------- | -------- | -------- | 172| faultType | [FaultType](#faulttype) | Yes| Fault type.| 173| callback | AsyncCallback<Array<[FaultLogInfo](#faultloginfo)>> | Yes| Callback used to return the fault information array.<br>**value** is the fault information array obtained. If **value** is **undefined**, an exception occurs during the information retrieval. In this case, an error string will be returned. 174 175**Example** 176 177```ts 178import faultLogger from '@ohos.faultLogger' 179import { BusinessError } from '@ohos.base' 180 181function queryFaultLogCallback(error: BusinessError, value: Array<faultLogger.FaultLogInfo>) { 182 if (error) { 183 console.info('error is ' + error); 184 } else { 185 console.info("value length is " + value.length); 186 let len: number = value.length; 187 for (let i = 0; i < len; i++) { 188 console.info("log: " + i); 189 console.info("Log pid: " + value[i].pid); 190 console.info("Log uid: " + value[i].uid); 191 console.info("Log type: " + value[i].type); 192 console.info("Log timestamp: " + value[i].timestamp); 193 console.info("Log reason: " + value[i].reason); 194 console.info("Log module: " + value[i].module); 195 console.info("Log summary: " + value[i].summary); 196 console.info("Log text: " + value[i].fullLog); 197 } 198 } 199} 200faultLogger.querySelfFaultLog(faultLogger.FaultType.JS_CRASH, queryFaultLogCallback); 201``` 202 203## faultLogger.querySelfFaultLog<sup>(deprecated)</sup> 204 205querySelfFaultLog(faultType: FaultType) : Promise<Array<FaultLogInfo>> 206 207> **NOTE** 208> 209> This API is deprecated since API version 9. You are advised to use [faultLogger.query](#faultloggerquery9-1). 210 211Obtains the fault information about the current process. This API uses a promise to return the fault information array obtained, which contains a maximum of 10 pieces of fault information. 212 213**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger 214 215**Parameters** 216 217| Name| Type| Mandatory| Description| 218| -------- | -------- | -------- | -------- | 219| faultType | [FaultType](#faulttype) | Yes| Fault type.| 220 221**Return value** 222 223| Type| Description| 224| -------- | -------- | 225| Promise<Array<[FaultLogInfo](#faultloginfo)>> | Promise used to return the fault information array. You can obtain the fault information instance in its **then()** method or use **await**.<br>**value** is the fault information array obtained. If **value** is **undefined**, an exception occurs during the information retrieval.| 226 227**Example** 228 229```ts 230import faultLogger from '@ohos.faultLogger' 231 232async function getLog() { 233 let value: Array<faultLogger.FaultLogInfo> = await faultLogger.querySelfFaultLog(faultLogger.FaultType.JS_CRASH); 234 if (value) { 235 console.info("value length is " + value.length); 236 let len: number = value.length; 237 for (let i = 0; i < len; i++) { 238 console.info("log: " + i); 239 console.info("Log pid: " + value[i].pid); 240 console.info("Log uid: " + value[i].uid); 241 console.info("Log type: " + value[i].type); 242 console.info("Log timestamp: " + value[i].timestamp); 243 console.info("Log reason: " + value[i].reason); 244 console.info("Log module: " + value[i].module); 245 console.info("Log summary: " + value[i].summary); 246 console.info("Log text: " + value[i].fullLog); 247 } 248 } 249} 250``` 251