1# Console
2
3The **console** module provides a simple debugging console, which is similar to the JavaScript console provided by the browser.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## console.debug
10
11debug(message: string, ...arguments: any[]): void
12
13Prints debugging information in formatted output mode.
14
15This API can be used in ArkTS widgets since API version 9.
16
17**System capability**: SystemCapability.ArkUI.ArkUI.Full
18
19**Parameters**
20
21| Name    | Type    | Mandatory  | Description         |
22| ------- | ------ | ---- | ----------- |
23| message | string | Yes   | Text to be printed.|
24| arguments | any[] | No   | Arguments in the message or other information to be printed.|
25
26**Example**
27
28```js
29const number = 5;
30console.debug('count: %d', number);  // Print the debugging information with arguments in the message replaced.
31// count: 5
32console.debug('count:', number);  // Print the message and other information.
33// count: 5
34console.debug('count:'); // Print the message only.
35// count:
36```
37
38## console.log
39
40log(message: string, ...arguments: any[]): void
41
42Prints log information in formatted output mode.
43
44This API can be used in ArkTS widgets since API version 9.
45
46**System capability**: SystemCapability.ArkUI.ArkUI.Full
47
48**Parameters**
49
50| Name    | Type    | Mandatory  | Description         |
51| ------- | ------ | ---- | ----------- |
52| message | string | Yes   | Text to be printed.|
53| arguments | any[] | No   |Arguments in the message or other information to be printed.|
54
55**Example**
56
57```js
58const number = 5;
59console.log('count: %d', number);  // Print the log information with arguments in the message replaced.
60// count: 5
61console.log('count:', number);  // Print the message and other information.
62// count: 5
63console.log('count:'); // Print the message only.
64// count:
65```
66
67## console.info
68
69info(message: string, ...arguments: any[]): void
70
71Prints log information in formatted output mode. This API is the alias of **console.log ()**.
72
73This API can be used in ArkTS widgets since API version 9.
74
75**System capability**: SystemCapability.ArkUI.ArkUI.Full
76
77**Parameters**
78
79| Name    | Type    | Mandatory  | Description         |
80| ------- | ------ | ---- | ----------- |
81| message | string | Yes   | Text to be printed.|
82| arguments | any[] | No   | Arguments in the message or other information to be printed.|
83
84**Example**
85
86```js
87const number = 5;
88console.info('count: %d', number);  // Print the log information with arguments in the message replaced.
89// count: 5
90console.info('count:', number);  // Print the message and other information.
91// count: 5
92console.info('count:'); // Print the message only.
93// count:
94```
95
96## console.warn
97
98warn(message: string, ...arguments: any[]): void
99
100Prints warning information in formatted output mode.
101
102This API can be used in ArkTS widgets since API version 9.
103
104**System capability**: SystemCapability.ArkUI.ArkUI.Full
105
106**Parameters**
107
108| Name    | Type    | Mandatory  | Description         |
109| ------- | ------ | ---- | ----------- |
110| message | string | Yes   | Warning information to be printed.|
111| arguments | any[] | No   | Arguments in the message or other information to be printed.|
112
113**Example**
114
115```js
116const str = "name should be string";
117console.warn('warn: %d', str); // Print the warning information with arguments in the message replaced.
118// warn: name should be string
119console.warn('warn:', str);  // Print the message and other information.
120// warn: name should be string
121console.warn('warn:'); // Print the message only.
122// warn:
123```
124
125## console.error
126
127error(message: string, ...arguments: any[]): void
128
129Prints error information in formatted output mode.
130
131This API can be used in ArkTS widgets since API version 9.
132
133**System capability**: SystemCapability.ArkUI.ArkUI.Full
134
135**Parameters**
136
137| Name    | Type    | Mandatory  | Description         |
138| ------- | ------ | ---- | ----------- |
139| message | string | Yes   | Error information to be printed.|
140| arguments | any[] | No   | Arguments in the message or other information to be printed.|
141
142
143**Example**
144
145```js
146const str = "value is not defined";
147console.error('error: %d', str);  // Print the error information with arguments in the message replaced.
148// error: value is not defined
149console.error('error:', str);  // Print the message and other information.
150// error: value is not defined
151console.error('error:'); // Print the message only.
152// error:
153```
154
155## console.assert<sup>10+</sup>
156
157assert(value?: Object, ...arguments: Object[]): void
158
159Prints assertion information.
160
161**Atomic service API**: This API can be used in atomic services since API version 12.
162
163**System capability**: SystemCapability.Utils.Lang
164
165**Parameters**
166
167| Name    | Type    | Mandatory  | Description         |
168| ------- | ------ | ---- | ----------- |
169| value | Object | No   | Result value. If **value** is **false** or left blank, the output starting with "Assertion failed" is printed. If **value** is **true**, no information is printed.|
170| arguments | Object | No   | Other information to be printed when **value** is **false**. If this parameter is left blank, other information is not printed.|
171
172**Example**
173
174```js
175console.assert(true, 'does nothing');  // Do not print error information as value is true.
176console.assert(2% 1 == 0,'does nothing');  // Do not print error information as value is true.
177
178console.assert(false, 'console %s work', 'didn\'t');
179// Assertion failed: console didn't work
180
181console.assert();
182// Assertion failed
183```
184
185## console.count<sup>10+</sup>
186
187count(label?: string): void
188
189Maintains an internal counter. When this counter is invoked, its label name and the corresponding call count are printed.
190
191**Atomic service API**: This API can be used in atomic services since API version 12.
192
193**System capability**: SystemCapability.Utils.Lang
194
195**Parameters**
196
197| Name    | Type    | Mandatory  | Description         |
198| ------- | ------ | ---- | ----------- |
199| label | string | No   | Counter label name. The default value is **default**.|
200
201
202**Example**
203
204```js
205console.count()
206// default: 1
207console.count('default')
208// default: 2
209console.count('abc')
210// abc: 1
211console.count('xyz')
212// xyz: 1
213console.count('abc')
214// abc: 2
215console.count()
216// default: 3
217```
218
219## console.countReset<sup>10+</sup>
220
221countReset(label?: string): void
222
223Resets a counter based on the specified label name.
224
225**Atomic service API**: This API can be used in atomic services since API version 12.
226
227**System capability**: SystemCapability.Utils.Lang
228
229**Parameters**
230
231| Name    | Type    | Mandatory  | Description         |
232| ------- | ------ | ---- | ----------- |
233| label | string | No   | Counter label name. The default value is **default**.|
234
235**Example**
236
237```js
238console.count('abc');
239// abc: 1
240console.countReset('abc');
241console.count('abc');
242// abc: 1
243```
244
245## console.dir<sup>10+</sup>
246
247dir(dir?: Object): void
248
249Prints content of the specified object.
250
251**Atomic service API**: This API can be used in atomic services since API version 12.
252
253**System capability**: SystemCapability.Utils.Lang
254
255**Parameters**
256
257| Name    | Type    | Mandatory  | Description         |
258| ------- | ------ | ---- | ----------- |
259| dir | Object | No   | Object whose content needs to be printed. If this parameter is left blank, no information is printed.|
260
261
262**Example**
263
264```js
265class bar {
266  baz: boolean = true;
267}
268let b: bar = {baz: true}
269class foo{
270  bar: bar = b;
271}
272let c: foo = {bar: b}
273class  c1{
274  foo: foo = c;
275}
276let a: c1 = {foo: c}
277console.dir(a);
278// Object: {"foo":{"bar":{"baz":true}}}
279
280console.dir(); // No information is printed.
281```
282
283
284## console.dirxml<sup>10+</sup>
285
286dirxml(...arguments: Object[]): void
287
288Displays an interactive tree of the descendant elements of the specified XML element. This API is implemented by calling **console.log()** internally. It does not produce any XML elements. The usage method is the same as that of **console.log()**.
289
290**Atomic service API**: This API can be used in atomic services since API version 12.
291
292**System capability**: SystemCapability.Utils.Lang
293
294**Parameters**
295
296| Name    | Type    | Mandatory  | Description         |
297| ------- | ------ | ---- | ----------- |
298| arguments | Object | Yes   | Information to be printed.|
299
300**Example**
301
302```js
303const number = 5;
304console.dirxml('count: %d', number);
305// count: 5
306console.dirxml('count:', number);
307// count: 5
308console.dirxml('count:');
309// count:
310```
311
312## console.group<sup>10+</sup>
313
314group(...arguments: Object[]): void
315
316Increases the indentation of subsequent lines by two spaces.
317If the information to be printed is provided, the information is printed without extra indentation.
318
319**Atomic service API**: This API can be used in atomic services since API version 12.
320
321**System capability**: SystemCapability.Utils.Lang
322
323**Parameters**
324
325| Name    | Type    | Mandatory  | Description         |
326| ------- | ------ | ---- | ----------- |
327| arguments | Object | No   | Information to be printed.|
328
329**Example**
330
331```js
332console.log("outter");
333// outter
334console.group();
335console.log("level 1");
336//   level 1
337console.group("in level1");
338//   in level1
339console.log("level 2");
340//     level 2
341```
342
343
344## console.groupCollapsed<sup>10+</sup>
345
346groupCollapsed(...arguments: Object[]): void
347
348Creates a new inline group in collapsed mode. The usage and function of this API are the same as those of **console.group()**.
349
350**Atomic service API**: This API can be used in atomic services since API version 12.
351
352**System capability**: SystemCapability.Utils.Lang
353
354**Parameters**
355
356| Name    | Type    | Mandatory  | Description         |
357| ------- | ------ | ---- | ----------- |
358| arguments | Object | No   | Information to be printed.|
359
360
361**Example**
362
363```js
364console.groupCollapsed("outter");
365// outter
366console.groupCollapsed();
367console.log("level 1");
368//   level 1
369console.groupCollapsed("in level1");
370//   in level1
371console.log("level 2");
372//     level 2
373```
374
375## console.groupEnd<sup>10+</sup>
376
377groupEnd(): void
378
379Reduces the indentation of subsequent lines by two spaces.
380
381**Atomic service API**: This API can be used in atomic services since API version 12.
382
383**System capability**: SystemCapability.Utils.Lang
384
385
386**Example**
387
388```js
389console.log("outter");
390// outter
391console.group();
392console.log("level 1");
393//   level 1
394console.groupEnd();
395console.log("outter");
396// outter
397```
398
399
400## console.table<sup>10+</sup>
401
402table(tableData?: Object): void
403
404Prints data in a table.
405
406**Atomic service API**: This API can be used in atomic services since API version 12.
407
408**System capability**: SystemCapability.Utils.Lang
409
410**Parameters**
411
412| Name    | Type    | Mandatory  | Description         |
413| ------- | ------ | ---- | ----------- |
414| tableData | Object | No   | Data to be printed in a table. If this parameter is left blank, no information is printed.|
415
416**Example**
417
418```js
419console.table([1, 2, 3]);
420// ┌─────────┬────────┐
421// │ (index) │ Values │
422// ├─────────┼────────┤
423// │    0    │   1    │
424// │    1    │   2    │
425// │    2    │   3    │
426// └─────────┴────────┘
427
428console.table({ a: [1, 2, 3, 4, 5], b: 5, c: { e: 5 } });
429
430// ┌─────────┬───┬───┬───┬───┬───┬───┬────────┐
431// │ (index) │ 0 │ 1 │ 2 │ 3 │ 4 │ e │ Values │
432// ├─────────┼───┼───┼───┼───┼───┼───┼────────┤
433// │    a    │ 1 │ 2 │ 3 │ 4 │ 5 │   │        │
434// │    b    │   │   │   │   │   │   │   5    │
435// │    c    │   │   │   │   │   │ 5 │        │
436// └─────────┴───┴───┴───┴───┴───┴───┴────────┘
437```
438
439## console.time<sup>10+</sup>
440
441time(label?: string): void
442
443Starts a timer to track the duration of an operation. You can use **console.timeEnd()** to close the timer and print the elapsed time (in ms).
444
445**Atomic service API**: This API can be used in atomic services since API version 12.
446
447**System capability**: SystemCapability.Utils.Lang
448
449**Parameters**
450
451| Name    | Type    | Mandatory  | Description         |
452| ------- | ------ | ---- | ----------- |
453| label | string | No   | Timer label. The default value is **default**.|
454
455**Example**
456
457```js
458console.time('abc');
459```
460
461## console.timeEnd<sup>10+</sup>
462
463timeEnd(label?: string): void
464
465Stops the timer started by calling **console.time()** and prints the elapsed time (in ms).
466
467**Atomic service API**: This API can be used in atomic services since API version 12.
468
469**System capability**: SystemCapability.Utils.Lang
470
471**Parameters**
472
473| Name    | Type    | Mandatory  | Description         |
474| ------- | ------ | ---- | ----------- |
475| label | string | No   | Timer label. The default value is **default**.|
476
477**Example**
478
479```js
480console.time('abc');
481console.timeEnd('abc');
482// abc: 225.438ms
483```
484
485## console.timeLog<sup>10+</sup>
486
487timeLog(label?: string, ...arguments: Object[]): void
488
489Prints the elapsed time and other data parameters for the timer started by **console.time()**.
490
491**Atomic service API**: This API can be used in atomic services since API version 12.
492
493**System capability**: SystemCapability.Utils.Lang
494
495**Parameters**
496
497| Name    | Type    | Mandatory  | Description         |
498| ------- | ------ | ---- | ----------- |
499| label | string | No   | Timer label. The default value is **default**.|
500| arguments | Object | No   | Logs to be printed.|
501
502**Example**
503
504```js
505console.time('timer1');
506console.timeLog('timer1', 17);
507// timer1: 365.227ms 17
508console.timeEnd('timer1');
509// timer1: 513.22ms
510```
511
512## console.trace<sup>10+</sup>
513
514static trace(...arguments: Object[]): void
515
516Creates a stack trace.
517
518**Atomic service API**: This API can be used in atomic services since API version 12.
519
520**System capability**: SystemCapability.Utils.Lang
521
522**Parameters**
523
524| Name    | Type    | Mandatory  | Description         |
525| ------- | ------ | ---- | ----------- |
526| arguments | Object | No   | Logs to be printed. If this parameter is left blank, only stack information is printed.|
527
528**Example**
529
530```js
531console.trace();
532// Trace:
533//     xxxxxxxxxx (current stack information)
534console.trace("Show the trace");
535// Trace: Show the trace
536//     xxxxxxxxxx (current stack information)
537```
538
539## console.traceHybridStack<sup>12+</sup>
540
541static traceHybridStack(): void
542
543Prints information about the current hybrid stack of the calling thread in the main thread or worker thread.
544
545**Atomic service API**: This API can be used in atomic services since API version 12.
546
547**System capability**: SystemCapability.Utils.Lang
548
549**Example**
550
551```ts
552console.traceHybridStack();
553// TraceHybridStack:
554// xxxxxxxxxxxxx (information about the current hybrid stack of the calling thread)
555```
556