• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.hiSysEvent (System Event Logging) (System API)
2
3<!--Kit: Performance Analysis Kit-->
4<!--Subsystem: HiviewDFX-->
5<!--Owner: @lyj_love_code-->
6<!--SE: @tangyyan-->
7<!--TSE: @gcw_KuLfPSbe-->
8
9The **hiSysEvent** module provides the system event logging functions, such as configuring trace points, subscribing to system events, and querying system events written to the event file.
10
11> **NOTE**
12>
13> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
14> - The APIs provided by this module are system APIs.
15
16## Modules to Import
17
18```ts
19import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
20```
21
22## EventType
23
24Enumerates event types.
25
26**System capability**: SystemCapability.HiviewDFX.HiSysEvent
27
28| Name| Value| Description|
29| -------- | -------- | -------- |
30| FAULT | 1 | Error event.|
31| STATISTIC | 2 | Statistic event.|
32| SECURITY | 3 | Security event.|
33| BEHAVIOR | 4 | User behavior event.|
34
35## SysEventInfo
36
37Defines a system event.
38
39**System capability**: SystemCapability.HiviewDFX.HiSysEvent
40
41| Name| Type| Read-Only| Optional| Description|
42| -------- | -------- | -------- | -------- | -------- |
43| domain | string | No| No| Event domain.|
44| name | string | No| No| Event name.|
45| eventType | [EventType](#eventtype) | No| No| Event type.|
46| params | object | No| Yes| Event parameters.|
47
48
49## hiSysEvent.write
50
51write(info: SysEventInfo, callback: AsyncCallback&lt;void&gt;): void
52
53Writes event information to the event file. This API uses an asynchronous callback to return the result.
54
55**System capability**: SystemCapability.HiviewDFX.HiSysEvent
56
57**Parameters**
58
59| Name   | Type                     | Mandatory| Description                                                        |
60| --------- | ------------------------- | ---- | ------------------------------------------------------------ |
61| info | [SysEventInfo](#syseventinfo) | Yes| System event information.|
62| callback  | AsyncCallback&lt;void&gt; | Yes| Callback used to process the received return value.<br>- Value **0**: The event verification is successful, and the event will be written to the event file asynchronously. <br>- A value greater than **0**: Invalid parameters are present in the event, and the event will be written to the event file asynchronously after the invalid parameters are ignored.<br>- A value smaller than **0**: The event parameter verification fails, and the event will not be written to the event file.|
63
64**Error codes**
65
66For details about the following error codes, see [HiSysEvent Error Codes](errorcode-hisysevent-sys.md).
67
68| ID| Error Message|
69| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
70| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
71| 11200001 | Invalid event domain.                                                                                                                           |
72| 11200002 | Invalid event name.                                                                                                                             |
73| 11200003 | Abnormal environment.                                                                                                                           |
74| 11200004 | The event length exceeds the limit.                                                                                                             |
75| 11200051 | Invalid event parameter.                                                                                                                        |
76| 11200052 | The size of the event parameter of the string type exceeds the limit.                                                                           |
77| 11200053 | The number of event parameters exceeds the limit.                                                                                               |
78| 11200054 | The number of event parameters of the array type exceeds the limit.                                                                             |
79
80**Example**
81
82```ts
83import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
84import { BusinessError } from '@kit.BasicServicesKit';
85
86try {
87  let customizedParams: Record<string, string | number> = {
88    'PID': 487,
89    'UID': 103,
90    'PACKAGE_NAME': "com.ohos.hisysevent.test",
91    'PROCESS_NAME': "syseventservice",
92    'MSG': "no msg."
93  };
94  let eventInfo: hiSysEvent.SysEventInfo = {
95    domain: "RELIABILITY",
96    name: "STACK",
97    eventType: hiSysEvent.EventType.FAULT,
98    params: customizedParams
99  };
100  hiSysEvent.write(eventInfo, (err: BusinessError) => {
101    // do something here.
102  });
103} catch (err) {
104  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
105}
106```
107
108
109## hiSysEvent.write
110
111write(info: SysEventInfo): Promise&lt;void&gt;
112
113Writes event information to the event file. This API uses a promise to return the result.
114
115**System capability**: SystemCapability.HiviewDFX.HiSysEvent
116
117**Parameters**
118
119| Name   | Type                   | Mandatory| Description|
120| --------- | ----------------------- | ---- | --------------- |
121| info | [SysEventInfo](#syseventinfo) | Yes  | System event information.|
122
123**Return value**
124
125| Type               | Description                                                        |
126| ------------------- | ------------------------------------------------------------ |
127| Promise&lt;void&gt; | Promise used to return the result. Depending on whether event writing is successful, you can use the **then()** or **catch()** method to process the callback.|
128
129**Error codes**
130
131For details about the following error codes, see [HiSysEvent Error Codes](errorcode-hisysevent-sys.md).
132
133| ID| Error Message|
134| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
135| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
136| 11200001 | Invalid event domain.                                                                                                                           |
137| 11200002 | Invalid event name.                                                                                                                             |
138| 11200003 | Abnormal environment.                                                                                                                           |
139| 11200004 | The event length exceeds the limit.                                                                                                             |
140| 11200051 | Invalid event parameter.                                                                                                                        |
141| 11200052 | The size of the event parameter of the string type exceeds the limit.                                                                           |
142| 11200053 | The number of event parameters exceeds the limit.                                                                                               |
143| 11200054 | The number of event parameters of the array type exceeds the limit.                                                                             |
144
145**Example**
146
147```ts
148import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
149import { BusinessError } from '@kit.BasicServicesKit';
150
151try {
152  let customizedParams: Record<string, string | number> = {
153    'PID': 487,
154    'UID': 103,
155    'PACKAGE_NAME': "com.ohos.hisysevent.test",
156    'PROCESS_NAME': "syseventservice",
157    'MSG': "no msg."
158  };
159  let eventInfo: hiSysEvent.SysEventInfo = {
160    domain: "RELIABILITY",
161    name: "STACK",
162    eventType: hiSysEvent.EventType.FAULT,
163    params: customizedParams
164  };
165  hiSysEvent.write(eventInfo).then(
166    () => {
167      // do something here.
168    }
169  ).catch(
170    (err: BusinessError) => {
171      console.error(`error code: ${err.code}, error msg: ${err.message}`);
172    }
173  );
174} catch (err) {
175  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
176}
177```
178
179## RuleType
180
181Enumerates matching rule types.
182
183**System capability**: SystemCapability.HiviewDFX.HiSysEvent
184
185| Name| Value| Description|
186| -------- | -------- | -------- |
187| WHOLE_WORD | 1 | Whole word matching.|
188| PREFIX | 2 | Prefix matching.|
189| REGULAR | 3 | Regular expression matching.|
190
191## WatchRule
192
193Defines event subscription rules.
194
195**System capability**: SystemCapability.HiviewDFX.HiSysEvent
196
197| Name| Type| Read-Only| Optional| Description|
198| -------- | -------- | -------- | -------- | -------- |
199| domain | string | No| No| Event domain.|
200| name | string | No| No| Event name.|
201| tag | string | No| Yes| Event tag.|
202| ruleType | [RuleType](#ruletype) | No| No| Matching rule type.|
203
204## Watcher
205
206Defines a watcher for event subscription.
207
208**System capability**: SystemCapability.HiviewDFX.HiSysEvent
209
210| Name| Type| Read-Only| Optional| Description|
211| -------- | -------- | -------- | -------- | -------- |
212| rules | [WatchRule](#watchrule)[] | No| No| Array of matching event subscription rules.|
213| onEvent | function | No| No| Callback for event subscription: (info: [SysEventInfo](#syseventinfo)) => void|
214| onServiceDied | function | No| No| Callback for disabling of event subscription: () => void|
215
216## hiSysEvent.addWatcher
217
218addWatcher(watcher: Watcher): void
219
220Adds a watcher for event subscription.
221
222**Required permission**: ohos.permission.READ_DFX_SYSEVENT
223
224**System capability**: SystemCapability.HiviewDFX.HiSysEvent
225
226**Parameters**
227
228| Name| Type| Mandatory| Description|
229| ------ | ----------------------------- | ---- | ------------------------ |
230| watcher | [Watcher](#watcher) | Yes| Watcher for event subscription.|
231
232**Error codes**
233
234For details about the following error codes, see [HiSysEvent Error Codes](errorcode-hisysevent-sys.md).
235
236| ID| Error Message|
237| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
238| 201      | Permission denied. An attempt was made to read system event forbidden by permission: ohos.permission.READ_DFX_SYSEVENT.                         |
239| 202      | System API is not allowed called by Non-system application.                                                                                     |
240| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
241| 11200101 | The number of watchers exceeds the limit.                                                                                                       |
242| 11200102 | The number of watch rules exceeds the limit.                                                                                                    |
243
244**Example**
245
246```ts
247import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
248import { BusinessError } from '@kit.BasicServicesKit';
249
250let watchRules: hiSysEvent.WatchRule[] = [{
251    domain: "RELIABILITY",
252    name: "STACK",
253    tag: "STABILITY",
254    ruleType: hiSysEvent.RuleType.WHOLE_WORD,
255  } as hiSysEvent.WatchRule];
256let watcher: hiSysEvent.Watcher = {
257  rules: watchRules,
258  onEvent: (info: hiSysEvent.SysEventInfo) => {
259    // do something here.
260  },
261  onServiceDied: () => {
262    // do something here.
263  }
264};
265try {
266  hiSysEvent.addWatcher(watcher);
267} catch (err) {
268  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
269}
270```
271
272## hiSysEvent.removeWatcher
273
274removeWatcher(watcher: Watcher): void
275
276Removes a watcher used for event subscription.
277
278**Required permission**: ohos.permission.READ_DFX_SYSEVENT
279
280**System capability**: SystemCapability.HiviewDFX.HiSysEvent
281
282**Parameters**
283
284| Name| Type | Mandatory| Description |
285| ------ | ------------- | ---- | ------------------------- |
286| watcher | [Watcher](#watcher) | Yes| Watcher for event subscription.|
287
288**Error codes**
289
290For details about the following error codes, see [HiSysEvent Error Codes](errorcode-hisysevent-sys.md).
291
292| ID| Error Message|
293| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
294| 201      | Permission denied. An attempt was made to read system event forbidden by permission: ohos.permission.READ_DFX_SYSEVENT.                         |
295| 202      | System API is not allowed called by Non-system application.                                                                                     |
296| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
297| 11200201 | The watcher does not exist.                                                                                                                     |
298
299**Example**
300
301```ts
302import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
303import { BusinessError } from '@kit.BasicServicesKit';
304
305let watchRules: hiSysEvent.WatchRule[] = [{
306    domain: "RELIABILITY",
307    name: "STACK",
308    tag: "STABILITY",
309    ruleType: hiSysEvent.RuleType.WHOLE_WORD,
310  } as hiSysEvent.WatchRule ];
311let watcher: hiSysEvent.Watcher = {
312  rules: watchRules,
313  onEvent: (info: hiSysEvent.SysEventInfo) => {
314    // do something here.
315  },
316  onServiceDied: () => {
317    // do something here.
318  }
319};
320try {
321  hiSysEvent.addWatcher(watcher);
322  hiSysEvent.removeWatcher(watcher);
323} catch (err) {
324  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
325}
326```
327
328## QueryArg
329
330Defines arguments for an event query.
331
332**System capability**: SystemCapability.HiviewDFX.HiSysEvent
333
334| Name| Type| Read-Only| Optional| Description|
335| -------- | -------- | -------- | -------- | -------- |
336| beginTime | number | No| No| Start time (13-digit timestamp) for the event query.|
337| endTime | number | No| No| End time (13-digit timestamp) for the event query.|
338| maxEvents | number | No| No| Maximum number of events that can be queried.|
339| fromSeq<sup>10+</sup> | number | No| Yes| Start SN of the events to be queried. The default value is **-1**.|
340| toSeq<sup>10+</sup> | number | No| Yes| End SN of the system events to be queried. The default value is **-1**.|
341
342## QueryRule
343
344Defines event query rules.
345
346**System capability**: SystemCapability.HiviewDFX.HiSysEvent
347
348| Name| Type| Read-Only| Optional| Description|
349| -------- | -------- | -------- | -------- | -------- |
350| domain | string | No| No| Event domain.|
351| names | string[] | No| No| Array of event names. A **QueryRule** object contains multiple system event names.|
352| condition<sup>10+</sup> | string | No| Yes| Additional event conditions. The value of this parameter is in the format of {"version":"V1","condition":{"and":[{"param":"*Parameter*","op":"*Operator*","value":"*Comparison value*"}]}}.|
353
354## Querier
355
356Defines an event query instance.
357
358**System capability**: SystemCapability.HiviewDFX.HiSysEvent
359
360| Name| Type| Read-Only| Optional| Description|
361| -------- | -------- | -------- | -------- | -------- |
362| onQuery | function | No| No| Callback used to return the queried system events: (infos: [SysEventInfo](#syseventinfo)[]) => void.|
363| onComplete | function | No| No| Callback used to return the query result statistics: (reason: number, total: number) => void|
364
365## hiSysEvent.query
366
367query(queryArg: QueryArg, rules: QueryRule[], querier: Querier): void
368
369Queries system events.
370
371**Required permission**: ohos.permission.READ_DFX_SYSEVENT
372
373**System capability**: SystemCapability.HiviewDFX.HiSysEvent
374
375**Parameters**
376
377| Name| Type| Mandatory| Description|
378| ------ | ----------------------------- | ---- | ------------------------ |
379| queryArg | [QueryArg](#queryarg) | Yes  | Arguments for event query.|
380| rules | [QueryRule](#queryrule)[] | Yes  | Array of event query rules.|
381| querier | [Querier](#querier) | Yes  | Event query instance.|
382
383**Error codes**
384
385For details about the following error codes, see [HiSysEvent Error Codes](errorcode-hisysevent-sys.md).
386
387| ID| Error Message|
388| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
389| 201      | Permission denied. An attempt was made to read system event forbidden by permission: ohos.permission.READ_DFX_SYSEVENT.                         |
390| 202      | System API is not allowed called by Non-system application.                                                                                     |
391| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
392| 11200301 | The number of query rules exceeds the limit.                                                                                                    |
393| 11200302 | Invalid query rule.                                                                                                                             |
394| 11200303 | The number of concurrent queriers exceeds the limit.                                                                                            |
395| 11200304 | The query frequency exceeds the limit.                                                                                                          |
396
397**Example**
398
399```ts
400import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
401import { BusinessError } from '@kit.BasicServicesKit';
402
403try {
404  let customizedParams: Record<string, string | number> = {
405    'PID': 487,
406    'UID': 103,
407    'PACKAGE_NAME': "com.ohos.hisysevent.test",
408    'PROCESS_NAME': "syseventservice",
409    'MSG': "no msg."
410  };
411  let eventInfo: hiSysEvent.SysEventInfo = {
412    domain: "RELIABILITY",
413    name: "STACK",
414    eventType: hiSysEvent.EventType.FAULT,
415    params: customizedParams
416  };
417  hiSysEvent.write(eventInfo, (err: BusinessError) => {
418    // do something here.
419  });
420
421  let queryArg: hiSysEvent.QueryArg = {
422    beginTime: -1,
423    endTime: -1,
424    maxEvents: 5,
425  };
426  let queryRules: hiSysEvent.QueryRule[] = [{
427    domain: "RELIABILITY",
428    names: ["STACK"],
429  } as hiSysEvent.QueryRule];
430  let querier: hiSysEvent.Querier = {
431    onQuery: (infos: hiSysEvent.SysEventInfo[]) => {
432      // do something here.
433    },
434    onComplete: (reason: number, total: number) => {
435      // do something here.
436    }
437  };
438  hiSysEvent.query(queryArg, queryRules, querier);
439} catch (err) {
440  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
441}
442```
443
444## hiSysEvent.exportSysEvents<sup>10+</sup>
445
446exportSysEvents(queryArg: QueryArg, rules: QueryRule[]): number
447
448Exports system events in batches and writes them as a file to the fixed directory of the application sandbox (that is, **/data/storage/el2/base/cache/hiview/event/**).
449
450**Required permission**: ohos.permission.READ_DFX_SYSEVENT
451
452**System capability**: SystemCapability.HiviewDFX.HiSysEvent
453
454**Parameters**
455
456| Name  | Type                     | Mandatory| Description                                      |
457| -------- | ------------------------- | ---- | ------------------------------------------ |
458| queryArg | [QueryArg](#queryarg)     | Yes  | Event query parameters for the export.                  |
459| rules    | [QueryRule](#queryrule)[] | Yes  | Array of event query rules for the export.|
460
461**Return value**
462
463| Type  | Description            |
464| ------ | ---------------- |
465| number | API call timestamp.|
466
467**Error codes**
468
469For details about the following error codes, see [HiSysEvent Error Codes](errorcode-hisysevent-sys.md).
470
471| ID| Error Message|
472| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
473| 201      | Permission denied. An attempt was made to read system event forbidden by permission: ohos.permission.READ_DFX_SYSEVENT.                         |
474| 202      | System API is not allowed called by Non-system application.                                                                                     |
475| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
476| 11200301 | The number of query rules exceeds the limit.                                                                                                    |
477| 11200302 | Invalid query rule.                                                                                                                             |
478| 11200304 | The query frequency exceeds the limit.                                                                                                          |
479
480**Example**
481
482```ts
483import { fileIo } from '@kit.CoreFileKit';
484import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
485import { BusinessError } from '@kit.BasicServicesKit';
486
487try {
488  let customizedParams: Record<string, string | number> = {
489    'PID': 487,
490    'UID': 103,
491    'PACKAGE_NAME': "com.ohos.hisysevent.test",
492    'PROCESS_NAME': "syseventservice",
493    'MSG': "no msg."
494  };
495  let eventInfo: hiSysEvent.SysEventInfo = {
496    domain: "RELIABILITY",
497    name: "STACK",
498    eventType: hiSysEvent.EventType.FAULT,
499    params: customizedParams
500  };
501  hiSysEvent.write(eventInfo, (err: BusinessError) => {
502    // do something here.
503  });
504
505  let queryArg: hiSysEvent.QueryArg = {
506    beginTime: -1,
507    endTime: -1,
508    maxEvents: 1,
509  };
510  let queryRules: hiSysEvent.QueryRule[] = [{
511    domain: "RELIABILITY",
512    names: ["STACK"],
513  } as hiSysEvent.QueryRule];
514  let time = hiSysEvent.exportSysEvents(queryArg, queryRules);
515  console.log(`receive export task time is : ${time}`);
516
517  // Postpone reading of exported events.
518  setTimeout(() => {
519    let eventDir = '/data/storage/el2/base/cache/hiview/event';
520    let filenames = fileIo.listFileSync(eventDir);
521    for (let i = 0; i < filenames.length; i++) {
522      if (filenames[i].indexOf(time.toString()) != -1) {
523        let res = fileIo.readTextSync(eventDir + '/' + filenames[i]);
524        let events: string = JSON.parse('[' + res.slice(0, res.length - 1) + ']');
525        console.log("read file end, events is :" + JSON.stringify(events));
526      }
527    }
528  }, 10000);
529} catch (err) {
530  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
531}
532```
533
534## hiSysEvent.subscribe<sup>10+</sup>
535
536subscribe(rules: QueryRule[]): number
537
538Subscribes to real-time system events that occur occasionally or occur in a low frequency. These events are written as a file to the fixed directory of the application sandbox (that is, **/data/storage/el2/base/cache/hiview/event/**).
539
540**Required permission**: ohos.permission.READ_DFX_SYSEVENT
541
542**System capability**: SystemCapability.HiviewDFX.HiSysEvent
543
544**Parameters**
545
546| Name| Type                     | Mandatory| Description                                      |
547| ------ | ------------------------- | ---- | ------------------------------------------ |
548| rules  | [QueryRule](#queryrule)[] | Yes  | Array of event query rules for the subscription.|
549
550**Return value**
551
552| Type  | Description            |
553| ------ | ---------------- |
554| number | API call timestamp.|
555
556**Error codes**
557
558For details about the following error codes, see [HiSysEvent Error Codes](errorcode-hisysevent-sys.md).
559
560| ID| Error Message|
561| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
562| 201      | Permission denied. An attempt was made to read system event forbidden by permission: ohos.permission.READ_DFX_SYSEVENT.                         |
563| 202      | System API is not allowed called by Non-system application.                                                                                     |
564| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
565| 11200301 | The number of query rules exceeds the limit.                                                                                                    |
566| 11200302 | Invalid query rule.                                                                                                                             |
567
568**Example**
569
570```ts
571import { fileIo } from '@kit.CoreFileKit';
572import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
573import { BusinessError } from '@kit.BasicServicesKit';
574
575try {
576  let rules: hiSysEvent.QueryRule[] = [{
577    domain: "RELIABILITY",
578    names: ["STACK"],
579  } as hiSysEvent.QueryRule,
580  {
581    domain: "BUNDLE_MANAGER",
582    names: ["BUNDLE_UNINSTALL"],
583  } as hiSysEvent.QueryRule];
584  hiSysEvent.subscribe(rules);
585
586  let customizedParams: Record<string, string | number> = {
587    'PID': 487,
588    'UID': 103,
589    'PACKAGE_NAME': "com.ohos.hisysevent.test",
590    'PROCESS_NAME': "syseventservice",
591    'MSG': "no msg."
592  };
593  let eventInfo: hiSysEvent.SysEventInfo = {
594    domain: "RELIABILITY",
595    name: "STACK",
596    eventType: hiSysEvent.EventType.FAULT,
597    params: customizedParams
598  };
599  hiSysEvent.write(eventInfo, (err: BusinessError) => {
600    // do something here.
601  });
602
603  // Postpone reading of subscribed events.
604  setTimeout(() => {
605    let eventDir = '/data/storage/el2/base/cache/hiview/event';
606    let filenames = fileIo.listFileSync(eventDir);
607    for (let i = 0; i < filenames.length; i++) {
608      let res = fileIo.readTextSync(eventDir + '/' + filenames[i]);
609      let events: string = JSON.parse('[' + res.slice(0, res.length - 1) + ']');
610      console.log("read file end, events is :" + JSON.stringify(events));
611    }
612  }, 10000);
613} catch (err) {
614  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
615}
616```
617
618## hiSysEvent.unsubscribe<sup>10+</sup>
619
620unsubscribe(): void
621
622Unsubscribes from system events.
623
624**Required permission**: ohos.permission.READ_DFX_SYSEVENT
625
626**System capability**: SystemCapability.HiviewDFX.HiSysEvent
627
628**Error codes**
629
630For details about the following error codes, see [HiSysEvent Error Codes](errorcode-hisysevent-sys.md).
631
632| ID| Error Message|
633| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
634| 201      | Permission denied. An attempt was made to read system event forbidden by permission: ohos.permission.READ_DFX_SYSEVENT.                         |
635| 202      | System API is not allowed called by Non-system application.                                                                                     |
636| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
637| 11200305 | Failed to unsubscribe.                                                                                                                          |
638
639**Example**
640
641```ts
642import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
643import { BusinessError } from '@kit.BasicServicesKit';
644
645try {
646  let rules: hiSysEvent.QueryRule[] = [{
647    domain: "RELIABILITY",
648    names: ["STACK"],
649  } as hiSysEvent.QueryRule,
650  {
651    domain: "BUNDLE_MANAGER",
652    names: ["BUNDLE_UNINSTALL"],
653  } as hiSysEvent.QueryRule];
654  hiSysEvent.subscribe(rules);
655  hiSysEvent.unsubscribe();
656} catch (err) {
657  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
658}
659```
660