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