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```js 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>The value is the fault information array obtained. If the 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```js 69function queryFaultLogCallback(error, value) { 70 if (error) { 71 console.info('error is ' + error); 72 } else { 73 console.info("value length is " + value.length); 74 let len = value.length; 75 for (let i = 0; i < len; i++) { 76 console.info("log: " + i); 77 console.info("Log pid: " + value[i].pid); 78 console.info("Log uid: " + value[i].uid); 79 console.info("Log type: " + value[i].type); 80 console.info("Log timestamp: " + value[i].timestamp); 81 console.info("Log reason: " + value[i].reason); 82 console.info("Log module: " + value[i].module); 83 console.info("Log summary: " + value[i].summary); 84 console.info("Log text: " + value[i].fullLog); 85 } 86 } 87} 88try { 89 faultLogger.query(faultLogger.FaultType.JS_CRASH, queryFaultLogCallback); 90} catch (err) { 91 console.error(`code: ${err.code}, message: ${err.message}`); 92} 93``` 94 95## faultLogger.query<sup>9+</sup> 96 97query(faultType: FaultType) : Promise<Array<FaultLogInfo>> 98 99Obtains 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. 100 101**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger 102 103**Parameters** 104 105| Name| Type| Mandatory| Description| 106| -------- | -------- | -------- | -------- | 107| faultType | [FaultType](#faulttype) | Yes| Fault type.| 108 109**Return value** 110 111| Type| Description| 112| -------- | -------- | 113| 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>The value is the fault information array obtained. If the value is **undefined**, an exception occurs during the information retrieval.| 114 115**Error codes** 116 117For details about the error codes, see [FaultLogger Error Codes](../errorcodes/errorcode-faultlogger.md). 118 119| ID| Error Message| 120| --- | --- | 121| 10600001 | The service is not started or is faulty | 122 123**Example** 124 125```js 126async function getLog() { 127 try { 128 let value = await faultLogger.query(faultLogger.FaultType.JS_CRASH); 129 if (value) { 130 console.info("value length is " + value.length); 131 let len = value.length; 132 for (let i = 0; i < len; i++) { 133 console.info("log: " + i); 134 console.info("Log pid: " + value[i].pid); 135 console.info("Log uid: " + value[i].uid); 136 console.info("Log type: " + value[i].type); 137 console.info("Log timestamp: " + value[i].timestamp); 138 console.info("Log reason: " + value[i].reason); 139 console.info("Log module: " + value[i].module); 140 console.info("Log summary: " + value[i].summary); 141 console.info("Log text: " + value[i].fullLog); 142 } 143 } 144 } catch (err) { 145 console.error(`code: ${err.code}, message: ${err.message}`); 146 } 147} 148``` 149 150## faultLogger.querySelfFaultLog<sup>(deprecated)</sup> 151 152querySelfFaultLog(faultType: FaultType, callback: AsyncCallback<Array<FaultLogInfo>>) : void 153 154> **NOTE** 155> 156> This API is deprecated since API version 9. You are advised to use [faultLogger.query](#faultloggerquery9) instead. 157 158Obtains 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. 159 160**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger 161 162**Parameters** 163 164| Name| Type| Mandatory| Description| 165| -------- | -------- | -------- | -------- | 166| faultType | [FaultType](#faulttype) | Yes| Fault type.| 167| callback | AsyncCallback<Array<[FaultLogInfo](#faultloginfo)>> | Yes| Callback used to return the fault information array.<br>The value is the fault information array obtained. If the value is **undefined**, an exception occurs during the information retrieval. In this case, an error string will be returned. 168 169**Example** 170 171```js 172function queryFaultLogCallback(error, value) { 173 if (error) { 174 console.info('error is ' + error); 175 } else { 176 console.info("value length is " + value.length); 177 let len = value.length; 178 for (let i = 0; i < len; i++) { 179 console.info("log: " + i); 180 console.info("Log pid: " + value[i].pid); 181 console.info("Log uid: " + value[i].uid); 182 console.info("Log type: " + value[i].type); 183 console.info("Log timestamp: " + value[i].timestamp); 184 console.info("Log reason: " + value[i].reason); 185 console.info("Log module: " + value[i].module); 186 console.info("Log summary: " + value[i].summary); 187 console.info("Log text: " + value[i].fullLog); 188 } 189 } 190} 191faultLogger.querySelfFaultLog(faultLogger.FaultType.JS_CRASH, queryFaultLogCallback); 192``` 193 194## faultLogger.querySelfFaultLog<sup>(deprecated)</sup> 195 196querySelfFaultLog(faultType: FaultType) : Promise<Array<FaultLogInfo>> 197 198> **NOTE** 199> 200> This API is deprecated since API version 9. You are advised to use [faultLogger.query](#faultloggerquery9-1) instead. 201 202Obtains 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. 203 204**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger 205 206**Parameters** 207 208| Name| Type| Mandatory| Description| 209| -------- | -------- | -------- | -------- | 210| faultType | [FaultType](#faulttype) | Yes| Fault type.| 211 212**Return value** 213 214| Type| Description| 215| -------- | -------- | 216| 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>The value is the fault information array obtained. If the value is **undefined**, an exception occurs during the information retrieval.| 217 218**Example** 219 220```js 221async function getLog() { 222 let value = await faultLogger.querySelfFaultLog(faultLogger.FaultType.JS_CRASH); 223 if (value) { 224 console.info("value length is " + value.length); 225 let len = value.length; 226 for (let i = 0; i < len; i++) { 227 console.info("log: " + i); 228 console.info("Log pid: " + value[i].pid); 229 console.info("Log uid: " + value[i].uid); 230 console.info("Log type: " + value[i].type); 231 console.info("Log timestamp: " + value[i].timestamp); 232 console.info("Log reason: " + value[i].reason); 233 console.info("Log module: " + value[i].module); 234 console.info("Log summary: " + value[i].summary); 235 console.info("Log text: " + value[i].fullLog); 236 } 237 } 238} 239``` 240