• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.hilog (HiLog)
2
3The 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.
4
5> **NOTE**
6>
7> 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.
8
9## Modules to Import
10
11```js
12import hilog from '@ohos.hilog';
13```
14
15## hilog.isLoggable
16
17isLoggable(domain: number, tag: string, level: LogLevel) : boolean
18
19Checks whether logs are printable based on the specified service domain, log tag, and log level.
20
21**System capability**: SystemCapability.HiviewDFX.HiLog
22
23**Parameters**
24
25| Name| Type                 | Mandatory| Description                                                        |
26| ------ | --------------------- | ---- | ------------------------------------------------------------ |
27| domain | number                | Yes  | Service domain of logs. The value ranges from **0x0** to **0xFFFF**.<br>You can define the value as required.|
28| 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.|
29| level  | [LogLevel](#loglevel) | Yes  | Log level.                                                  |
30
31**Return value**
32
33| Type   | Description                                                        |
34| ------- | ------------------------------------------------------------ |
35| boolean | Returns **true** logs are printable based on the specified service domain, log tag, and log level; returns **false** otherwise.|
36
37**Example**
38
39```js
40hilog.isLoggable(0x0001, "testTag", hilog.LogLevel.INFO);
41```
42
43## LogLevel
44
45Log level.
46
47**System capability**: SystemCapability.HiviewDFX.HiLog
48
49| Name |   Value  | Description                                                        |
50| ----- | ------ | ------------------------------------------------------------ |
51| DEBUG | 3      | Log level used to record more detailed process information than INFO logs to help developers analyze service processes and locate faults.|
52| 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.|
53| 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.|
54| 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.|
55| FATAL | 7      | Log level used to record program or functionality crashes that cannot be rectified.              |
56
57## hilog.debug
58
59debug(domain: number, tag: string, format: string, ...args: any[]) : void
60
61Prints DEBUG logs.
62
63DEBUG logs are not recorded in official versions by default. They are available in debug versions or in official versions with the debug function enabled.
64
65**System capability**: SystemCapability.HiviewDFX.HiLog
66
67**Parameters**
68
69| Name| Type  | Mandatory| Description                                                        |
70| ------ | ------ | ---- | ------------------------------------------------------------ |
71| domain | number | Yes  | Service domain of logs. The value ranges from **0x0** to **0xFFFF**.<br>You can define the value as required.|
72| 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.|
73| 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>**.|
74| 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.|
75
76**Example**
77
78This 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.
79
80```js
81hilog.debug(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
82```
83
84If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
85
86```
8708-05 12:21:47.579  2695-2703/com.example.myapplication D 00001/testTag: hello World <private>
88```
89
90## hilog.info
91
92info(domain: number, tag: string, format: string, ...args: any[]) : void
93
94Prints INFO logs.
95
96**System capability**: SystemCapability.HiviewDFX.HiLog
97
98**Parameters**
99
100| Name| Type  | Mandatory| Description                                                        |
101| ------ | ------ | ---- | ------------------------------------------------------------ |
102| domain | number | Yes  | Service domain of logs. The value ranges from **0x0** to **0xFFFF**.<br>You can define the value as required. |
103| 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.|
104| 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>**.|
105| 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.|
106
107**Example**
108
109This 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.
110
111```js
112hilog.info(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
113```
114
115If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
116
117```
11808-05 12:21:47.579  2695-2703/com.example.myapplication I 00001/testTag: hello World <private>
119```
120
121## hilog.warn
122
123warn(domain: number, tag: string, format: string, ...args: any[]) : void
124
125Prints WARN logs.
126
127**System capability**: SystemCapability.HiviewDFX.HiLog
128
129**Parameters**
130
131| Name| Type  | Mandatory| Description                                                        |
132| ------ | ------ | ---- | ------------------------------------------------------------ |
133| domain | number | Yes  | Service domain of logs. The value ranges from **0x0** to **0xFFFF**.<br>You can define the value as required. |
134| 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.|
135| 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>**.|
136| 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.|
137
138**Example**
139
140This 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.
141
142```js
143hilog.warn(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
144```
145
146If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
147
148```
14908-05 12:21:47.579  2695-2703/com.example.myapplication W 00001/testTag: hello World <private>
150```
151
152## hilog.error
153
154error(domain: number, tag: string, format: string, ...args: any[]) : void
155
156Prints ERROR logs.
157
158**System capability**: SystemCapability.HiviewDFX.HiLog
159
160**Parameters**
161
162| Name| Type  | Mandatory| Description                                                        |
163| ------ | ------ | ---- | ------------------------------------------------------------ |
164| domain | number | Yes  | Service domain of logs. The value ranges from **0x0** to **0xFFFF**.<br>You can define the value as required. |
165| 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.|
166| 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>**.|
167| 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.|
168
169**Example**
170
171This 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.
172
173```js
174hilog.error(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
175```
176
177If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
178
179```
18008-05 12:21:47.579  2695-2703/com.example.myapplication E 00001/testTag: hello World <private>
181```
182
183## hilog.fatal
184
185fatal(domain: number, tag: string, format: string, ...args: any[]) : void
186
187Prints FATAL logs.
188
189**System capability**: SystemCapability.HiviewDFX.HiLog
190
191**Parameters**
192
193| Name| Type  | Mandatory| Description                                                        |
194| ------ | ------ | ---- | ------------------------------------------------------------ |
195| domain | number | Yes  | Service domain of logs. The value ranges from **0x0** to **0xFFFF**.<br>You can define the value as required. |
196| 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.|
197| 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>**.|
198| 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.|
199
200**Example**
201
202This 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.
203
204```js
205hilog.fatal(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
206```
207
208If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
209
210```
21108-05 12:21:47.579  2695-2703/com.example.myapplication F 00001/testTag: hello World <private>
212```
213
214## Parameter Format
215
216Parameters in the log are printed in the following format:
217
218%{[private flag]}specifier
219
220|  Privacy Flag| Description|
221| ------------ | ---- |
222|      Unspecified     | The default value is **private**, indicating that parameters in plaintext are not printed.|
223|  private     | Prints private parameters.|
224|  public      | Prints parameters in plaintext.|
225
226| Specifier| Description| Example|
227| ------------ | ---- | ---- |
228|      d/i      | Prints logs of the **number** and **bigint** types.| 123 |
229|   s     | Prints logs of the **string undefined bool** and **null** types.| "123" |
230
231**Example**
232```js
233let testObj: Record<string, string | number> = {
234    'name': "Jack",
235    'age': 22
236}
237let isBol = true;
238let bigNum = BigInt(1234567890123456789);
239hilog.info(0x0001, "jsHilogTest", "print object: %{public}s", JSON.stringify(testObj));
240hilog.info(0x0001, "jsHilogTest", "private flag: %{private}s %s, print null: %{public}s", "hello", "world", null);
241hilog.info(0x0001, "jsHilogTest", "print undefined: %{public}s", undefined);
242hilog.info(0x0001, "jsHilogTest", "print number: %{public}d %{public}i", 123, 456);
243hilog.info(0x0001, "jsHilogTest", "print bigNum: %{public}d %{public}i", bigNum, bigNum);
244hilog.info(0x0001, "jsHilogTest", "print boolean: %{public}s", isBol);
245```
246
247Log printing result:
248```
24908-09 13:26:29.094  2266  2266 I A00001/jsHilogTest: print object: {"name":"Jack","age":22}
25008-09 13:26:29.094  2266  2266 I A00001/jsHilogTest: private flag: <private> <private>, print null: null
25108-09 13:26:29.094  2266  2266 I A00001/jsHilogTest: print undefined: undefined
25208-09 13:26:29.094  2266  2266 I A00001/jsHilogTest: print number: 123 456
25308-09 13:26:29.095  2266  2266 I A00001/jsHilogTest: print bigNum: 1234567890123456768 1234567890123456768
25408-09 13:26:29.095  2266  2266 I A00001/jsHilogTest: print boolean: true
255```
256