1# Using HiLog (ArkTS) 2 3 4During application development, you can log from application's key code. Through logs, you can find out how the application is running. For example, the logs can tell you whether the application is running properly, and whether the code execution sequence and logic branch are correct. 5 6 7To print logs, you can use HiLog or console APIs. This topic focuses on how to use HiLog APIs. For details about the console APIs, see [console](../reference/common/js-apis-logs.md). 8 9 10## Available APIs 11 12HiLog defines five log levels (DEBUG, INFO, WARN, ERROR, and FATAL) and provides APIs to output logs of different levels. For details about the APIs, see [HiLog](../reference/apis-performance-analysis-kit/js-apis-hilog.md). 13 14| API| Description| 15| -------- | -------- | 16| isLoggable(domain: number, tag: string, level: LogLevel) | Checks whether logs of the specified domain, tag, and level can be printed.| 17| debug(domain: number, tag: string, format: string, ...args: any[]) | Outputs DEBUG logs, which are used only for debugging applications and services.<br>To set the log level to **DEBUG**, run the **hdc shell hilogcat** command in the **Terminal** window of DevEco Studio or in the **cmd** window.| 18| info(domain: number, tag: string, format: string, ...args: any[]) | Outputs INFO logs, which provide prevalent, highlighting events related to key service processes..| 19| warn(domain: number, tag: string, format: string, ...args: any[]) | Outputs WARN logs, which indicate issues that have little impact on the system.| 20| error(domain: number, tag: string, format: string, ...args: any[]) | Outputs ERROR logs, which indicate program or functional errors.| 21| fatal(domain: number, tag: string, format: string, ...args: any[]) | Outputs FATAL logs, which indicate program or functionality crashes that cannot be rectified.| 22 23### Parameters 24 25> **NOTE** 26> 27> - The domain and tag specified in **isLoggable()** must be the same as those of the logging API. 28> 29> - The log level specified in **isLoggable()** must match that of the logging API. 30 31- **domain**: service domain of logs. The value range is 0x0000 to 0xFFFF. You can define the value as required. 32 33- **tag**: log identifier. It can be any string. You are advised to use this parameter to identify the class or service behavior of a method call. A tag can contain a maximum of 31 bytes. If a tag contains more than 31 bytes, it will be truncated. Chinese characters are not recommended because garbled characters or alignment problems may occur. 34 35- **level**: log level. For details about the value, see [LogLevel](../reference/apis-performance-analysis-kit/js-apis-hilog.md#loglevel). 36 37- **format**: format of the log to output. The value is a string in the "%{private flag}specifier" format. 38 | Private Flag| Description| 39 | -------- | -------- | 40 | private | The output is **\<private>**, which indicates that the log information is invisible.| 41 | public | The log information is displayed.| 42 | Not specified| The default value **\<private>** is used.| 43 44 | Specifier| Description| Example| 45 | -------- | -------- | -------- | 46 | d/i | Prints logs of the **number** and **bigint** types.| 123 | 47 | s | Prints logs of the **string**, **undefined**, **boolean**, and **null** types.| "123" | 48 49 You can set multiple parameters in the **format** string, for example, **%s World**, where **%s** is a variable of the string type and its value is defined by **args**. <!--Del--> 50 51 To display the log masked by {**private**} in debugging, run the **hilog -p off** command. 52<!--DelEnd--> 53 54- **args**: parameters of the types specified by **specifier** in **format**. This parameter can be left blank. The number and type of parameters must match **specifier**. 55 56## Constraints 57 58The maximum size of a log file is 4096 bytes. Excess content will be discarded. 59 60## Example 61 62Add a click event in a button, which prints a log when the button is clicked. 63 641. Create a project, with **Empty Ability** as the template. 65 662. On the project configuration page, set **Model** to **Stage**. 67 683. In the **Project** window, choose **entry** > **src** > **main** > **ets** > **pages**, open the **Index.ets** file, add a button to enable a log to be printed when the button is clicked. 69 The sample code is as follows: 70 71 ```ts 72 // Index.ets 73 74 import { hilog } from '@kit.PerformanceAnalysisKit'; 75 76 @Entry 77 @Component 78 struct Index { 79 build() { 80 Row() { 81 Column() { 82 // Add a button named Next. 83 Button() { 84 Text('Next') 85 .fontSize(30) 86 .fontWeight(FontWeight.Bold) 87 } 88 .type(ButtonType.Capsule) 89 .margin({ 90 top: 20 91 }) 92 .backgroundColor('#0D9FFB') 93 .width('40%') 94 .height('5%') 95 // Add a onClick event with the button to print a log when the button is clicked. 96 .onClick(() => { 97 hilog.isLoggable(0xFF00, "testTag", hilog.LogLevel.INFO); 98 hilog.info(0xFF00, "testTag", "%{public}s World %{public}d", "hello", 3); 99 }) 100 } 101 .width('100%') 102 } 103 .height('100%') 104 } 105 } 106 ``` 107 108 For example, output an INFO log in the following format: 109 110 ```txt 111 "%{public}s World %{public}d" 112 ``` 113 114 *%{public}s* indicates a string, and *%{public}d* indicates an integer. Both of them are displayed in plaintext. 115 1164. Run the project on a real device, and click the **Next** button on the app/service. 117 1185. At the bottom of DevEco Studio, switch to the **Log** tab and set the filter criteria. 119 Specifically, select the current device and process, set the log level to **Verbose**, and enter **testTag** in the search box. Then, only the logs that meet the filter criteria are displayed. 120 121 In this example, the printed log is "hello World 3". 122 123<!--RP1--> 124<!--RP1End--> 125