1e41f4b71Sopenharmony_ci# @ohos.hilog (HiLog) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe HiLog subsystem allows your applications or services to output logs based on the specified type, level, and format string. Such logs help you learn the running status of applications and better debug programs. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 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. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Modules to Import 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```js 12e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## hilog.isLoggable 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciisLoggable(domain: number, tag: string, level: LogLevel) : boolean 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciChecks whether logs are printable based on the specified service domain, log tag, and log level. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiLog 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**Parameters** 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 28e41f4b71Sopenharmony_ci| ------ | --------------------- | ---- | ------------------------------------------------------------ | 29e41f4b71Sopenharmony_ci| domain | number | Yes | Service domain of logs. The value ranges from **0x0** to **0xFFFF**.<br>You can define the value as required. | 30e41f4b71Sopenharmony_ci| tag | string | Yes | Log tag in the string format. You are advised to use this parameter to identify a particular service behavior or the class holding the ongoing method. | 31e41f4b71Sopenharmony_ci| level | [LogLevel](#loglevel) | Yes | Log level. | 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**Return value** 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci| Type | Description | 36e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------------ | 37e41f4b71Sopenharmony_ci| boolean | Returns **true** logs are printable based on the specified service domain, log tag, and log level; returns **false** otherwise. | 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci**Example** 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci```js 42e41f4b71Sopenharmony_cihilog.isLoggable(0x0001, "testTag", hilog.LogLevel.INFO); 43e41f4b71Sopenharmony_ci``` 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci## LogLevel 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ciLog level. 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiLog 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci| Name | Value | Description | 54e41f4b71Sopenharmony_ci| ----- | ------ | ------------------------------------------------------------ | 55e41f4b71Sopenharmony_ci| DEBUG | 3 | Log level used to record more detailed process information than INFO logs to help developers analyze service processes and locate faults. | 56e41f4b71Sopenharmony_ci| INFO | 4 | Log level used to record key service process nodes and exceptions that occur during service running,<br>for example, no network signal or login failure.<br>These logs should be recorded by the dominant module in the service to avoid repeated logging conducted by multiple invoked modules or low-level functions. | 57e41f4b71Sopenharmony_ci| WARN | 5 | Log level used to record severe, unexpected faults that have little impact on users and can be rectified by the programs themselves or through simple operations. | 58e41f4b71Sopenharmony_ci| ERROR | 6 | Log level used to record program or functional errors that affect the normal running or use of the functionality and can be fixed at a high cost, for example, by resetting data. | 59e41f4b71Sopenharmony_ci| FATAL | 7 | Log level used to record program or functionality crashes that cannot be rectified. | 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci## hilog.debug 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_cidebug(domain: number, tag: string, format: string, ...args: any[]) : void 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ciPrints DEBUG logs. 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ciDEBUG logs are not recorded in official versions by default. They are available in debug versions or in official versions with the debug function enabled. 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiLog 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci**Parameters** 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 76e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 77e41f4b71Sopenharmony_ci| domain | number | Yes | Service domain of logs. The value ranges from **0x0** to **0xFFFF**.<br>You can define the value as required. | 78e41f4b71Sopenharmony_ci| tag | string | Yes | Log tag in the string format. You are advised to use this parameter to identify a particular service behavior or the class holding the ongoing method. | 79e41f4b71Sopenharmony_ci| format | string | Yes | Format string used to output logs in a specified format. It can contain several elements, where the parameter type and privacy identifier are mandatory.<br>Parameters labeled **{public}** are public data and are displayed in plaintext; parameters labeled **{private}** (default value) are private data and are filtered by **\<private>**. | 80e41f4b71Sopenharmony_ci| args | any[] | Yes | Variable-length parameter list corresponding to the format string. The number and type of parameters must map to the identifier in the format string. | 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**Example** 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ciThis example is used to output a DEBUG log with the format string being `"%{public}s World %{private}d"`. The variable `%{public}s` is a plaintext string, and the variable `%{private}d` is a private integer. 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci```js 87e41f4b71Sopenharmony_cihilog.debug(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3); 88e41f4b71Sopenharmony_ci``` 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ciIf `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows: 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci``` 93e41f4b71Sopenharmony_ci08-05 12:21:47.579 2695-2703/com.example.myapplication D 00001/testTag: hello World <private> 94e41f4b71Sopenharmony_ci``` 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci## hilog.info 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ciinfo(domain: number, tag: string, format: string, ...args: any[]) : void 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ciPrints INFO logs. 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiLog 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci**Parameters** 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 109e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 110e41f4b71Sopenharmony_ci| domain | number | Yes | Service domain of logs. The value ranges from **0x0** to **0xFFFF**.<br>You can define the value as required. | 111e41f4b71Sopenharmony_ci| tag | string | Yes | Log tag in the string format. You are advised to use this parameter to identify a particular service behavior or the class holding the ongoing method. | 112e41f4b71Sopenharmony_ci| format | string | Yes | Format string used to output logs in a specified format. It can contain several elements, where the parameter type and privacy identifier are mandatory.<br>Parameters labeled **{public}** are public data and are displayed in plaintext; parameters labeled **{private}** (default value) are private data and are filtered by **\<private>**. | 113e41f4b71Sopenharmony_ci| args | any[] | Yes | Variable-length parameter list corresponding to the format string. The number and type of parameters must map to the identifier in the format string. | 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**Example** 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ciThis example is used to output an INFO log with the format string being `"%{public}s World %{private}d"`. The variable `%{public}s` is a plaintext string, and the variable `%{private}d` is a private integer. 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci```js 120e41f4b71Sopenharmony_cihilog.info(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3); 121e41f4b71Sopenharmony_ci``` 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ciIf `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows: 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci``` 126e41f4b71Sopenharmony_ci08-05 12:21:47.579 2695-2703/com.example.myapplication I 00001/testTag: hello World <private> 127e41f4b71Sopenharmony_ci``` 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci## hilog.warn 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ciwarn(domain: number, tag: string, format: string, ...args: any[]) : void 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ciPrints WARN logs. 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiLog 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci**Parameters** 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 142e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 143e41f4b71Sopenharmony_ci| domain | number | Yes | Service domain of logs. The value ranges from **0x0** to **0xFFFF**.<br>You can define the value as required. | 144e41f4b71Sopenharmony_ci| tag | string | Yes | Log tag in the string format. You are advised to use this parameter to identify a particular service behavior or the class holding the ongoing method. | 145e41f4b71Sopenharmony_ci| format | string | Yes | Format string used to output logs in a specified format. It can contain several elements, where the parameter type and privacy identifier are mandatory.<br>Parameters labeled **{public}** are public data and are displayed in plaintext; parameters labeled **{private}** (default value) are private data and are filtered by **\<private>**. | 146e41f4b71Sopenharmony_ci| args | any[] | Yes | Variable-length parameter list corresponding to the format string. The number and type of parameters must map to the identifier in the format string. | 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci**Example** 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ciThis example is used to output a WARN log with the format string being `"%{public}s World %{private}d"`. The variable `%{public}s` is a plaintext string, and the variable `%{private}d` is a private integer. 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci```js 153e41f4b71Sopenharmony_cihilog.warn(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3); 154e41f4b71Sopenharmony_ci``` 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ciIf `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows: 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci``` 159e41f4b71Sopenharmony_ci08-05 12:21:47.579 2695-2703/com.example.myapplication W 00001/testTag: hello World <private> 160e41f4b71Sopenharmony_ci``` 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci## hilog.error 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_cierror(domain: number, tag: string, format: string, ...args: any[]) : void 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ciPrints ERROR logs. 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiLog 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci**Parameters** 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 175e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 176e41f4b71Sopenharmony_ci| domain | number | Yes | Service domain of logs. The value ranges from **0x0** to **0xFFFF**.<br>You can define the value as required. | 177e41f4b71Sopenharmony_ci| tag | string | Yes | Log tag in the string format. You are advised to use this parameter to identify a particular service behavior or the class holding the ongoing method. | 178e41f4b71Sopenharmony_ci| format | string | Yes | Format string used to output logs in a specified format. It can contain several elements, where the parameter type and privacy identifier are mandatory.<br>Parameters labeled **{public}** are public data and are displayed in plaintext; parameters labeled **{private}** (default value) are private data and are filtered by **\<private>**. | 179e41f4b71Sopenharmony_ci| args | any[] | Yes | Variable-length parameter list corresponding to the format string. The number and type of parameters must map to the identifier in the format string. | 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci**Example** 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ciThis example is used to output an ERROR log with the format string being `"%{public}s World %{private}d"`. The variable `%{public}s` is a plaintext string, and the variable `%{private}d` is a private integer. 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci```js 186e41f4b71Sopenharmony_cihilog.error(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3); 187e41f4b71Sopenharmony_ci``` 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ciIf `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows: 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci``` 192e41f4b71Sopenharmony_ci08-05 12:21:47.579 2695-2703/com.example.myapplication E 00001/testTag: hello World <private> 193e41f4b71Sopenharmony_ci``` 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci## hilog.fatal 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_cifatal(domain: number, tag: string, format: string, ...args: any[]) : void 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ciPrints FATAL logs. 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiLog 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci**Parameters** 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 208e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 209e41f4b71Sopenharmony_ci| domain | number | Yes | Service domain of logs. The value ranges from **0x0** to **0xFFFF**.<br>You can define the value as required. | 210e41f4b71Sopenharmony_ci| tag | string | Yes | Log tag in the string format. You are advised to use this parameter to identify a particular service behavior or the class holding the ongoing method. | 211e41f4b71Sopenharmony_ci| format | string | Yes | Format string used to output logs in a specified format. It can contain several elements, where the parameter type and privacy identifier are mandatory.<br>Parameters labeled **{public}** are public data and are displayed in plaintext; parameters labeled **{private}** (default value) are private data and are filtered by **\<private>**. | 212e41f4b71Sopenharmony_ci| args | any[] | Yes | Variable-length parameter list corresponding to the format string. The number and type of parameters must map to the identifier in the format string. | 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci**Example** 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ciThis example is used to output a FATAL log with the format string being `"%{public}s World %{private}d"`. The variable `%{public}s` is a plaintext string, and the variable `%{private}d` is a private integer. 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci```js 219e41f4b71Sopenharmony_cihilog.fatal(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3); 220e41f4b71Sopenharmony_ci``` 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ciIf `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows: 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci``` 225e41f4b71Sopenharmony_ci08-05 12:21:47.579 2695-2703/com.example.myapplication F 00001/testTag: hello World <private> 226e41f4b71Sopenharmony_ci``` 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci## Parameter Format 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ciParameters in the log are printed in the following format: 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci%{[private flag]}specifier 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci| Privacy Flag | Description | 235e41f4b71Sopenharmony_ci| ------------ | ---- | 236e41f4b71Sopenharmony_ci| Unspecified | The default value is **private**, indicating that parameters in plaintext are not printed. | 237e41f4b71Sopenharmony_ci| private | Prints private parameters. | 238e41f4b71Sopenharmony_ci| public | Prints parameters in plaintext. | 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci| Specifier | Description | Example | 241e41f4b71Sopenharmony_ci| ------------ | ---- | ---- | 242e41f4b71Sopenharmony_ci| d/i | Prints logs of the **number** and **bigint** types. | 123 | 243e41f4b71Sopenharmony_ci| s | Prints logs of the **string undefined bool** and **null** types. | "123" | 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci**Example** 246e41f4b71Sopenharmony_ci```js 247e41f4b71Sopenharmony_cilet testObj: Record<string, string | number> = { 248e41f4b71Sopenharmony_ci 'name': "Jack", 249e41f4b71Sopenharmony_ci 'age': 22 250e41f4b71Sopenharmony_ci} 251e41f4b71Sopenharmony_cilet isBol = true; 252e41f4b71Sopenharmony_cilet bigNum = BigInt(1234567890123456789); 253e41f4b71Sopenharmony_cihilog.info(0x0001, "jsHilogTest", "print object: %{public}s", JSON.stringify(testObj)); 254e41f4b71Sopenharmony_cihilog.info(0x0001, "jsHilogTest", "private flag: %{private}s %s, print null: %{public}s", "hello", "world", null); 255e41f4b71Sopenharmony_cihilog.info(0x0001, "jsHilogTest", "print undefined: %{public}s", undefined); 256e41f4b71Sopenharmony_cihilog.info(0x0001, "jsHilogTest", "print number: %{public}d %{public}i", 123, 456); 257e41f4b71Sopenharmony_cihilog.info(0x0001, "jsHilogTest", "print bigNum: %{public}d %{public}i", bigNum, bigNum); 258e41f4b71Sopenharmony_cihilog.info(0x0001, "jsHilogTest", "print boolean: %{public}s", isBol); 259e41f4b71Sopenharmony_ci``` 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ciLog printing result: 262e41f4b71Sopenharmony_ci``` 263e41f4b71Sopenharmony_ci08-09 13:26:29.094 2266 2266 I A00001/jsHilogTest: print object: {"name":"Jack","age":22} 264e41f4b71Sopenharmony_ci08-09 13:26:29.094 2266 2266 I A00001/jsHilogTest: private flag: <private> <private>, print null: null 265e41f4b71Sopenharmony_ci08-09 13:26:29.094 2266 2266 I A00001/jsHilogTest: print undefined: undefined 266e41f4b71Sopenharmony_ci08-09 13:26:29.094 2266 2266 I A00001/jsHilogTest: print number: 123 456 267e41f4b71Sopenharmony_ci08-09 13:26:29.095 2266 2266 I A00001/jsHilogTest: print bigNum: 1234567890123456768 1234567890123456768 268e41f4b71Sopenharmony_ci08-09 13:26:29.095 2266 2266 I A00001/jsHilogTest: print boolean: true 269e41f4b71Sopenharmony_ci``` 270