Skip to content

Commit

Permalink
Add global verbosity control
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkGriffiths committed Mar 9, 2019
1 parent a46aef1 commit 9e61575
Show file tree
Hide file tree
Showing 14 changed files with 619 additions and 587 deletions.
57 changes: 35 additions & 22 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ <h4>Examples</h4>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Works like normal...'</span>)
<span class="hljs-built_in">console</span>.debug(<span class="hljs-string">'...but now controllable.'</span>)

<span class="hljs-built_in">console</span>.verbosity(<span class="hljs-number">3</span>)
<span class="hljs-built_in">console</span>.verbosity(<span class="hljs-number">3</span>) <span class="hljs-comment">// Use numbered levels 5 (debug) to 1 (error)</span>

<span class="hljs-built_in">console</span>.debug(<span class="hljs-string">'...this isn’t printed now.'</span>)

<span class="hljs-built_in">console</span>.canWrite(<span class="hljs-number">5</span>) &amp;&amp; <span class="hljs-built_in">console</span>.dir({<span class="hljs-attr">print</span>: <span class="hljs-string">'this won’t.'</span>})

<span class="hljs-built_in">console</span>.verbosity(<span class="hljs-number">5</span>)
<span class="hljs-built_in">console</span>.verbosity(<span class="hljs-string">'debug'</span>) <span class="hljs-comment">// Use named levels [debug, info, log, warning, error]</span>

<span class="hljs-built_in">console</span>.canWrite(<span class="hljs-number">5</span>) &amp;&amp; <span class="hljs-built_in">console</span>.dir({<span class="hljs-attr">print</span>: <span class="hljs-string">'this will now.'</span>})</pre>
<p>This will direct all console output to stderr, but silence 'info' and 'debug' messages.</p>
Expand Down Expand Up @@ -278,7 +278,7 @@ <h3 class='left m0 panel-title' id='createconsole'>
</h3>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/index.js#L15-L17'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/index.js#L15-L17'>
<span>src/index.js</span>
</a>

Expand Down Expand Up @@ -354,7 +354,7 @@ <h3 class='left m0 panel-title' id='getversion'>
</h3>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/index.js#L25-L25'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/index.js#L25-L25'>
<span>src/index.js</span>
</a>

Expand Down Expand Up @@ -430,7 +430,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
</h3>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L31-L288'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L32-L303'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -503,6 +503,8 @@ <h3 class='left m0 panel-title' id='verbosity'>

<tr>
<td class='break-word'><span class='code bold'>options.verbosity</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></code>

(default <code>3</code>)
</td>
<td class='break-word'><span>The verboseness of output:
0: Mute
Expand Down Expand Up @@ -534,6 +536,17 @@ <h3 class='left m0 panel-title' id='verbosity'>



<tr>
<td class='break-word'><span class='code bold'>options.global</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code>

(default <code>true</code>)
</td>
<td class='break-word'><span>Should changes to verbosity be made globally?
</span></td>
</tr>



<tr>
<td class='break-word'><span class='code bold'>options.prefix</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a></code>
</td>
Expand Down Expand Up @@ -587,7 +600,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L81-L88'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L89-L103'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand All @@ -597,7 +610,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<p>Set the current verbosity.</p>


<div class='pre p1 bg-darken-1 mt0 rounded signature'>verbosity(level: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a>) → <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></div>
<div class='pre p1 bg-darken-1 mt0 rounded signature'>verbosity(level: (<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>)) → <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></div>



Expand All @@ -614,8 +627,8 @@ <h3 class='left m0 panel-title' id='verbosity'>

<div class='space-bottom0'>
<div>
<span class='code bold'>level</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a>)</code>
The current level (0 to 5).
<span class='code bold'>level</span> <code class='quiet'>((<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>))</code>
The current level (0 to 5) or level name.

</div>

Expand Down Expand Up @@ -664,7 +677,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L95-L98'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L110-L113'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -742,7 +755,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L130-L132'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L145-L147'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -820,7 +833,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L130-L132'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L145-L147'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -898,7 +911,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L139-L141'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L154-L156'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -976,7 +989,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L148-L150'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L163-L165'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -1054,7 +1067,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L157-L159'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L172-L174'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -1132,7 +1145,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L166-L168'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L181-L183'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -1210,7 +1223,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L175-L177'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L190-L192'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -1288,7 +1301,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L184-L186'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L199-L201'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -1366,7 +1379,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L193-L195'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L208-L210'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -1444,7 +1457,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L202-L207'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L217-L222'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -1523,7 +1536,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L228-L240'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L243-L255'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down Expand Up @@ -1629,7 +1642,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
<div class='clearfix'>


<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/f54cc3a10606b613baeb95fa37497169e6e0d418/src/lib/verbosity.class.js#L260-L287'>
<a class='pl3 pr1 right rounded github' href='https://github.com/MarkGriffiths/verbosity/blob/a46aef135100739b56d52865eafdba6d5508a71c/src/lib/verbosity.class.js#L275-L302'>
<span>src/lib/verbosity.class.js</span>
</a>

Expand Down
30 changes: 23 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Object.defineProperty(exports, '__esModule', { value: true });

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var chalk = _interopDefault(require('chalk'));
var meta = _interopDefault(require('@thebespokepixel/meta'));
var util = _interopDefault(require('util'));
var termNG = _interopDefault(require('term-ng'));
var chalk = _interopDefault(require('chalk'));
var sparkles = _interopDefault(require('sparkles'));
var time = require('@thebespokepixel/time');
var meta = _interopDefault(require('@thebespokepixel/meta'));

function matrix(sOut, sErr) {
return {
Expand Down Expand Up @@ -67,9 +67,10 @@ class Verbosity extends Console {
constructor({
outStream,
errorStream,
verbosity,
verbosity = 3,
timestamp,
namespace,
global = true,
prefix
} = {}) {
const sOut = (ws => {
Expand All @@ -90,23 +91,38 @@ class Verbosity extends Console {

super(sOut, sErr);
this.willEmit = Boolean(namespace);
this.globalControl = Boolean(global);

this.timeFormatter = (ts => ts ? () => `[${chalk.dim(time.bespokeTimeFormat(ts))}] ` : () => '')(timestamp);

this.prefixFormatter = (pfix => pfix ? () => `[${pfix}] ` : () => '')(prefix);

this._stdout = sOut;
this._stderr = sErr;
this.threshold = verbosity ? verbosity : 3;
this.threshold = verbosity;
this.globalVerbosityController = this.globalControl && sparkles('verbosityGlobal');
this.emitter = this.willEmit && sparkles(namespace);
this.matrix = matrix(sOut, sErr);
this.globalVerbosityController.on('level', ({
level
}) => {
this.threshold = level;
});
}

verbosity(level) {
level = typeof level === 'string' ? this.matrix[level] : level;
if (level) {
level = typeof level === 'string' ? this.matrix[level].level : level;

if (level < 6) {
this.threshold = level;
if (level < 6) {
this.threshold = level;
}

if (this.globalControl) {
this.globalVerbosityController.emit('level', {
level
});
}
}

return this.threshold;
Expand Down
30 changes: 23 additions & 7 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import chalk from 'chalk';
import meta from '@thebespokepixel/meta';
import util from 'util';
import termNG from 'term-ng';
import chalk from 'chalk';
import sparkles from 'sparkles';
import { bespokeTimeFormat } from '@thebespokepixel/time';
import meta from '@thebespokepixel/meta';

function matrix(sOut, sErr) {
return {
Expand Down Expand Up @@ -61,9 +61,10 @@ class Verbosity extends Console {
constructor({
outStream,
errorStream,
verbosity,
verbosity = 3,
timestamp,
namespace,
global = true,
prefix
} = {}) {
const sOut = (ws => {
Expand All @@ -84,23 +85,38 @@ class Verbosity extends Console {

super(sOut, sErr);
this.willEmit = Boolean(namespace);
this.globalControl = Boolean(global);

this.timeFormatter = (ts => ts ? () => `[${chalk.dim(bespokeTimeFormat(ts))}] ` : () => '')(timestamp);

this.prefixFormatter = (pfix => pfix ? () => `[${pfix}] ` : () => '')(prefix);

this._stdout = sOut;
this._stderr = sErr;
this.threshold = verbosity ? verbosity : 3;
this.threshold = verbosity;
this.globalVerbosityController = this.globalControl && sparkles('verbosityGlobal');
this.emitter = this.willEmit && sparkles(namespace);
this.matrix = matrix(sOut, sErr);
this.globalVerbosityController.on('level', ({
level
}) => {
this.threshold = level;
});
}

verbosity(level) {
level = typeof level === 'string' ? this.matrix[level] : level;
if (level) {
level = typeof level === 'string' ? this.matrix[level].level : level;

if (level < 6) {
this.threshold = level;
if (level < 6) {
this.threshold = level;
}

if (this.globalControl) {
this.globalVerbosityController.emit('level', {
level
});
}
}

return this.threshold;
Expand Down
Loading

0 comments on commit 9e61575

Please sign in to comment.