Elegant Console Logger
- Easy to use
- Fancy output with Fallback for CI environments
- A global mockable stdout/stderr wrapper
- Pluggable reporters
- Consistent CLI experience
- Scoped Loggers
Using yarn:
yarn add consola
Using npm:
npm i consola
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'))
[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
Minimal Reporter (CI)
You can group logs using an scope:
const logger = consola.withScope('test')
logger.info('Log from test scope') // [Test] Log from test scope
You can choose between one of the built-in reporters or bring your own.
By default FancyReporter
is registered for modern terminals or BasicReporter
will be used if running in limited environments such as CIs.
Available reporters:
Please see Examples for usage info.
A reporter is nothing more than a Class or Object that should expose log(logObj)
method.
See implementations to get idea how to write your own.
You can think of types like extended logging levels in Consola's world.
A list of all available default types is here.
Consola has a global instance and it is recommended to use it everywhere. In case that you need more control, you can create a new instance too.
const { Consola, BasicReporter } = require('consola')
const consola = new Consola({
level: 30,
reporters: [],
types: []
})
consola.add(BasicReporter)
consola.<type>([logObj|message|error])
Log to all reporters. If a plain string or error is given it 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 })
.
logObject is a free-to-extend object which will be passed into reporters.
Here are standard possible fields:
Common fields:
message
date
scope
Extended fields:
clear
badge
additional
consola.clear().add({
log: jest.fn()
})
{
virtualConsole: new jsdom.VirtualConsole().sendTo(consola)
}
MIT