What is consola?
The consola npm package is a console logger for Node.js and browsers. It provides an easy-to-use API for logging information, warnings, errors, and more, with a focus on developer experience and universal compatibility.
What are consola's main functionalities?
Basic Logging
Consola allows you to log messages at various levels, such as info, warn, and error, which are color-coded and formatted for better readability.
const consola = require('consola');
consola.info('Information message');
consola.warn('Warning message');
consola.error('Error message');
Reporters
You can add custom reporters to modify how logs are displayed or processed, giving you control over the logging output.
const consola = require('consola');
consola.addReporter({
log(logObj) {
console.log(logObj.message);
}
});
Tagged Logging
Consola supports tagged logging, which allows you to prepend a tag to your log messages, making it easier to filter and identify logs related to specific parts of your application.
const consola = require('consola').withTag('my-tag');
consola.info('Tagged information message');
Log Level Control
You can control the log level threshold, which determines the minimum level of logs that will be displayed, allowing you to filter out less important logs in different environments.
const consola = require('consola');
consola.level = 3; // Only display logs with a level of 3 (errors) or higher
Other packages similar to consola
winston
Winston is a multi-transport async logging library for Node.js. It is similar to consola in that it provides logging capabilities, but it also allows for more complex transport configurations, such as logging to files, databases, or remote services.
pino
Pino is a very low overhead Node.js logger, which focuses on performance. It provides similar logging capabilities to consola but is designed to be as efficient as possible, making it suitable for high-performance applications.
debug
Debug is a tiny Node.js debugging utility that is similar to consola's tagged logging feature. It allows you to create debug instances with different namespaces, which you can enable or disable using environment variables.
log4js
Log4js is a logging framework for Node.js, which provides similar functionality to consola. It supports multiple appenders, log levels, and layouts, and it can be configured via JSON configuration files.
🐨 Consola
Elegant Console Logger for Node.js and Browser
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
Installation
Using yarn:
yarn add consola
Using npm:
npm i consola
Getting Started
const consola = require('consola')
consola.success('Built!')
consola.info('Reporter: Some info')
consola.error(new Error('Foo'))
Methods
<type>(logObject)
<type>(args...)
Log to all reporters.
addReporter(reporter)
Register a custom reporter instance.
removeReporter(reporter?)
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)
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()
Wraps 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.
pause()
resume()
Globally pause and resume logs.
Consola will enqueue all logs when paused and then sends them to the reported when resumed.
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.
You can set log level using CONSOLA_LEVEL
environment variable.
logObject
logObject is a free-to-extend object which will be passed to reporters.
Standard fields:
message
additional
args
date
tag
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:
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 are logging levels. A list of all available default types is here.
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.
import consola from 'consola'
const logger = consola.create({
reporters: [
new consola.JSONReporter()
],
defaults: {
additionalColor: 'white'
}
})
Integrations
With jest
consola.setReporters({
log: jest.fn()
})
With jsdom
{
virtualConsole: new jsdom.VirtualConsole().sendTo(consola)
}
License
MIT - Made with 💖 By Nuxt.js team!