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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
<p align="center">
<h1>🐨 Consola</h1>
<span>Elegant Console Logger for Node.js and Browser</span>
</p>
[![Standard JS][standard-js-src]][standard-js-href]
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![package phobia][package-phobia-src]][package-phobia-href]
[![bundle phobia][bundle-phobia-src]][bundle-phobia-href]
## Why Consola?
- Easy to use
- Fancy output with fallback for minimal environments
- Pluggable reporters
- Consistent command line interface (CLI) experience
- Tag support
- Redirect `console` and `stdout/stderr` to the consola and easily restore redirect.
- Browser support
- Pause/Resume support
- Mocking support
- Spam prevention by throttling logs
## 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.success('Built!')
consola.info('Reporter: Some info')
consola.error(new Error('Foo'))
```
Will display in the terminal:
![Screenshot 2020-01-28 at 14 15 15](https://user-images.githubusercontent.com/904724/73267133-af6b2f00-41d8-11ea-9f16-4a8243d19c43.png)
**NOTE:** Alternatively, you can import consola from source. But don't forget to whitelist it for transpilation:
```js
import consola from 'consola/src/node'
import consola from 'consola/src/browser'
```
## Methods
#### `<type>(logObject)` `<type>(args...)`
Log to all reporters.
Example: `consola.info('Message')`
A list of available types can be found [here](./src/types.js).
#### `addReporter(reporter)`
- Aliases: `add`
Register a custom reporter instance.
#### `removeReporter(reporter?)`
- Aliases: `remove`, `clear`
Remove a registered reporter.
If no arguments are passed all reporters will be removed.
#### `setReporters(reporter|reporter[])`
Replace all reporters.
#### `create(options)`
Create a new `Consola` instance and inherit all parent options for defaults.
#### `withDefaults(defaults)`
Create a new `Consola` instance with provided defaults
#### `withTag(tag)`
- Aliases: `withScope`
Create a new `Consola` instance with that tag.
#### `wrapConsole()` `restoreConsole()`
Globally redirect all `console.log`, etc calls to consola handlers.
#### `wrapStd()` `restoreStd()`
Globally redirect all stdout/stderr outputs to consola.
#### `wrapAll()` `restoreAll()`
Wrap both, std and console.
console uses std in the underlying so calling `wrapStd` redirects console too.
Benefit of this function is that things like `console.info` will be correctly redirected to the corresponding type.
#### `pauseLogs()` `resumeLogs()`
- Aliases: `pause`/`resume`
**Globally** pause and resume logs.
Consola will enqueue all logs when paused and then sends them to the reported when resumed.
#### `mockTypes`
- Aliases: `mock`
Mock all types. Useful for using with tests.
The first argument passed to `mockTypes` should be a callback function accepting `(typeName, type)` and returning the mocked value:
```js
consola.mockTypes((typeName, type) => jest.fn())
```
Please note that with the example above, everything is mocked independently for each type. If you need one mocked fn create it outside:
```js
const fn = jest.fn()
consola.mockTypes(() => fn)
```
If callback function returns a _falsy_ value, that type won't be mocked.
For example if you just need to mock `consola.fatal`:
```js
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.js).
You can set log level using `CONSOLA_LEVEL` environment variable.
## `logObject`
The `logObject` is a free-to-extend object which will be passed to reporters.
Standard fields:
- `message`
- `additional`
- `args`
- `date`
- `tag`
Extra fields:
- `badge`
## Reporters
Choose between one of the built-in reporters or bring in your own one.
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)
### Creating your own reporter
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.
## Types
Types are used to actually log messages to the reporters.
Each type is attached to a _logging level_.
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
import consola from 'consola'
const logger = consola.create({
// level: 4,
reporters: [
new consola.JSONReporter()
],
defaults: {
additionalColor: 'white'
}
})
```
## Integrations
### With jest
```js
describe('your-consola-mock-test', () => {
beforeAll(() => {
// Redirect std and console to consola too
// Calling this once is sufficient
consola.wrapAll()
})
beforeEach(() => {
// Re-mock consola before each test call to remove
// calls from before
consola.mockTypes(() => jest.fn())
})
test('your test', async () => {
// Some code here
// Let's retrieve all messages of `consola.log`
// Get the mock and map all calls to their first argument
const consolaMessages = consola.log.mock.calls.map(c => c[0])
expect(consolaMessages).toContain('your message')
})
})
```
### With jsdom
```js
{
virtualConsole: new jsdom.VirtualConsole().sendTo(consola)
}
```
## License
MIT - Made with 💖 By Nuxt.js team!
<!-- Refs -->
[standard-js-src]: https://flat.badgen.net/badge/code%20style/standard/green
[standard-js-href]: https://standardjs.com
[npm-version-src]: https://flat.badgen.net/npm/v/consola/latest
[npm-version-href]: https://npmjs.com/package/consola
[npm-downloads-src]: https://flat.badgen.net/npm/dt/consola
[npm-downloads-href]: https://npmjs.com/package/consola
[package-phobia-src]: https://flat.badgen.net/packagephobia/install/consola
[package-phobia-href]: https://packagephobia.now.sh/result?p=consola
[bundle-phobia-src]: https://flat.badgen.net/bundlephobia/minzip/consola
[bundle-phobia-href]: https://bundlephobia.com/result?p=consola
|