js-apis-hilog.md 11.8 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.hilog
S
shawn_he 已提交
2

S
shawn_he 已提交
3
The **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.
S
shawn_he 已提交
4 5

> **NOTE**<br>
S
shawn_he 已提交
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.
S
shawn_he 已提交
7 8 9

## Modules to Import

L
lyj_love_code 已提交
10
```js
S
shawn_he 已提交
11 12 13
import hilog from '@ohos.hilog';
```

S
shawn_he 已提交
14
## hilog.isLoggable
S
shawn_he 已提交
15

S
shawn_he 已提交
16
isLoggable(domain: number, tag: string, level: LogLevel) : boolean
S
shawn_he 已提交
17

S
shawn_he 已提交
18
Checks whether logs are printable based on the specified service domain, log tag, and log level.
S
shawn_he 已提交
19

S
shawn_he 已提交
20 21
**System capability**: SystemCapability.HiviewDFX.HiLog

S
shawn_he 已提交
22 23
**Parameters**

S
shawn_he 已提交
24 25
| Name| Type                 | Mandatory| Description                                                        |
| ------ | --------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
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.|
S
shawn_he 已提交
27 28 29 30 31 32 33 34
| 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.|
| level  | [LogLevel](#loglevel) | Yes  | Log level.                                                  |

**Return value**

| Type   | Description                                                        |
| ------- | ------------------------------------------------------------ |
| boolean | Returns **true** logs are printable based on the specified service domain, log tag, and log level; returns **false** otherwise.|
S
shawn_he 已提交
35 36 37

**Example**

L
lyj_love_code 已提交
38
```js
S
shawn_he 已提交
39
hilog.isLoggable(0x0001, "testTag", hilog.LogLevel.INFO);
S
shawn_he 已提交
40 41
```

S
shawn_he 已提交
42
## LogLevel
S
shawn_he 已提交
43

S
shawn_he 已提交
44
Enumerates the log levels.
S
shawn_he 已提交
45

S
shawn_he 已提交
46
**System capability**: SystemCapability.HiviewDFX.HiLog
S
shawn_he 已提交
47

S
shawn_he 已提交
48
| Name | Value| Description                                                        |
S
shawn_he 已提交
49 50 51 52 53 54 55 56 57 58
| ----- | ------ | ------------------------------------------------------------ |
| DEBUG | 3      | Log level used to record more detailed process information than INFO logs to help developers analyze service processes and locate faults.|
| 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.|
| 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.|
| 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.|
| FATAL | 7      | Log level used to record program or functionality crashes that cannot be rectified.              |

## hilog.debug

debug(domain: number, tag: string, format: string, ...args: any[]) : void
S
shawn_he 已提交
59

S
shawn_he 已提交
60 61 62
Prints DEBUG logs.

DEBUG logs are not recorded in official versions by default. They are available in debug versions or in official versions with the debug function enabled.
S
shawn_he 已提交
63

S
shawn_he 已提交
64 65
**System capability**: SystemCapability.HiviewDFX.HiLog

S
shawn_he 已提交
66 67
**Parameters**

S
shawn_he 已提交
68 69
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
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.|
S
shawn_he 已提交
71 72 73
| 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.|
| 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>**.|
| 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.|
S
shawn_he 已提交
74 75 76

**Example**

S
shawn_he 已提交
77 78
This 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.

L
lyj_love_code 已提交
79
```js
S
shawn_he 已提交
80
hilog.debug(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
S
shawn_he 已提交
81 82
```

S
shawn_he 已提交
83
If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
S
shawn_he 已提交
84 85

```
S
shawn_he 已提交
86
08-05 12:21:47.579  2695-2703/com.example.myapplication D 00001/testTag: hello World <private>
S
shawn_he 已提交
87 88
```

S
shawn_he 已提交
89
## hilog.info
S
shawn_he 已提交
90

S
shawn_he 已提交
91
info(domain: number, tag: string, format: string, ...args: any[]) : void
S
shawn_he 已提交
92

S
shawn_he 已提交
93
Prints INFO logs.
S
shawn_he 已提交
94

S
shawn_he 已提交
95 96
**System capability**: SystemCapability.HiviewDFX.HiLog

S
shawn_he 已提交
97 98
**Parameters**

S
shawn_he 已提交
99 100
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
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.|
S
shawn_he 已提交
102 103 104
| 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.|
| 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>**.|
| 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.|
S
shawn_he 已提交
105 106 107

**Example**

S
shawn_he 已提交
108 109
This 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.

L
lyj_love_code 已提交
110
```js
S
shawn_he 已提交
111
hilog.info(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
S
shawn_he 已提交
112 113
```

S
shawn_he 已提交
114
If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
S
shawn_he 已提交
115 116

```
S
shawn_he 已提交
117
08-05 12:21:47.579  2695-2703/com.example.myapplication I 00001/testTag: hello World <private>
S
shawn_he 已提交
118 119
```

S
shawn_he 已提交
120
## hilog.warn
S
shawn_he 已提交
121

S
shawn_he 已提交
122
warn(domain: number, tag: string, format: string, ...args: any[]) : void
S
shawn_he 已提交
123

S
shawn_he 已提交
124
Prints WARN logs.
S
shawn_he 已提交
125

S
shawn_he 已提交
126 127
**System capability**: SystemCapability.HiviewDFX.HiLog

S
shawn_he 已提交
128 129
**Parameters**

S
shawn_he 已提交
130 131
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
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.|
S
shawn_he 已提交
133 134 135
| 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.|
| 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>**.|
| 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.|
S
shawn_he 已提交
136 137 138

**Example**

S
shawn_he 已提交
139 140
This 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.

L
lyj_love_code 已提交
141
```js
S
shawn_he 已提交
142
hilog.warn(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
S
shawn_he 已提交
143 144
```

S
shawn_he 已提交
145
If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
S
shawn_he 已提交
146 147

```
S
shawn_he 已提交
148
08-05 12:21:47.579  2695-2703/com.example.myapplication W 00001/testTag: hello World <private>
S
shawn_he 已提交
149 150
```

S
shawn_he 已提交
151
## hilog.error
S
shawn_he 已提交
152

S
shawn_he 已提交
153
error(domain: number, tag: string, format: string, ...args: any[]) : void
S
shawn_he 已提交
154

S
shawn_he 已提交
155
Prints ERROR logs.
S
shawn_he 已提交
156

S
shawn_he 已提交
157 158
**System capability**: SystemCapability.HiviewDFX.HiLog

S
shawn_he 已提交
159 160
**Parameters**

S
shawn_he 已提交
161 162
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
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.|
S
shawn_he 已提交
164 165 166
| 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.|
| 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>**.|
| 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.|
S
shawn_he 已提交
167 168 169

**Example**

S
shawn_he 已提交
170 171
This 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.

L
lyj_love_code 已提交
172
```js
S
shawn_he 已提交
173
hilog.error(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
S
shawn_he 已提交
174 175
```

S
shawn_he 已提交
176
If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
S
shawn_he 已提交
177 178

```
S
shawn_he 已提交
179
08-05 12:21:47.579  2695-2703/com.example.myapplication E 00001/testTag: hello World <private>
S
shawn_he 已提交
180 181
```

S
shawn_he 已提交
182
## hilog.fatal
S
shawn_he 已提交
183

S
shawn_he 已提交
184
fatal(domain: number, tag: string, format: string, ...args: any[]) : void
S
shawn_he 已提交
185

S
shawn_he 已提交
186
Prints FATAL logs.
S
shawn_he 已提交
187

S
shawn_he 已提交
188 189
**System capability**: SystemCapability.HiviewDFX.HiLog

S
shawn_he 已提交
190 191
**Parameters**

S
shawn_he 已提交
192 193
| Name| Type  | Mandatory| Description                                                        |
| ------ | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
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.|
S
shawn_he 已提交
195 196 197
| 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.|
| 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>**.|
| 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.|
S
shawn_he 已提交
198 199 200

**Example**

S
shawn_he 已提交
201 202
This 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.

L
lyj_love_code 已提交
203
```js
S
shawn_he 已提交
204
hilog.fatal(0x0001, "testTag", "%{public}s World %{private}d", "hello", 3);
S
shawn_he 已提交
205 206
```

S
shawn_he 已提交
207
If `"hello"` is filled in `%{public}s` and `3` in `%{private}d`, the output log is as follows:
S
shawn_he 已提交
208

S
shawn_he 已提交
209 210 211
```
08-05 12:21:47.579  2695-2703/com.example.myapplication F 00001/testTag: hello World <private>
```