js-apis-hiappevent.md 8.9 KB
Newer Older
S
shawn_he 已提交
1
# HiAppEvent
Z
zengyawen 已提交
2

S
shawn_he 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
5

S
shawn_he 已提交
6 7

## Modules to Import
Z
zengyawen 已提交
8

L
lyj_love_code 已提交
9
```js
Z
zengyawen 已提交
10 11 12
import hiAppEvent from '@ohos.hiAppEvent';
```

S
shawn_he 已提交
13 14 15 16

## hiAppEvent.write

write(eventName: string, eventType: EventType, keyValues: object, callback: AsyncCallback<void>): void
Z
zengyawen 已提交
17

S
shawn_he 已提交
18 19 20
Writes event information to the event file of the current day. This API supports JSON parameters and uses an asynchronous callback to return the result.

**System capability**: SystemCapability.HiviewDFX.HiAppEvent
Z
zengyawen 已提交
21

S
shawn_he 已提交
22 23
**Parameters**

S
shawn_he 已提交
24 25 26 27 28 29
| Name   | Type                     | Mandatory| Description                                                        |
| --------- | ------------------------- | ---- | ------------------------------------------------------------ |
| eventName | string                    | Yes  | App event name.<br>You need to customize the event name. It can contain a maximum of 48 characters, including digits (0 to 9), letters (a to z), and underscores (\_). It must start with a letter.|
| eventType | [EventType](#eventtype)   | Yes  | Application event type.                                              |
| keyValues | object                    | Yes  | Parameter key-value pair. For a variable-length parameter, enter each pair of parameter name and value in sequence. For a JSON parameter, enter the parameter name as the key and parameter value as the value.<br>- A key must be a string, and a value must be a string, number, boolean, or Array (which can only be a string, number, or boolean).<br>- The number of event parameters must be less than or equal to 32.<br>- The parameter name can contain a maximum of 16 characters, including digits (0 to 9), letters (a to z), and underscores (\_). It must start with a letter and cannot end with an underscore (\_).<br>- A string value can contain a maximum of 8*1024 characters.<br>- An array value can contain a maximum of 100 elements. Excess elements will be truncated.|
| callback  | AsyncCallback&lt;void&gt; | No  | Callback used to process the received return value.<br>-&nbsp; Value **0** indicates that the event verification is successful, and the event will be written to the event file asynchronously. <br>-&nbsp; A value greater than **0** indicates that 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>-&nbsp; A value smaller than **0** indicates that the event verification fails, and the event will not be written to the event file.|
S
shawn_he 已提交
30 31 32

**Example**

L
lyj_love_code 已提交
33
```js
S
shawn_he 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
hiAppEvent.write("test_event", hiAppEvent.EventType.FAULT, {"int_data":100, "str_data":"strValue"}, (err, value) => {
    if (err) {
        // Event writing exception: Write the event to the event file after the invalid parameters in the event are ignored, or stop writing the event if the event verification fails.
        console.error(`failed to write event because ${err.code}`);
        return;
    }

    // Event writing succeeded.
    console.log(`success to write event: ${value}`);
});
```


## hiAppEvent.write

write(eventName: string, eventType: EventType, keyValues: object): Promise&lt;void&gt;

S
shawn_he 已提交
51 52 53
Writes event information to the event file of the current day. This API supports JSON parameters and uses a promise to return the result.

**System capability**: SystemCapability.HiviewDFX.HiAppEvent
S
shawn_he 已提交
54 55 56

**Parameters**

S
shawn_he 已提交
57 58 59 60 61
| Name   | Type                   | Mandatory| Description                                                        |
| --------- | ----------------------- | ---- | ------------------------------------------------------------ |
| eventName | string                  | Yes  | App event name.<br>You need to customize the event name. It can contain a maximum of 48 characters, including digits (0 to 9), letters (a to z), and underscores (\_). It must start with a letter.|
| eventType | [EventType](#eventtype) | Yes  | Application event type.                                              |
| keyValues | object                  | Yes  | Parameter key-value pair. For a variable-length parameter, enter each pair of parameter name and value in sequence. For a JSON parameter, enter the parameter name as the key and parameter value as the value.<br>- A key must be a string, and a value must be a string, number, boolean, or Array (which can only be a string, number, or boolean).<br>- The number of event parameters must be less than or equal to 32.<br>- The parameter name can contain a maximum of 16 characters, including digits (0 to 9), letters (a to z), and underscores (\_). It must start with a letter and cannot end with an underscore (\_).<br>- A string value can contain a maximum of 8*1024 characters.<br>- An array value can contain a maximum of 100 elements. Excess elements will be truncated.|
S
shawn_he 已提交
62 63 64

**Return value**

S
shawn_he 已提交
65 66
| Type               | Description                                                        |
| ------------------- | ------------------------------------------------------------ |
S
shawn_he 已提交
67 68 69 70
| Promise&lt;void&gt; | Promise used to process the callback in the then() and catch() methods when event writing succeeded or failed.|

**Example**

L
lyj_love_code 已提交
71
```js
S
shawn_he 已提交
72 73
hiAppEvent.write("test_event", hiAppEvent.EventType.FAULT, {"int_data":100, "str_data":"strValue"})
    .then((value) => {
Z
zengyawen 已提交
74 75
        // Event writing succeeded.
        console.log(`success to write event: ${value}`);
S
shawn_he 已提交
76 77 78
    }).catch((err) => {
        // Event writing exception: Write the event to the event file after the invalid parameters in the event are ignored, or stop writing the event if the event verification fails.
        console.error(`failed to write event because ${err.code}`);
Z
zengyawen 已提交
79
    });
S
shawn_he 已提交
80
```
Z
zengyawen 已提交
81 82


S
shawn_he 已提交
83
## hiAppEvent.configure
Z
zengyawen 已提交
84

S
shawn_he 已提交
85
configure(config: ConfigOption): boolean
Z
zengyawen 已提交
86

S
shawn_he 已提交
87
Configures the application event logging function, such as setting the event logging switch and maximum size of the directory that stores the event logging files.
Z
zengyawen 已提交
88

S
shawn_he 已提交
89 90
**System capability**: SystemCapability.HiviewDFX.HiAppEvent

S
shawn_he 已提交
91
**Parameters**
Z
zengyawen 已提交
92

S
shawn_he 已提交
93 94 95
| Name| Type                         | Mandatory| Description                    |
| ------ | ----------------------------- | ---- | ------------------------ |
| config | [ConfigOption](#configoption) | Yes  | Configuration items for application event logging.|
Z
zengyawen 已提交
96

S
shawn_he 已提交
97 98
**Return value**

S
shawn_he 已提交
99 100
| Type   | Description                                                       |
| ------- | ----------------------------------------------------------- |
S
shawn_he 已提交
101 102 103
| boolean | Returns **true** if the configuration is successful; returns **false** otherwise.|

**Example**
L
lyj_love_code 已提交
104
```js
S
shawn_he 已提交
105 106 107 108 109 110 111 112 113 114
// Set the application event logging switch.
hiAppEvent.configure({
    disable: true
});

// Configure the maximum size of the directory that stores the event logging files.
hiAppEvent.configure({
    maxStorage: '100M'
});
```
Z
zengyawen 已提交
115 116


S
shawn_he 已提交
117
## ConfigOption
Z
zengyawen 已提交
118 119 120

Provides the configuration items for application event logging.

S
shawn_he 已提交
121 122 123 124 125 126
**System capability**: SystemCapability.HiviewDFX.HiAppEvent

| Name    | Type   | Mandatory| Description                                                        |
| ---------- | ------- | ---- | ------------------------------------------------------------ |
| disable    | boolean | No  | Application event logging switch. The value <b>true</b> means to disable the application event logging function, and the value <b>false</b> means the opposite.|
| maxStorage | string  | No  | Maximum size of the event file storage directory. The default value is **10M**. If the specified size is exceeded, the oldest event logging files in the storage directory will be deleted to free up space.|
S
shawn_he 已提交
127 128 129


## EventType
Z
zengyawen 已提交
130 131 132

Enumerates event types.

S
shawn_he 已提交
133 134 135 136 137 138 139 140
**System capability**: SystemCapability.HiviewDFX.HiAppEvent

| Name     | Default Value| Description          |
| --------- | ------ | -------------- |
| FAULT     | 1      | Fault event|
| STATISTIC | 2      | Statistical event|
| SECURITY  | 3      | Security event|
| BEHAVIOR  | 4      | Behavior event|
S
shawn_he 已提交
141 142 143


## Event
Z
zengyawen 已提交
144 145 146

Provides constants that define the names of all predefined events.

S
shawn_he 已提交
147 148 149 150 151 152 153
**System capability**: SystemCapability.HiviewDFX.HiAppEvent

| Name                     | Type| Readable| Writable| Description                |
| ------------------------- | -------- | ---- | ---- | -------------------- |
| USER_LOGIN                | string   | Yes  | No  | User login event.      |
| USER_LOGOUT               | string   | Yes  | No  | User logout event.      |
| DISTRIBUTED_SERVICE_START | string   | Yes  | No  | Distributed service startup event.|
Z
zengyawen 已提交
154 155


S
shawn_he 已提交
156 157 158
## Param

Provides constants that define the names of all predefined event parameters.
Z
zengyawen 已提交
159

S
shawn_he 已提交
160 161 162 163 164 165 166
**System capability**: SystemCapability.HiviewDFX.HiAppEvent

| Name                           | Type| Readable| Writable| Description              |
| ------------------------------- | -------- | ---- | ---- | ------------------ |
| USER_ID                         | string   | Yes  | No  | Custom user ID.    |
| DISTRIBUTED_SERVICE_NAME        | string   | Yes  | No  | Distributed service name.  |
| DISTRIBUTED_SERVICE_INSTANCE_ID | string   | Yes  | No  | Distributed service instance ID.|