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 '@kit.PerformanceAnalysisKit';
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| Millisecond-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&lt;Array&lt;FaultLogInfo&gt;&gt;) : 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&lt;Array&lt;[FaultLogInfo](#faultloginfo)&gt;&gt; | 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](errorcode-faultlogger.md).
61
62| ID| Error Message|
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**Example**
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&lt;Array&lt;FaultLogInfo&gt;&gt;
103
104Obtains 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.
105
106**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
107
108**Parameters**
109
110| Name| Type| Mandatory| Description|
111| -------- | -------- | -------- | -------- |
112| faultType | [FaultType](#faulttype) | Yes| Fault type.|
113
114**Return value**
115
116| Type| Description|
117| -------- | -------- |
118| Promise&lt;Array&lt;[FaultLogInfo](#faultloginfo)&gt;&gt; | 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.|
119
120**Error codes**
121
122For details about the error codes, see [FaultLogger Error Codes](errorcode-faultlogger.md).
123
124| ID| Error Message|
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**Example**
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&lt;Array&lt;FaultLogInfo&gt;&gt;) : void
163
164> **NOTE**
165>
166> This API is deprecated since API version 9. You are advised to use [FaultLogger.query](#faultloggerquery9).
167
168Obtains 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.
169
170**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
171
172**Parameters**
173
174| Name| Type| Mandatory| Description|
175| -------- | -------- | -------- | -------- |
176| faultType | [FaultType](#faulttype) | Yes| Fault type.|
177| callback | AsyncCallback&lt;Array&lt;[FaultLogInfo](#faultloginfo)&gt;&gt; | 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.
178
179**Example**
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&lt;Array&lt;FaultLogInfo&gt;&gt;
210
211> **NOTE**
212>
213> This API is deprecated since API version 9. You are advised to use [FaultLogger.query](#faultloggerquery9-1).
214
215Obtains 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.
216
217**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
218
219**Parameters**
220
221| Name| Type| Mandatory| Description|
222| -------- | -------- | -------- | -------- |
223| faultType | [FaultType](#faulttype) | Yes| Fault type.|
224
225**Return value**
226
227| Type| Description|
228| -------- | -------- |
229| Promise&lt;Array&lt;[FaultLogInfo](#faultloginfo)&gt;&gt; | 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.|
230
231**Example**
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```
255