From a952bc298b08318f1203f28e6d59ccb1556b253f Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 18 Apr 2023 14:59:44 +0200 Subject: [PATCH] chore: update the docs --- README.md | 71 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 45970f3b..7f056951 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ import { consola, createConsola } from "consola/browser"; import { createConsola } from "consola/core"; ``` -## Methods +## Consola Methods #### `(logObject)` `(args...)` @@ -81,8 +81,6 @@ 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`. @@ -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