# bare-console

<mark style="{ backgroundColor: '#7dde9a', padding: '2px 8px', borderRadius: '4px', fontSize: '0.85em', fontWeight: 500 }">
  stable
</mark>

[`bare-console`](https://github.com/holepunchto/bare-console) — WHATWG debugging console for JavaScript.

Mirrors the Node.js [`console`](https://nodejs.org/api/console.html) module.

```sh
npm i bare-console
```

## Usage [#usage]

```js
const Console = require('bare-console')

const console = new Console()

console.log('Hello')
console.error(new Error('Something happened'))

console.time()
for (let i = 0; i < 1000000000; i++) {}
console.timeEnd()

console.trace('Show me')
```

To install `console` as a global, require the `bare-console/global` subpath:

```js
require('bare-console/global')

console.log('Hello')
```

## API [#api]

### Console [#console]

#### `new Console(log?: Log)` [#new-consolelog-log]

Construct a new `Console`. By default, output is written through [`bare-logger`](/bare/reference/bare/modules/bare-logger), or [`bare-system-logger`](/bare/reference/bare/modules/bare-system-logger) on Android.

**Parameters**

| Parameter | Type  | Default | Description                                                                                                                                                                                                                                                                     |
| --------- | ----- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `log?`    | `Log` | —       | The logging backend to write through. Defaults to a `bare-logger` instance, or `bare-system-logger` on Android. The object must implement the methods used by console operations: `{ debug(...data), info(...data), warn(...data), error(...data), clear(), format(...data) }`. |

#### `assert(condition: unknown, ...data: unknown[]): void` [#assertcondition-unknown-data-unknown-void]

If `condition` is falsy, log `data` prefixed with `'Assertion failed'`. Otherwise do nothing.

**Parameters**

| Parameter   | Type        | Default | Description                                                                    |
| ----------- | ----------- | ------- | ------------------------------------------------------------------------------ |
| `condition` | `unknown`   | —       | The value tested for truthiness; when falsy, `data` is logged.                 |
| `data`      | `unknown[]` | —       | Values logged after the `'Assertion failed'` prefix when `condition` is falsy. |

#### `clear(): void` [#clear-void]

Forward to `log.clear()`.

#### `Console: ConsoleConstructor` [#console-consoleconstructor]

The `Console` constructor, exposed for Node.js compatibility.

#### `count(label?: string): void` [#countlabel-string-void]

Increment and log the counter identified by `label` (default `'default'`).

**Parameters**

| Parameter | Type     | Default | Description                                                       |
| --------- | -------- | ------- | ----------------------------------------------------------------- |
| `label?`  | `string` | —       | The label identifying the timer or counter (default `'default'`). |

#### `countReset(label?: string): void` [#countresetlabel-string-void]

Remove the counter identified by `label` (default `'default'`).

**Parameters**

| Parameter | Type     | Default | Description                                                       |
| --------- | -------- | ------- | ----------------------------------------------------------------- |
| `label?`  | `string` | —       | The label identifying the timer or counter (default `'default'`). |

#### `debug(...data: unknown[]): void` [#debugdata-unknown-void]

Forward `data` to the corresponding method on the backend.

**Parameters**

| Parameter | Type        | Default | Description    |
| --------- | ----------- | ------- | -------------- |
| `data`    | `unknown[]` | —       | Values to log. |

#### `error(...data: unknown[]): void` [#errordata-unknown-void]

Forward `data` to the corresponding method on the backend.

**Parameters**

| Parameter | Type        | Default | Description    |
| --------- | ----------- | ------- | -------------- |
| `data`    | `unknown[]` | —       | Values to log. |

#### `info(...data: unknown[]): void` [#infodata-unknown-void]

Forward `data` to the corresponding method on the backend.

**Parameters**

| Parameter | Type        | Default | Description    |
| --------- | ----------- | ------- | -------------- |
| `data`    | `unknown[]` | —       | Values to log. |

#### `log(...data: unknown[]): void` [#logdata-unknown-void]

Alias for `info`; forward `data` to the corresponding method on the backend.

**Parameters**

| Parameter | Type        | Default | Description    |
| --------- | ----------- | ------- | -------------- |
| `data`    | `unknown[]` | —       | Values to log. |

#### `table(tabularData: unknown, properties?: readonly string[]): void` [#tabletabulardata-unknown-properties-readonly-string-void]

Render `tabularData` as a table. Arrays and plain objects use an `(index)` column; `Map` and `Set` use an `(iteration index)` column with `key` and `values` (or `values` only) headers. Pass `properties` to restrict which object keys are shown as columns; the argument is ignored for `Map` and `Set`. Values that aren't tabular (primitives, `null`, functions) fall back to `console.log(tabularData)`.

**Parameters**

| Parameter     | Type                | Default | Description                                                     |
| ------------- | ------------------- | ------- | --------------------------------------------------------------- |
| `tabularData` | `unknown`           | —       | The data to render as a table.                                  |
| `properties?` | `readonly string[]` | —       | Object keys to include as columns; ignored for `Map` and `Set`. |

#### `time(label?: string): void` [#timelabel-string-void]

Start a timer identified by `label` (default `'default'`). Warns if a timer with the same label is already running.

**Parameters**

| Parameter | Type     | Default | Description                                                       |
| --------- | -------- | ------- | ----------------------------------------------------------------- |
| `label?`  | `string` | —       | The label identifying the timer or counter (default `'default'`). |

#### `timeEnd(label?: string): void` [#timeendlabel-string-void]

Log the elapsed time and remove the timer identified by `label` (default `'default'`).

**Parameters**

| Parameter | Type     | Default | Description                                                       |
| --------- | -------- | ------- | ----------------------------------------------------------------- |
| `label?`  | `string` | —       | The label identifying the timer or counter (default `'default'`). |

#### `timeLog(label?: string, ...data: unknown[]): void` [#timeloglabel-string-data-unknown-void]

Log the elapsed time for the timer identified by `label` (default `'default'`), along with any additional `data`. Output uses milliseconds for short durations and seconds beyond one second.

**Parameters**

| Parameter | Type        | Default | Description                                            |
| --------- | ----------- | ------- | ------------------------------------------------------ |
| `label?`  | `string`    | —       | The label identifying the timer (default `'default'`). |
| `data`    | `unknown[]` | —       | Additional values logged after the elapsed time.       |

#### `trace(...data: unknown[]): void` [#tracedata-unknown-void]

Log a stack trace prefixed with the formatted `data`.

**Parameters**

| Parameter | Type        | Default | Description                                         |
| --------- | ----------- | ------- | --------------------------------------------------- |
| `data`    | `unknown[]` | —       | Values formatted and prefixed onto the stack trace. |

#### `warn(...data: unknown[]): void` [#warndata-unknown-void]

Forward `data` to the corresponding method on the backend.

**Parameters**

| Parameter | Type        | Default | Description    |
| --------- | ----------- | ------- | -------------- |
| `data`    | `unknown[]` | —       | Values to log. |

## See also [#see-also]

* Builds on `bare-format`, `bare-hrtime`, `bare-logger`, `bare-system-logger`, and `bare-type`.
* It's pure JavaScript.
* [`bare-inspector`](/bare/reference/bare/modules/bare-inspector) — deeper debugging via the V8 inspector.
* [Bare modules](/bare/reference/bare/modules) — the full `bare-*` catalog.
* [Bare runtime API](/bare/reference/bare/runtime) — the runtime these modules extend.