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