aboutsummaryrefslogtreecommitdiff
path: root/node_modules/consola/README.md
blob: 2d24fb1fbc3f222f165cde3bd2e92338c66c955a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Consola

Elegant Console Logger

## Why Consola?

- Easy to use
- Fancy output with Fallback for Continuous Integration (CI) environments
- Global mockable stdout/stderr wrapper
- Pluggable reporters
- Consistent Command Line Interface (CLI) experience
- Scoped Loggers

## Installation

Using yarn:

```bash
yarn add consola
```

Using npm:

```bash
npm i consola
```

## Getting started

```js
const consola = require('consola')

// See types section for all available types
consola.start('Starting build')
consola.success('Built!')
consola.info('Reporter: Some info')
consola.error(new Error('Foo'))
```

<div align="center">
<br>
<img src="./assets/screen1.png" width="600px">
<p>Fancy Reporter</p>
<br>
</div>

<pre>
[2:17:17 PM] Starting build
[2:17:17 PM] [TEST] Log from test scope
[2:17:18 PM] Built!
[2:17:18 PM] Some info
[2:17:18 PM] Error: Foo
</pre>
<div align="center">
<p>Minimal Reporter (CI)</p>
<br>
</div>

## Scoped Loggers

Group logs using a scope:

```js
const logger = consola.withScope('test')

logger.info('Log from test scope') // [Test] Log from test scope
```

## Reporters

Choose between one of the built-in reporters or bring own reporter.

By default `FancyReporter` is registered for modern terminals or `BasicReporter` will be used if running in limited environments such as CIs.

Available reporters:

- [BasicReporter](./src/reporters/basic.js)
- [FancyReporter](./src/reporters/fancy.js)
- [JSONReporter](./src/reporters/json.js)
- [WinstonReporter](./src/reporters/winston.js)

Please see [Examples](./examples) for usage info.

### Creating your own reporter

A reporter (Class or Object) exposes `log(logObj)` method.
To write a reporter, check implementations to get an idea.

## Types

Types can be treated as _extended logging levels_ in Consola's world.

A list of all available default types is [here](./src/types.js).

## Creating a new instance

Consola has a global instance and is recommended to use everywhere.
In case more control is needed, create a new instance.

```js
const { Consola, BasicReporter } = require('consola')


const consola = new Consola({
    level: 30,
    reporters: [],
    types: []
})

consola.add(BasicReporter)
```

## API

- `consola.<type>([logObj|message|error])`

Log to all reporters. If a plain string or error is given, then the message will be automatically translated to a logObject.

- `add(reporter)`

Register a custom reporter instance.

- `remove(reporter)`

Remove a registered reporter.

- `clear()`

Remove all current reporters (Useful for writing tests).

- `withDefaults(defaults)`

Create a wrapper interface with all types available and `defaults` applied to all logs.

- `withScope(scope)`

Shortcut to `withDefaults({ scope })`.

- `level`

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

## logObject

logObject is a free-to-extend object which will be passed to reporters.

Here are standard possible fields:

Common fields:

- `message`
- `date`
- `scope`

Extended fields:

- `clear`
- `badge`
- `additional`
- `additionalStyle` (By default: `grey`)
- `icon` (Default depends on log type)

## Integrations

### With jest

```js
consola.clear().add({
  log: jest.fn()
})
```

### With jsdom

```js
{
  virtualConsole: new jsdom.VirtualConsole().sendTo(consola)
}
```

## License

MIT