• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.hilog (HiLog)
2
3The **hilog** module 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**<br>
6> 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.
7
8## Modules to Import
9
10```js
11import hilog from '@ohos.hilog';
12```
13
14## hilog.isLoggable
15
16isLoggable(domain: number, tag: string, level: LogLevel) : boolean
17
18Checks whether logs are printable based on the specified service domain, log tag, and log level.
19
20**System capability**: SystemCapability.HiviewDFX.HiLog
21
22**Parameters**
23
24| Name| Type                 | Mandatory| Description                                                        |
25| ------ | --------------------- | ---- | ------------------------------------------------------------ |
26| domain | number                | Yes  | Service domain of logs. The value ranges from **0x0** to **0xFFFF**. You can define the value within your application as required.|
27| 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.|
28| level  | [LogLevel](#loglevel) | Yes  | Log level.                                                  |
29
30**Return value**
31
32| Type   | Description                                                        |
33| ------- | ------------------------------------------------------------ |
34| boolean | Returns **true** logs are printable based on the specified service domain, log tag, and log level; returns **false** otherwise.|
35
36**Example**
37
38```js
39hilog.isLoggable(0x0001, "testTag", hilog.LogLevel.INFO);
40```
41
42## LogLevel
43
44Enumerates the log levels.
45
46**System capability**: SystemCapability.HiviewDFX.HiLog
47
48| Name | Value| Description                                                        |
49| ----- | ------ | ------------------------------------------------------------ |
50| DEBUG | 3      | Log level used to record more detailed process information than INFO logs to help developers analyze service processes and locate faults.|
51| 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.|
52| 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.|
53| 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.|
54| FATAL | 7      | Log level used to record program or functionality crashes that cannot be rectified.              |
55
56## hilog.debug
57
58debug(domain: number, tag: string, format: string, ...args: any[]) : void
59
60Prints DEBUG logs.
61
62DEBUG logs are not recorded in official versions by default. They are available in debug versions or in official versions with the debug function enabled.
63
64**System capability**: SystemCapability.HiviewDFX.HiLog
65
66**Parameters**
67
68| Name| Type  | Mandatory| Description                                                        |
69| ------ | ------ | ---- | ------------------------------------------------------------ |
70| domain | number | Yes  | Service domain of logs. The value ranges from **0x0** to **0xFFFF**. You can define the value within your application as required.|
71| 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.|
72| format | string | Yes  | Format string used to output logs in a specified format. It can contain several parameters, 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>**.|
73| 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.|
74
75**Example**
76
77This 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.
78
79```js
80hilog.debug(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
81```
82
83If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
84
85```
8608-05 12:21:47.579  2695-2703/com.example.myapplication D 00001/testTag: hello World <private>
87```
88
89## hilog.info
90
91info(domain: number, tag: string, format: string, ...args: any[]) : void
92
93Prints INFO logs.
94
95**System capability**: SystemCapability.HiviewDFX.HiLog
96
97**Parameters**
98
99| Name| Type  | Mandatory| Description                                                        |
100| ------ | ------ | ---- | ------------------------------------------------------------ |
101| domain | number | Yes  | Service domain of logs. The value ranges from **0x0** to **0xFFFF**. You can define the value within your application as required.|
102| 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.|
103| format | string | Yes  | Format string used to output logs in a specified format. It can contain several parameters, 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>**.|
104| 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.|
105
106**Example**
107
108This 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.
109
110```js
111hilog.info(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
112```
113
114If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
115
116```
11708-05 12:21:47.579  2695-2703/com.example.myapplication I 00001/testTag: hello World <private>
118```
119
120## hilog.warn
121
122warn(domain: number, tag: string, format: string, ...args: any[]) : void
123
124Prints WARN logs.
125
126**System capability**: SystemCapability.HiviewDFX.HiLog
127
128**Parameters**
129
130| Name| Type  | Mandatory| Description                                                        |
131| ------ | ------ | ---- | ------------------------------------------------------------ |
132| domain | number | Yes  | Service domain of logs. The value ranges from **0x0** to **0xFFFF**. You can define the value within your application as required.|
133| 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.|
134| format | string | Yes  | Format string used to output logs in a specified format. It can contain several parameters, 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>**.|
135| 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.|
136
137**Example**
138
139This 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.
140
141```js
142hilog.warn(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
143```
144
145If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
146
147```
14808-05 12:21:47.579  2695-2703/com.example.myapplication W 00001/testTag: hello World <private>
149```
150
151## hilog.error
152
153error(domain: number, tag: string, format: string, ...args: any[]) : void
154
155Prints ERROR logs.
156
157**System capability**: SystemCapability.HiviewDFX.HiLog
158
159**Parameters**
160
161| Name| Type  | Mandatory| Description                                                        |
162| ------ | ------ | ---- | ------------------------------------------------------------ |
163| domain | number | Yes  | Service domain of logs. The value ranges from **0x0** to **0xFFFF**. You can define the value within your application as required.|
164| 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.|
165| format | string | Yes  | Format string used to output logs in a specified format. It can contain several parameters, 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>**.|
166| 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.|
167
168**Example**
169
170This 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.
171
172```js
173hilog.error(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
174```
175
176If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
177
178```
17908-05 12:21:47.579  2695-2703/com.example.myapplication E 00001/testTag: hello World <private>
180```
181
182## hilog.fatal
183
184fatal(domain: number, tag: string, format: string, ...args: any[]) : void
185
186Prints FATAL logs.
187
188**System capability**: SystemCapability.HiviewDFX.HiLog
189
190**Parameters**
191
192| Name| Type  | Mandatory| Description                                                        |
193| ------ | ------ | ---- | ------------------------------------------------------------ |
194| domain | number | Yes  | Service domain of logs. The value ranges from **0x0** to **0xFFFF**. You can define the value within your application as required.|
195| 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.|
196| format | string | Yes  | Format string used to output logs in a specified format. It can contain several parameters, 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>**.|
197| 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.|
198
199**Example**
200
201This 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.
202
203```js
204hilog.fatal(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
205```
206
207If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
208
209```
21008-05 12:21:47.579  2695-2703/com.example.myapplication F 00001/testTag: hello World <private>
211```
212