UNPKG

6.41 kBMarkdownView Raw
1<p align="center">
2 <h1>🐨 Consola</h1>
3 <span>Elegant Console Logger for Node.js and Browser</span>
4</p>
5
6[![Standard JS][standard-js-src]][standard-js-href]
7[![npm version][npm-version-src]][npm-version-href]
8[![npm downloads][npm-downloads-src]][npm-downloads-href]
9[![package phobia][package-phobia-src]][package-phobia-href]
10[![bundle phobia][bundle-phobia-src]][bundle-phobia-href]
11
12## Why Consola?
13
14- Easy to use
15- Fancy output with fallback for minimal environments
16- Pluggable reporters
17- Consistent command line interface (CLI) experience
18- Tag support
19- Redirect `console` and `stdout/stderr` to the consola and easily restore redirect.
20- Browser support
21- Pause/Resume support
22- Mocking support
23
24## Installation
25
26Using yarn:
27
28```bash
29yarn add consola
30```
31
32Using npm:
33
34```bash
35npm i consola
36```
37
38## Getting Started
39
40```js
41const consola = require('consola')
42
43// See types section for all available types
44
45consola.success('Built!')
46consola.info('Reporter: Some info')
47consola.error(new Error('Foo'))
48```
49
50## Methods
51
52#### `<type>(logObject)` `<type>(args...)`
53
54Log to all reporters.
55
56Example: `consola.info('Message')`
57
58A list of available types can be found [here](./src/types.js).
59
60#### `addReporter(reporter)`
61
62- Aliases: `add`
63
64Register a custom reporter instance.
65
66#### `removeReporter(reporter?)`
67
68- Aliases: `remove`, `clear`
69
70Remove a registered reporter.
71
72If no arguments are passed all reporters will be removed.
73
74#### `setReporters(reporter|reporter[])`
75
76Replace all reporters.
77
78#### `create(options)`
79
80Create a new `Consola` instance and inherit all parent options for defaults.
81
82#### `withDefaults(defaults)`
83
84Create a new `Consola` instance with provided defaults
85
86#### `withTag(tag)`
87
88- Aliases: `withScope`
89
90Create a new `Consola` instance with that tag.
91
92#### `wrapConsole()` `restoreConsole()`
93
94Globally redirect all `console.log`, etc calls to consola handlers.
95
96#### `wrapStd()` `restoreStd()`
97
98Globally redirect all stdout/stderr outputs to consola.
99
100#### `wrapAll()` `restoreAll()`
101
102Wrap both, std and console.
103
104console uses std in the underlying so calling `wrapStd` redirects console too.
105Benefit of this function is that things like `console.info` will be correctly redirected to the corresponding type.
106
107#### `pauseLogs()` `resumeLogs()`
108
109- Aliases: `pause`/`resume`
110
111**Globally** pause and resume logs.
112
113Consola will enqueue all logs when paused and then sends them to the reported when resumed.
114
115#### `mockTypes`
116
117- Aliases: `mock`
118
119Mock all types. Useful for using with tests.
120
121The first argument passed to `mockTypes` should be a callback function accepting `(typeName, type)` and returning the mocked value:
122
123```js
124consola.mockTypes((typeName, type) => jest.fn())
125```
126
127Please note that with the example above, everything is mocked independently for each type. If you need one mocked fn create it outside:
128
129```js
130const fn = jest.fn()
131consola.mockTypes(() => fn)
132```
133
134If callback function returns a _falsy_ value, that type won't be mocked.
135
136For example if you just need to mock `consola.fatal`:
137
138```js
139consola.mockTypes((typeName) => typeName === 'fatal' && jest.fn())
140```
141
142**NOTE:** Any instance of consola that inherits the mocked instance, will apply provided callback again.
143This way, mocking works for `withTag` scoped loggers without need to extra efforts.
144
145## Fields
146
147#### `reporters`
148
149An array of active reporters.
150
151#### `level`
152
153The level to display logs. Any logs at or above this level will be displayed.
154List of available levels [here](./src/types.js).
155
156You can set log level using `CONSOLA_LEVEL` environment variable.
157
158## `logObject`
159
160The `logObject` is a free-to-extend object which will be passed to reporters.
161
162Standard fields:
163
164- `message`
165- `additional`
166- `args`
167- `date`
168- `tag`
169
170Extra fields:
171
172- `badge`
173
174## Reporters
175
176Choose between one of the built-in reporters or bring in your own one.
177
178By default `FancyReporter` is registered for modern terminals or `BasicReporter` will be used if running in limited environments such as CIs.
179
180Available reporters:
181
182- [BasicReporter](./src/reporters/basic.js)
183- [FancyReporter](./src/reporters/fancy.js)
184- [JSONReporter](./src/reporters/json.js)
185- [WinstonReporter](./src/reporters/winston.js)
186
187### Creating your own reporter
188
189A reporter (class or object) exposes `log(logObj)` method.
190To get more info about how to write your own reporter, take a look into the linked implementations above.
191
192## Types
193
194Types are used to actually log messages to the reporters.
195Each type is attached to a _logging level_.
196
197A list of all available default types is [here](./src/types.js).
198
199## Creating a new instance
200
201Consola has a global instance and is recommended to use everywhere.
202In case more control is needed, create a new instance.
203
204```js
205import consola from 'consola'
206
207const logger = consola.create({
208 // level: 4,
209 reporters: [
210 new consola.JSONReporter()
211 ],
212 defaults: {
213 additionalColor: 'white'
214 }
215})
216```
217
218## Integrations
219
220### With jest
221
222```js
223describe('your-consola-mock-test', () => {
224 beforeAll(() => {
225 // Redirect std and console to consola too
226 // Calling this once is sufficient
227 consola.wrapAll()
228 })
229
230 beforeEach(() => {
231 // Re-mock consola before each test call to remove
232 // calls from before
233 consola.mockTypes(() => jest.fn())
234 })
235
236
237 test('your test', async () => {
238 // Some code here
239
240 // Let's retrieve all messages of `consola.log`
241 // Get the mock and map all calls to their first argument
242 const consolaMessages = consola.log.mock.calls.map(c => c[0])
243 expect(consolaMessages).toContain('your message')
244 })
245
246})
247```
248
249### With jsdom
250
251```js
252{
253 virtualConsole: new jsdom.VirtualConsole().sendTo(consola)
254}
255```
256
257## License
258
259MIT - Made with 💖 By Nuxt.js team!
260
261<!-- Refs -->
262[standard-js-src]: https://flat.badgen.net/badge/code%20style/standard/green
263[standard-js-href]: https://standardjs.com
264
265[npm-version-src]: https://flat.badgen.net/npm/v/consola/latest
266[npm-version-href]: https://npmjs.com/package/consola
267
268[npm-downloads-src]: https://flat.badgen.net/npm/dt/consola
269[npm-downloads-href]: https://npmjs.com/package/consola
270
271[package-phobia-src]: https://flat.badgen.net/packagephobia/install/consola
272[package-phobia-href]: https://packagephobia.now.sh/result?p=consola
273
274[bundle-phobia-src]: https://flat.badgen.net/bundlephobia/minzip/consola
275[bundle-phobia-href]: https://bundlephobia.com/result?p=consola