• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using HiLog (ArkTS)
2
3<!--Kit: Performance Analysis Kit-->
4<!--Subsystem: HiviewDFX-->
5<!--Owner: @liuyifeifei;@buzhenwang-->
6<!--Designer: @shenchenkai-->
7<!--Tester: @liyang2235-->
8<!--Adviser: @foryourself-->
9
10During 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.
11
12
13To 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).
14
15
16## Available APIs
17
18HiLog 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).
19
20| API| Description|
21| -------- | -------- |
22| isLoggable(domain: number, tag: string, level: LogLevel) | Checks whether logs of the specified domain, tag, and level can be printed.|
23| 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 hilog -b D** command in the **Terminal** window of DevEco Studio or in the **cmd** window.|
24| info(domain: number, tag: string, format: string, ...args: any[]) | Outputs INFO logs, which provide prevalent, highlighting events related to key service processes.|
25| warn(domain: number, tag: string, format: string, ...args: any[]) | Outputs WARN logs, which indicate issues that have little impact on the system.|
26| error(domain: number, tag: string, format: string, ...args: any[]) | Outputs ERROR logs, which indicate program or functional errors.|
27| fatal(domain: number, tag: string, format: string, ...args: any[]) | Outputs FATAL logs, which indicate program or functionality crashes that cannot be rectified.|
28| setMinLogLevel(level: LogLevel) | Sets the minimum log level.<br>If the set log level is lower than the [global log level](hilog.md#displaying-and-setting-log-levels), the setting does not take effect.<br>Note: This API is supported since API version 15.|
29
30
31### Parameters
32
33- **domain**: service domain of logs. The value range is 0x0000 to 0xFFFF. You can define the value as required.
34
35- **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 exceeds this limit, it will be truncated. Chinese characters are not recommended because garbled characters or alignment problems may occur.
36
37- **level**: log level. For details about the value, see [LogLevel](../reference/apis-performance-analysis-kit/js-apis-hilog.md#loglevel).
38
39- **format**: format of the log to output. The value is a string in the "%{private flag}specifier" format.
40
41  | Private Flag| Description|
42  | -------- | -------- |
43  | private | The output is **\<private>**, which indicates that the log information is invisible.|
44  | public | The log information is displayed.|
45  | Not specified| The default value **\<private>** is used.|
46
47  | Specifier| Description| Example|
48  | -------- | -------- | -------- |
49  | d/i | Prints logs of the **number** and **bigint** types.| 123 |
50  | s | Prints logs of the **string**, **undefined**, **boolean**, and **null** types.| "123" |
51  | o/O | Prints logs of the **object**, **undefined**, and **null** types.<br>This flag is supported since API version 20.| { 'name': "Jack", 'age': 22 } |
52
53  You can set multiple parameters in the **format** string, for example, **%{public}s World**, where **%{public}s** indicates a variable of the string type and its value is defined by **args**.
54
55- **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**.
56
57> **NOTE**
58>
59> - The domain and tag specified in **isLoggable()** must be the same as those of the logging API.
60>
61> - The log level specified in **isLoggable()** must match that of the logging API.
62>
63> - **isLoggable()** returns **true** if the specified logs can be printed; returns **false** otherwise.
64>
65>   For [debug applications](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-hvigor-compilation-options-customizing-guide#section192461528194916), logs of all levels can be printed.
66>
67>   For [release applications](https://developer.huawei.com/consumer/en/doc/harmonyos-guides/ide-hvigor-compilation-options-customizing-guide#section192461528194916), logs are printed only if the log level is not lower than the global log level.
68>   During debugging, you can change the log level. For details, see [Displaying and Setting Log Levels](hilog.md#displaying-and-setting-log-levels).
69
70
71## Constraints
72
73The maximum size of a log file is 4096 bytes. Excess content will be discarded.
74
75
76## How to Develop
77
78Add a click event in a button, which prints a log when the button is clicked.
79
801. Create a project, with **Empty Ability** as the template.
81
822. On the project configuration page, set **Model** to **Stage**.
83
843. 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.
85
86   The sample code is as follows:
87
88   ```ts
89   // Index.ets
90
91   import { hilog } from '@kit.PerformanceAnalysisKit';
92
93   @Entry
94   @Component
95   struct Index {
96     build() {
97       Row() {
98         Column() {
99           // Add a button named Next.
100           Button() {
101             Text('Next')
102               .fontSize(30)
103               .fontWeight(FontWeight.Bold)
104           }
105           .type(ButtonType.Capsule)
106           .margin({
107             top: 20
108           })
109           .backgroundColor('#0D9FFB')
110           .width('40%')
111           .height('5%')
112           // Add a onClick event with the button to print a log when the button is clicked.
113           .onClick(() => {
114             hilog.isLoggable(0xFF00, "testTag", hilog.LogLevel.INFO);
115             hilog.info(0xFF00, "testTag", "%{public}s World %{public}d", "hello", 3);
116             class Person {
117                constructor(name: string, age: number) {
118                  this.name = name;
119                  this.age = age;
120                }
121                name: string;
122                age:  number;
123             }
124             let peter: Person = new Person("peter", 15);
125             hilog.info(0xFF00, "testTag", "peter is %{public}o", peter);
126             // Set the minimum log level to Warn.
127             hilog.setMinLogLevel(hilog.LogLevel.WARN);
128             hilog.info(0x0000, 'testTag', 'this is an info level log');
129             hilog.error(0x0000, 'testTag', 'this is an error level log');
130           })
131         }
132         .width('100%')
133       }
134       .height('100%')
135     }
136   }
137   ```
138
139   For example, output an INFO log in the following format:
140
141   ```txt
142   '%{public}s World %{public}d'
143   ```
144
145   *%{public}s* indicates a string, and *%{public}d* indicates an integer. Both of them are displayed in plaintext.
146
147   To output objects, use the following format string:
148
149   ```txt
150   'peter is %{public}o'
151   ```
152
153   The variable parameter *%{public}o* is a public object.
154
1554. Run the project on a real device, and click the **Next** button on the app/service.
156
1575. At the bottom of DevEco Studio, switch to the **Log** tab and set the filter criteria.
158
159   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.
160
161<!--RP3-->
162   The log result is as follows:
163
164   ```txt
165   01-02 08:18:24.947   30988-30988   A0ff00/testTag                  com.example.hilogDemo  I     hello World 3
166   01-02 08:18:24.947   30988-30988   A0ff00/testTag                  com.example.hilogDemo  I     peter is {"name":"peter","age":15}
167   01-02 08:18:24.947   30988-30988   A00000/testTag                  com.example.hilogDemo  E     this is an error level log
168   ```
169<!--RP3End-->
170
171<!--RP1-->
172<!--RP1End-->
173