• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.hiAppEvent (Application Event Logging)
2
3The **hiAppEvent** module provides the application event logging functions, such as writing application events to the event file and managing the event logging configuration.
4
5> **NOTE**
6> - The APIs provided by this module are deprecated since API version 9. You are advised to use [`@ohos.hiviewdfx.hiAppEvent`](js-apis-hiviewdfx-hiappevent.md) instead.
7> - 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.
8
9
10## Modules to Import
11
12```js
13import hiAppEvent from '@ohos.hiAppEvent';
14```
15
16## How to Use
17
18Before using application event logging, you need to understand the requirements for related parameters.
19
20**Event Name**
21
22An event name is a string that contains a maximum of 48 characters, including digits (0 to 9), letters (a to z), and underscores (\_). It must start with a lowercase letter and cannot end with an underscore (\_).
23
24**Event Type**
25
26An event type is an enumerated value of [EventType](#eventtype).
27
28**Event Parameter**
29
30An event parameter is an object in key-value pair format, where the key is the parameter name and the value is the parameter value. The requirements are as follows:
31
32- A parameter name is a string that contains a maximum of 16 characters, including digits (0 to 9), letters (a to z), and underscores (\_). It must start with a lowercase letter and cannot end with an underscore (\_).
33- The parameter value can be of the string, number, boolean, or array type.
34- If the parameter value is a string, its maximum length is 8*1024 characters. If this limit is exceeded, excess characters will be discarded.
35- If the parameter value is a number, the value must be within the range of **Number.MIN_SAFE_INTEGER** to **Number.MAX_SAFE_INTEGER**. Otherwise, uncertain values may be generated.
36- If the parameter value is an array, the elements in the array must be of the same type, which can only be string, number, or boolean. In addition, the number of elements must be less than 100. If this limit is exceeded, excess elements will be discarded.
37- The maximum number of parameters is 32. If this limit is exceeded, excess parameters will be discarded.
38
39**Event Callback**
40
41Event callback can be a callback or promise that carries the return value obtained by invoking the event logging API. You can add the processing of the return value in the event callback as follows:
42
43- If the return value is **0**, the event verification is successful, and the event will be directly written to the event file.
44- If the return value is greater than **0**, invalid parameters are present in the event, and the event will be written to the event file after the invalid parameters are ignored.
45- If the return value is smaller than **0**, the event parameter verification fails, and the event will not be written to the event file.
46
47## hiAppEvent.write
48
49write(eventName: string, eventType: EventType, keyValues: object, callback: AsyncCallback<void>): void
50
51Writes event information to the event file of the current day. This API uses an asynchronous callback to return the result.
52
53**System capability**: SystemCapability.HiviewDFX.HiAppEvent
54
55**Parameters**
56
57| Name   | Type                     | Mandatory| Description          |
58| --------- | ------------------------- | ---- | -------------- |
59| eventName | string                    | Yes  | Event name.    |
60| eventType | [EventType](#eventtype)   | Yes  | Event type.    |
61| keyValues | object                    | Yes  | Event parameters.    |
62| callback  | AsyncCallback<void> | Yes  | Event callback.|
63
64**Example**
65
66```js
67hiAppEvent.write("test_event", hiAppEvent.EventType.FAULT, {"int_data":100, "str_data":"strValue"}, (err, value) => {
68    if (err) {
69        // Event writing error: 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.
70        console.error(`failed to write event because ${err.code}`);
71        return;
72    }
73
74    // Event writing success
75    console.log(`success to write event: ${value}`);
76});
77```
78
79
80## hiAppEvent.write
81
82write(eventName: string, eventType: EventType, keyValues: object): Promise<void>
83
84Writes event information to the event file of the current day. This API uses a promise to return the result.
85
86**System capability**: SystemCapability.HiviewDFX.HiAppEvent
87
88**Parameters**
89
90| Name   | Type                   | Mandatory| Description      |
91| --------- | ----------------------- | ---- | ---------- |
92| eventName | string                  | Yes  | Event name.|
93| eventType | [EventType](#eventtype) | Yes  | Event type.|
94| keyValues | object                  | Yes  | Event parameters.|
95
96**Return value**
97
98| Type               | Description                                                        |
99| ------------------- | ------------------------------------------------------------ |
100| Promise<void> | Promise used to asynchronously process the callback in the **then()** and **catch()** methods when event writing succeeded or failed.|
101
102**Example**
103
104```js
105hiAppEvent.write("test_event", hiAppEvent.EventType.FAULT, {"int_data":100, "str_data":"strValue"})
106    .then((value) => {
107        // Event writing success
108        console.log(`success to write event: ${value}`);
109    }).catch((err) => {
110        // Event writing error: 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.
111        console.error(`failed to write event because ${err.code}`);
112    });
113```
114
115## hiAppEvent.configure
116
117configure(config: ConfigOption): boolean
118
119Configures the application event logging function, such as setting the event logging switch and maximum size of the directory that stores the event logging files.
120
121**System capability**: SystemCapability.HiviewDFX.HiAppEvent
122
123**Parameters**
124
125| Name| Type                         | Mandatory| Description                    |
126| ------ | ----------------------------- | ---- | ------------------------ |
127| config | [ConfigOption](#configoption) | Yes  | Configuration items for application event logging.|
128
129**Return value**
130
131| Type   | Description                                                       |
132| ------- | ----------------------------------------------------------- |
133| boolean | Returns **true** if the configuration is successful; returns **false** otherwise.|
134
135**Example**
136
137```js
138// Set the application event logging switch.
139hiAppEvent.configure({
140    disable: true
141});
142
143// Configure the maximum size of the directory that stores the event logging files.
144hiAppEvent.configure({
145    maxStorage: '100M'
146});
147```
148
149## ConfigOption
150
151Provides the configuration items for application event logging.
152
153**System capability**: SystemCapability.HiviewDFX.HiAppEvent
154
155| Name      | Type   | Mandatory| Description                                                        |
156| ---------- | ------- | ---- | ------------------------------------------------------------ |
157| disable    | boolean | No  | Application event logging switch. The value **true** means to disable the application event logging function, and the value **false** means the opposite.|
158| maxStorage | string  | No  | Maximum size of the event file storage directory. The default value is <strong>10M</strong>. If the specified size is exceeded, the oldest event logging files in the directory will be deleted to free up space.|
159
160
161## EventType
162
163Enumerates event types.
164
165**System capability**: SystemCapability.HiviewDFX.HiAppEvent
166
167| Name     | Value  | Description          |
168| --------- | ---- | -------------- |
169| FAULT     | 1    | Fault event.|
170| STATISTIC | 2    | Statistical event.|
171| SECURITY  | 3    | Security event.|
172| BEHAVIOR  | 4    | Behavior event.|
173
174
175## Event
176
177Provides constants that define the names of all predefined events.
178
179**System capability**: SystemCapability.HiviewDFX.HiAppEvent
180
181| Name                     | Type  | Readable| Writable| Description                |
182| ------------------------- | ------ | ---- | ---- | -------------------- |
183| USER_LOGIN                | string | Yes  | No  | User login event.      |
184| USER_LOGOUT               | string | Yes  | No  | User logout event.      |
185| DISTRIBUTED_SERVICE_START | string | Yes  | No  | Distributed service startup event.|
186
187
188## Param
189
190Provides constants that define the names of all predefined event parameters.
191
192**System capability**: SystemCapability.HiviewDFX.HiAppEvent
193
194| Name                           | Type  | Readable| Writable| Description              |
195| ------------------------------- | ------ | ---- | ---- | ------------------ |
196| USER_ID                         | string | Yes  | No  | Custom user ID.    |
197| DISTRIBUTED_SERVICE_NAME        | string | Yes  | No  | Distributed service name.  |
198| DISTRIBUTED_SERVICE_INSTANCE_ID | string | Yes  | No  | Distributed service instance ID.|
199