项目作者: amekusa

项目描述 :
JavaScript console wrapper + extra features
高级语言: JavaScript
项目地址: git://github.com/amekusa/conso1e.git
创建时间: 2020-07-21T03:20:38Z
项目社区:https://github.com/amekusa/conso1e

开源协议:ISC License

下载


conso1e ( conso[one]e ) is a fully functional console wrapper with additional features. Since it wraps every console method, you can simply replace console with it.

Build Status codecov npm

Features

  • Supports method-chaining
  • It’s stateful
  • Logs can be suppressed & bufferable
  • Provides a global & singleton instance which is accessible across modules
  • Labels
  • Subcontexts

Getting Started

Install it with NPM:

  1. npm i conso1e

require() it and create() an instance:

  1. const console = require('conso1e').create();
  2. console.log('Hello');

If you want to replace console with conso1e entirely, overwrite the global console variable:

  1. console = require('conso1e').create(); // Overwriting the builtin console object
  2. console.log('Hello');
  3. // You can still access the original console
  4. // via .core property
  5. console.core.log('Hello');

* Beware that overwriting console affects the entire application

Instead of creating a local instance, you can also use global() to access the global instance:

  1. const console = require('conso1e').global();

global() always returns the same instance. In other words, it returns singleton.
The global instance persists across different modules.

ES module loading is also supported:

  1. import Conso1e from 'conso1e';
  2. const console = Conso1e.create(); // Local instance
  3. const console = Conso1e.global(); // Global instance

Methods & Properties

.suppress ( buffer = false )

Starts suppression. During suppression, any method calls don’t output to the console.
Suppression can be bypassed by prefixing methods with underscore (ex. console.log()console._log() ).

  • @param \ buffer
    • If true, suppressed calls will be buffered
  • @return
    • Returns this

.unsuppress ( flush = true )

Ends suppression.

  • @param \ flush
    • If true, all the buffered calls are sent to the console at once
  • @return
    • Returns this

.flush ( )

Outputs and clears the current buffers


.clearBuffers ( )

Clears the current buffers without output


.option ( name[, value] )

Sets or returns an option value by name

  • @param \ name
    • Name of the option
  • @param \ value
    • New value to set to the option
  • @return
    • Returns this if value is provided. Otherwise, returns the option value

Available Options:

name type description
label string If any string is set, it appears preceding every console output.
forceOutput boolean If it is true, suppression is completely ignored.
  1. // Example
  2. let console = require('conso1e').create();
  3. console.option('label', '[LABEL]');
  4. console.log('ABC'); // '[LABEL] ABC'
  5. console.log('DEF'); // '[LABEL] DEF'

.subcontext ( )

Creates and returns a subcontext. Subcontext is a child conso1e instance that inherits the current state and the core from the parent.

A subcontext defaults to the parent’s current state and the option values. However you can override these individually.

  1. // Example
  2. let parent = require('conso1e').create();
  3. let child = parent.subcontext();
  4. parent.option('label', '[LABEL]');
  5. parent.log('ABC'); // '[LABEL] ABC'
  6. child.log('123'); // '[LABEL] 123' // label is inherited
  7. child.option('label', '[SUB_LABEL]');
  8. parent.log('ABC'); // '[LABEL] ABC'
  9. child.log('123'); // '[SUB_LABEL] 123' // label is overriden
  • @return
    • Returns a new subcontext instance

.core

The real console object that is wrapped

@type object *(read only)


.parent

The parent conso1e instance. It is null if this is not a subcontext

@type object \ *(read only)


.isSuppressed

Whether suppression is currently active, or not

@type boolean *(read only)


.isBuffering

Whether buffering is currently active, or not

@type boolean *(read only)


.hasBuffer

Whether the console has any buffered call

@type boolean *(read only)

Advanced Usage

Custom Console

create() function has the exact same parameters as the constructor of the built-in Console class.

  1. const debugLog = fs.createWriteStream('./debug.log');
  2. const errorLog = fs.createWriteStream('./error.log');
  3. const console = require('conso1e').create(debugLog, errorLog);

You can also pass a console object to wrap() :

  1. const myConsole = new console.Console(debugLog, errorLog);
  2. const console = require('conso1e').wrap(myConsole);

© 2020 amekusa