1# Development of Application Event Logging 2 3## When to Use 4 5The event logging function helps applications log various information generated during running. 6 7## Available APIs 8 9JS application event logging APIs are provided by the **hiAppEvent** module. 10 11**APIs for Event Logging** 12 13| API | Return Value | Description | 14| ------------------------------------------------------------ | -------------- | ------------------------------------------------------------ | 15| write(string eventName, EventType type, object keyValues, AsyncCallback\<void> callback): void | void | Logs application events in asynchronous mode. This method uses an asynchronous callback to return the result. | 16| write(string eventName, EventType type, object keyValues): Promise\<void> | Promise\<void> | Logs application events in asynchronous mode. This method uses a promise to return the result. | 17 18When an asynchronous callback is used, the return value can be processed directly in the callback. When a promise is used, the return value can also be processed in the promise in a similar way. For details about the result codes, see [Event Verification Result Codes](#event-verification-result-codes). 19 20**APIs for Event Logging Configuration** 21 22| API | Return Value | Description | 23| ------------------------------ | ------------ | ------------------------------------------------------------ | 24| configure(ConfigOption config) | boolean | Sets the configuration options for application event logging.<br>The value **true** indicates that the operation is successful, and value **false** indicates the opposite. | 25 26## Event Verification Result Codes 27 28| Result Code | Cause | Check Rule | Processing Method | 29| ----------- | ---------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | 30| 0 | None | Event verification is successful. | Event logging is normal. No action is required. | 31| -1 | Invalid event name | The event name is not empty and contains a maximum of 48 characters.<br/>The event name consists of only the following characters: digits (0 to 9), letters (a to z), and underscore (\_).<br/>The event name does not start with a digit or underscore (_). | Ignore this event and do not perform logging. | 32| -2 | Invalid event parameter type | The event name must be a string.<br/>The event type must be a number.<br/>The key value must be an object. | Ignore this event and do not perform logging. | 33| -99 | Application event logging disabled | The application event logging function is disabled. | Ignore this event and do not perform logging. | 34| -100 | Unknown error | None | Ignore this event and do not perform logging. | 35| 1 | Invalid key name | The key name is not empty and contains a maximum of 16 characters.<br/>The key name consists of only the following characters: digits (0 to 9), letters (a to z), and underscore (\_).<br/>The key name does not start with a digit or underscore (\_).<br/>The key name does not end with an underscore (_). | Ignore the key-value pair and continue to perform logging. | 36| 2 | Invalid key type | The key must be a string. | Ignore the key-value pair and continue to perform logging. | 37| 3 | Invalid value type | The supported value types vary depending on the programming language:<br/>boolean, number, string, or Array [basic element] | Ignore the key-value pair and continue to perform logging. | 38| 4 | Value too long | The value can contain a maximum of 8*1024 characters. | Ignore the key-value pair and continue to perform logging. | 39| 5 | Excess key-value pairs | The number of key-value pairs must be less than or equal to 32. | Ignore the excess key-value pairs and continue to perform logging. | 40| 6 | Excess elements in a list value | The number of elements in a list value must be less than or equal to 100. | Truncate the list with only the first 100 elements retained, and continue to perform logging. | 41| 7 | Invalid list value | A list value can only be a basic element.<br/>The elements in a list value must be of the same type. | Ignore the key-value pair and continue to perform logging. | 42 43 44## How to Develop 45 46In this example, an application event is logged after the application startup execution page is loaded. 47 481. Create a JS application project. In the displayed Project window, choose **entry > src > main** > **js** > **default** > **pages > index**, and double-click index.js. Add the code to log the initial application event after page loading. The sample code is as follows: 49 50 ```js 51 import hiAppEvent from '@ohos.hiAppEvent' 52 53 export default { 54 data: { 55 title: "" 56 }, 57 onInit() { 58 this.title = this.$t('strings.world'); 59 60 // 1. Callback mode 61 hiAppEvent.write("start_event", hiAppEvent.EventType.BEHAVIOR, {"int_data":100, "str_data":"strValue"}, (err, value) => { 62 if (err) { 63 console.error(`failed to write event because ${err.code}`); 64 return; 65 } 66 console.log(`success to write event: ${value}`); 67 }); 68 69 // 2. Promise mode 70 hiAppEvent.write("start_event", hiAppEvent.EventType.BEHAVIOR, {"int_data":100, "str_data":"strValue"}) 71 .then((value) => { 72 console.log(`success to write event: ${value}`); 73 }).catch((err) => { 74 console.error(`failed to write event because ${err.code}`); 75 }); 76 77 // 3. Set the application event logging switch. 78 hiAppEvent.configure({ 79 disable: true 80 }); 81 82 // 4. Set the maximum size of the event file storage directory. The default value is 10M. 83 hiAppEvent.configure({ 84 maxStorage: '100M' 85 }); 86 } 87 } 88 ``` 89 902. Tap the run button on the application UI to run the project. 91