Skip to content

Commit

Permalink
chore: update the docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 18, 2023
1 parent 943992d commit a952bc2
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,14 @@ import { consola, createConsola } from "consola/browser";
import { createConsola } from "consola/core";
```

## Methods
## Consola Methods

#### `<type>(logObject)` `<type>(args...)`

Log to all reporters.

Example: `consola.info('Message')`

A list of available types can be found [here](./src/types.ts).

#### `await prompt(message, { type })`

Show an input prompt. Type can either of `text`, `confirm`, `select` or `multiselect`.
Expand Down Expand Up @@ -174,52 +172,57 @@ consola.mockTypes((typeName) => typeName === "fatal" && jest.fn());
**NOTE:** Any instance of consola that inherits the mocked instance, will apply provided callback again.
This way, mocking works for `withTag` scoped loggers without need to extra efforts.

## Fields

#### `reporters`

An array of active reporters.

#### `level`

The level to display logs. Any logs at or above this level will be displayed.
List of available levels [here](./src/types.ts).
## Custom Reporters

You can set the log level using the `CONSOLA_LEVEL` environment variable, which must have the numeric log level as its value.
Consola ships with 3 built-in reporters out of the box. A fancy colored reporter by default and fallsback to a basic reporter if running in a testing or CI environment detected using [unjs/std-env](https://github.com/unjs/std-env) and a basic browser reporter.

## `logObject`
You can create a new reporter object that implements `{ log(logObject): () => { } }` interface.

The `logObject` is a free-to-extend object which will be passed to reporters.
**Example:** Simple JSON reporter

Standard fields:
```ts
import { createConsola } from "consola";

- `message`
- `additional`
- `args`
- `date`
- `tag`
const consola = createConsola({
reporters: [
{
log: (logObj) => {
console.log(JSON.stringify(logObj));
},
},
],
});

Extra fields:
// Prints {"date":"2023-04-18T12:43:38.693Z","args":["foo bar"],"type":"log","level":2,"tag":""}
consola.log("foo bar");
```

- `badge`
## Log Level

## Reporters
Consola only shows logs with configured log level or below. (Default is `3`)

Choose between one of the built-in reporters or bring in your own one.
Available log levels:

Consola uses a fancy colored reporter by default and fallsback to a basic reporter if running in a testing or CI environment detected using [unjs/std-env](https://github.com/unjs/std-env).
- `0`: Fatal and Error
- `1`: Warnings
- `2`: Normal logs
- `3`: Informational logs, success, fail, ready, start, ...
- `4`: Debug logs
- `5`: Trace logs
- `-999`: Silent
- `+999`: Verbose logs

### Creating a custom reporter
You can set the log level by either:

A reporter (class or object) exposes `log(logObj)` method.
To get more info about how to write your own reporter, take a look into the linked implementations above.
- Passing `level` option to `createConsola`
- Setting `consola.level` on instance
- Using the `CONSOLA_LEVEL` environment variable (not supported for browser and core builds).

## Types
## Log Types

Types are used to actually log messages to the reporters.
Each type is attached to a _logging level_.
Log types are exposed as `consola.[type](...)` and each is a preset of styles and log level.

A list of all available default types is [here](./src/types.ts).
A list of all available built-in types is [available here](./src/constants.ts).

## Creating a new instance

Expand Down

0 comments on commit a952bc2

Please sign in to comment.