hiappevent-guidelines.md 6.8 KB
Newer Older
S
shawn_he 已提交
1
# Development of Application Event Logging
Z
zengyawen 已提交
2 3 4

## When to Use

S
shawn_he 已提交
5
The event logging function helps applications log various information generated during running.
Z
zengyawen 已提交
6 7 8 9 10

## Available APIs

JS application event logging APIs are provided by the **hiAppEvent** module.

S
shawn_he 已提交
11
The following table provides only a brief description of related APIs. For details, see [HiAppEvent](../reference/apis/js-apis-hiviewdfx-hiappevent.md).
Z
zengyawen 已提交
12

S
shawn_he 已提交
13
**Table 1** APIs for application event logging
Z
zengyawen 已提交
14

S
shawn_he 已提交
15 16 17 18
| API                                                      | Description                                                |
| ------------------------------------------------------------ | ---------------------------------------------------- |
| write(AppEventInfo info, AsyncCallback\<void> callback): void | Logs application events in asynchronous mode. This API uses an asynchronous callback to return the result.|
| write(AppEventInfo info): Promise\<void>                     | Logs application events in asynchronous mode. This API uses a promise to return the result. |
Z
zengyawen 已提交
19

S
shawn_he 已提交
20
**Table 2** APIs for event logging configuration 
S
shawn_he 已提交
21

S
shawn_he 已提交
22 23 24
| API                              | Description                                                |
| ------------------------------------ | ---------------------------------------------------- |
| configure(ConfigOption config): void | Sets the configuration options for application event logging.|
S
shawn_he 已提交
25

S
shawn_he 已提交
26
**Table 3** APIs for watcher management
Z
zengyawen 已提交
27

S
shawn_he 已提交
28 29 30 31
| API                                            | Description                |
| -------------------------------------------------- | -------------------- |
| addWatcher(Watcher watcher): AppEventPackageHolder | Adds an event watcher.|
| removeWatcher(Watcher watcher): void               | Removes an event watcher.|
Z
zengyawen 已提交
32

S
shawn_he 已提交
33
**Table 4** APIs for clearing logging data
Z
zengyawen 已提交
34

S
shawn_he 已提交
35 36 37 38
| API           | Description                |
| ----------------- | -------------------- |
| clearData(): void | Clears local logging data.|

S
shawn_he 已提交
39
## How to Develop
S
shawn_he 已提交
40 41 42

The following uses a one-time event watcher as an example to illustrate the development procedure.

S
shawn_he 已提交
43
1. Create an eTS application project. In the displayed **Project** window, choose **entry** > **src** > **main** > **ets** > **pages** > **index.ets**, and double-click **index.ets**. Then, add three buttons to simulate the process of watching for application events. Wherein, button 1 is used to invoke application event logging, button 2 to add an event watcher that automatically triggers a callback, and button 3 to remove the watcher. The complete sample code is as follows:
S
shawn_he 已提交
44 45

   ```ts
S
shawn_he 已提交
46
   import hiAppEvent from '@ohos.hiviewdfx.hiAppEvent';
Z
zengyawen 已提交
47
   
S
shawn_he 已提交
48 49 50 51
   @Entry
   @Component
   struct Index {
     @State message: string = 'Hello World'
Z
zengyawen 已提交
52
   
S
shawn_he 已提交
53 54 55 56 57 58 59 60
     build() {
       Row() {
         Column() {
           Text(this.message)
             .fontSize(50)
             .fontWeight(FontWeight.Bold)
   
           Button("1 writeTest").onClick(()=>{
S
shawn_he 已提交
61
             // Perform event logging based on the input event parameters.
S
shawn_he 已提交
62 63 64 65 66 67 68
             hiAppEvent.write({
               domain: "test_domain",
               name: "test_event",
               eventType: hiAppEvent.EventType.FAULT,
               params: {
                 int_data: 100,
                 str_data: "strValue"
Z
zengyawen 已提交
69
               }
S
shawn_he 已提交
70 71
             }).then(() => {
               console.log(`HiAppEvent success to write event`);
S
shawn_he 已提交
72
             }).catch((err) => {
S
shawn_he 已提交
73
               console.error(`code: ${err.code}, message: ${err.message}`);
S
shawn_he 已提交
74 75
             });
           })
Z
zengyawen 已提交
76
   
S
shawn_he 已提交
77
           Button("2 addWatcherTest").onClick(()=>{
S
shawn_he 已提交
78
             // Add an event watcher based on the input subscription parameters.
S
shawn_he 已提交
79 80 81 82 83 84 85 86 87
             hiAppEvent.addWatcher({
               name: "watcher1",
               appEventFilters: [{ domain: "test_domain" }],
               triggerCondition: {
                 row: 2,
                 size: 1000,
                 timeOut: 2
               },
               onTrigger: function (curRow, curSize, holder) {
S
shawn_he 已提交
88
                 // If the holder object is null, return an error after recording it in the log.
S
shawn_he 已提交
89 90 91 92
                 if (holder == null) {
                   console.error("HiAppEvent holder is null");
                   return;
                 }
S
shawn_he 已提交
93 94
                 // Set the size threshold to 1,000 bytes for obtaining an event package.
                 holder.setSize(1000);
S
shawn_he 已提交
95
                 let eventPkg = null;
S
shawn_he 已提交
96
                 // Obtain the event package based on the configured size threshold. If returned event package is null, all event data has been obtained.
S
shawn_he 已提交
97
                 while ((eventPkg = holder.takeNext()) != null) {
S
shawn_he 已提交
98
                   // Parse the obtained event package and display the result on the Log page.
S
shawn_he 已提交
99 100 101
                   console.info(`HiAppEvent eventPkg.packageId=${eventPkg.packageId}`);
                   console.info(`HiAppEvent eventPkg.row=${eventPkg.row}`);
                   console.info(`HiAppEvent eventPkg.size=${eventPkg.size}`);
S
shawn_he 已提交
102
                   // Traverse and parse event string arrays in the obtained event package.
S
shawn_he 已提交
103
                   for (const eventInfo of eventPkg.data) {
S
shawn_he 已提交
104
                     console.info(`HiAppEvent eventPkg.data=${eventInfo}`);
S
shawn_he 已提交
105 106 107 108 109
                   }
                 }
               }
             });
           })
Z
zengyawen 已提交
110
   
S
shawn_he 已提交
111
           Button("3 removeWatcherTest").onClick(()=>{
S
shawn_he 已提交
112
             // Remove the specified event watcher.
S
shawn_he 已提交
113 114 115 116 117 118
             hiAppEvent.removeWatcher({
               name: "watcher1"
             })
           })
         }
         .width('100%')
Z
zengyawen 已提交
119
       }
S
shawn_he 已提交
120 121
       .height('100%')
     }
Z
zengyawen 已提交
122 123 124
   }
   ```

S
shawn_he 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
2. Touch the run button on the IDE to run the project.

3. Touch button 1 on the application UI to start application event logging. If the logging is successful, you'll see the following message in the **Log** window:

   ```
   success to write event: 0
   ```

4. On the application UI, touch button 2 to add an event watcher, and touch button 1 for multiple times to perform application event logging. If any callback trigger condition (event count, event data size, and timeout duration) is met, the event watcher will invoke a callback and the event package obtained through the callback will be displayed in the **Log** window.

   ```
   HiAppEvent eventPkg.packageId=0
   HiAppEvent eventPkg.row=2
   HiAppEvent eventPkg.size=308
   HiAppEvent eventPkg.data={"domain_":"test_domain","name_":"test_event","type_":1,"time_":1502096107556,"tz_":"+0000","pid_":4204,"tid_":4223,"int_data":100,"str_data":"strValue"}
   ```

5. On the application UI, touch button 3 to remove the event watcher. Then, touch button 1 for multiple times to perform application event logging. In such a case, there will be no log information about the callback invoked by the event watcher.

## Samples

The following sample is provided to help you better understand how to develop the application event logging feature:

S
shawn_he 已提交
148
- [`JsDotTest`: Event Logging (JS) (API8)](https://gitee.com/openharmony/applications_app_samples/tree/master/DFX/JsDotTest)