GithubHelp home page GithubHelp logo

consola's Introduction

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:

yarn add consola

Using npm:

npm i consola

Getting started

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'))

Fancy Reporter


[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)


Scoped Loggers

Group logs using an scope:

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:

Please see 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.

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.

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

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

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

With jsdom

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

License

MIT

consola's People

Contributors

376temp avatar alberto avatar atinux avatar cehnegaitne avatar clarkdo avatar git-good avatar maazadeeb avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.