Skip to content

Commit 9e61575

Browse files
committedMar 9, 2019
Add global verbosity control
1 parent a46aef1 commit 9e61575

14 files changed

+619
-587
lines changed
 

‎docs/index.html

+35-22
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,13 @@ <h4>Examples</h4>
215215
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Works like normal...'</span>)
216216
<span class="hljs-built_in">console</span>.debug(<span class="hljs-string">'...but now controllable.'</span>)
217217

218-
<span class="hljs-built_in">console</span>.verbosity(<span class="hljs-number">3</span>)
218+
<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>
219219

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

222222
<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>})
223223

224-
<span class="hljs-built_in">console</span>.verbosity(<span class="hljs-number">5</span>)
224+
<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>
225225

226226
<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>
227227
<p>This will direct all console output to stderr, but silence 'info' and 'debug' messages.</p>
@@ -278,7 +278,7 @@ <h3 class='left m0 panel-title' id='createconsole'>
278278
</h3>
279279

280280

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

@@ -354,7 +354,7 @@ <h3 class='left m0 panel-title' id='getversion'>
354354
</h3>
355355

356356

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

@@ -430,7 +430,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
430430
</h3>
431431

432432

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

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

504504
<tr>
505505
<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>
506+
507+
(default <code>3</code>)
506508
</td>
507509
<td class='break-word'><span>The verboseness of output:
508510
0: Mute
@@ -534,6 +536,17 @@ <h3 class='left m0 panel-title' id='verbosity'>
534536

535537

536538

539+
<tr>
540+
<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>
541+
542+
(default <code>true</code>)
543+
</td>
544+
<td class='break-word'><span>Should changes to verbosity be made globally?
545+
</span></td>
546+
</tr>
547+
548+
549+
537550
<tr>
538551
<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>
539552
</td>
@@ -587,7 +600,7 @@ <h3 class='left m0 panel-title' id='verbosity'>
587600
<div class='clearfix'>
588601

589602

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

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

599612

600-
<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>
613+
<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>
601614

602615

603616

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

615628
<div class='space-bottom0'>
616629
<div>
617-
<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>
618-
The current level (0 to 5).
630+
<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>
631+
The current level (0 to 5) or level name.
619632

620633
</div>
621634

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

666679

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

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

744757

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

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

822835

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

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

900913

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

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

978991

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

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

10561069

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

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

11341147

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

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

12121225

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

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

12901303

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

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

13681381

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

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

14461459

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

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

15251538

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

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

16311644

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

‎index.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
44

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

7-
var chalk = _interopDefault(require('chalk'));
7+
var meta = _interopDefault(require('@thebespokepixel/meta'));
88
var util = _interopDefault(require('util'));
99
var termNG = _interopDefault(require('term-ng'));
10+
var chalk = _interopDefault(require('chalk'));
1011
var sparkles = _interopDefault(require('sparkles'));
1112
var time = require('@thebespokepixel/time');
12-
var meta = _interopDefault(require('@thebespokepixel/meta'));
1313

1414
function matrix(sOut, sErr) {
1515
return {
@@ -67,9 +67,10 @@ class Verbosity extends Console {
6767
constructor({
6868
outStream,
6969
errorStream,
70-
verbosity,
70+
verbosity = 3,
7171
timestamp,
7272
namespace,
73+
global = true,
7374
prefix
7475
} = {}) {
7576
const sOut = (ws => {
@@ -90,23 +91,38 @@ class Verbosity extends Console {
9091

9192
super(sOut, sErr);
9293
this.willEmit = Boolean(namespace);
94+
this.globalControl = Boolean(global);
9395

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

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

98100
this._stdout = sOut;
99101
this._stderr = sErr;
100-
this.threshold = verbosity ? verbosity : 3;
102+
this.threshold = verbosity;
103+
this.globalVerbosityController = this.globalControl && sparkles('verbosityGlobal');
101104
this.emitter = this.willEmit && sparkles(namespace);
102105
this.matrix = matrix(sOut, sErr);
106+
this.globalVerbosityController.on('level', ({
107+
level
108+
}) => {
109+
this.threshold = level;
110+
});
103111
}
104112

105113
verbosity(level) {
106-
level = typeof level === 'string' ? this.matrix[level] : level;
114+
if (level) {
115+
level = typeof level === 'string' ? this.matrix[level].level : level;
107116

108-
if (level < 6) {
109-
this.threshold = level;
117+
if (level < 6) {
118+
this.threshold = level;
119+
}
120+
121+
if (this.globalControl) {
122+
this.globalVerbosityController.emit('level', {
123+
level
124+
});
125+
}
110126
}
111127

112128
return this.threshold;

‎index.mjs

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import chalk from 'chalk';
1+
import meta from '@thebespokepixel/meta';
22
import util from 'util';
33
import termNG from 'term-ng';
4+
import chalk from 'chalk';
45
import sparkles from 'sparkles';
56
import { bespokeTimeFormat } from '@thebespokepixel/time';
6-
import meta from '@thebespokepixel/meta';
77

88
function matrix(sOut, sErr) {
99
return {
@@ -61,9 +61,10 @@ class Verbosity extends Console {
6161
constructor({
6262
outStream,
6363
errorStream,
64-
verbosity,
64+
verbosity = 3,
6565
timestamp,
6666
namespace,
67+
global = true,
6768
prefix
6869
} = {}) {
6970
const sOut = (ws => {
@@ -84,23 +85,38 @@ class Verbosity extends Console {
8485

8586
super(sOut, sErr);
8687
this.willEmit = Boolean(namespace);
88+
this.globalControl = Boolean(global);
8789

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

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

9294
this._stdout = sOut;
9395
this._stderr = sErr;
94-
this.threshold = verbosity ? verbosity : 3;
96+
this.threshold = verbosity;
97+
this.globalVerbosityController = this.globalControl && sparkles('verbosityGlobal');
9598
this.emitter = this.willEmit && sparkles(namespace);
9699
this.matrix = matrix(sOut, sErr);
100+
this.globalVerbosityController.on('level', ({
101+
level
102+
}) => {
103+
this.threshold = level;
104+
});
97105
}
98106

99107
verbosity(level) {
100-
level = typeof level === 'string' ? this.matrix[level] : level;
108+
if (level) {
109+
level = typeof level === 'string' ? this.matrix[level].level : level;
101110

102-
if (level < 6) {
103-
this.threshold = level;
111+
if (level < 6) {
112+
this.threshold = level;
113+
}
114+
115+
if (this.globalControl) {
116+
this.globalVerbosityController.emit('level', {
117+
level
118+
});
119+
}
104120
}
105121

106122
return this.threshold;

‎package-lock.json

+445-522
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@
5252
"devDependencies": {
5353
"@babel/core": "^7.3.3",
5454
"@babel/preset-env": "^7.3.1",
55-
"ava": "^1.2.1",
55+
"ava": "^1.3.1",
5656
"documentation-theme-bespoke": "^0.5.2",
5757
"gulp": "^4.0.0",
58-
"gulp-better-rollup": "^3.4.0",
58+
"gulp-better-rollup": "^4.0.1",
5959
"gulp-rename": "^1.4.0",
6060
"gulp-strip-comments": "^2.5.2",
6161
"nyc": "^13.3.0",
62+
"rollup": "^1.6.0",
6263
"rollup-plugin-babel": "^4.3.2",
6364
"rollup-plugin-commonjs": "^9.2.1",
6465
"rollup-plugin-node-resolve": "^4.0.1",

‎readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ const console = createConsole({
4646
console.log('Works like normal...')
4747
console.debug('...but now controllable.')
4848

49-
console.verbosity(3)
49+
console.verbosity(3) // Use numbered levels 5 (debug) to 1 (error)
5050

5151
console.debug('...this isn’t printed now.')
5252

5353
console.canWrite(5) && console.dir({print: 'this won’t.'})
5454

55-
console.verbosity(5)
55+
console.verbosity('debug') // Use named levels [debug, info, log, warning, error]
5656

5757
console.canWrite(5) && console.dir({print: 'this will now.'})
5858
```

‎src/docs/example.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ const console = createConsole({
2626
console.log('Works like normal...')
2727
console.debug('...but now controllable.')
2828

29-
console.verbosity(3)
29+
console.verbosity(3) // Use numbered levels 5 (debug) to 1 (error)
3030

3131
console.debug('...this isn’t printed now.')
3232

3333
console.canWrite(5) && console.dir({print: 'this won’t.'})
3434

35-
console.verbosity(5)
35+
console.verbosity('debug') // Use named levels [debug, info, log, warning, error]
3636

3737
console.canWrite(5) && console.dir({print: 'this will now.'})
3838
```

‎src/lib/verbosity.class.js

+21-6
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ const {Console} = console
2525
* 5: Debug
2626
* @param {String} options.timestamp - Timestamp format.
2727
* @param {String} options.namespace - Sparkles namespace to emit events to.
28+
* @param {Boolean} options.global - Should changes to verbosity be made globally?
2829
* @param {String} options.prefix - Logging message prefix.
2930
* @return {Verbosity} Verbosity's console object.
3031
*/
3132
export default class Verbosity extends Console {
3233
constructor({
3334
outStream,
3435
errorStream,
35-
verbosity,
36+
verbosity = 3,
3637
timestamp,
3738
namespace,
39+
global = true,
3840
prefix
3941
} = {}) {
4042
const sOut = (ws => {
@@ -55,6 +57,7 @@ export default class Verbosity extends Console {
5557

5658
super(sOut, sErr)
5759
this.willEmit = Boolean(namespace)
60+
this.globalControl = Boolean(global)
5861

5962
this.timeFormatter = (ts => ts ?
6063
() => `[${chalk.dim(bespokeTimeFormat(ts))}] ` :
@@ -68,20 +71,32 @@ export default class Verbosity extends Console {
6871

6972
this._stdout = sOut
7073
this._stderr = sErr
71-
this.threshold = verbosity ? verbosity : 3
74+
this.threshold = verbosity
75+
this.globalVerbosityController = this.globalControl && sparkles('verbosityGlobal')
7276
this.emitter = this.willEmit && sparkles(namespace)
7377
this.matrix = matrix(sOut, sErr)
78+
79+
this.globalVerbosityController.on('level', ({level}) => {
80+
this.threshold = level
81+
})
7482
}
7583

7684
/**
7785
* Set the current verbosity.
78-
* @param {Number} level - The current level (0 to 5).
86+
* @param {Number|String} level - The current level (0 to 5) or level name.
7987
* @return {Number} The current verboseness (0 to 5).
8088
*/
8189
verbosity(level) {
82-
level = (typeof level === 'string') ? this.matrix[level] : level
83-
if (level < 6) {
84-
this.threshold = level
90+
if (level) {
91+
level = (typeof level === 'string') ? this.matrix[level].level : level
92+
93+
if (level < 6) {
94+
this.threshold = level
95+
}
96+
97+
if (this.globalControl) {
98+
this.globalVerbosityController.emit('level', {level})
99+
}
85100
}
86101

87102
return this.threshold

‎test/arguments.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict'
2-
31
import stream from 'stream'
42
import test from 'ava'
53
import {createConsole} from '..'

‎test/builtin.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict'
2-
31
import stream from 'stream'
42
import test from 'ava'
53
import {createConsole} from '..'

‎test/global.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import stream from 'stream'
2+
import test from 'ava'
3+
import {createConsole} from '..'
4+
5+
const StreamProxy1 = new stream.PassThrough()
6+
StreamProxy1.setEncoding('utf8')
7+
8+
const StreamProxy2 = new stream.PassThrough()
9+
StreamProxy2.setEncoding('utf8')
10+
11+
test('Set verbosity 1 on one instance and globally affects another', t => {
12+
const testConsole1 = createConsole({outStream: StreamProxy1})
13+
const testConsole2 = createConsole({outStream: StreamProxy2})
14+
testConsole1.verbosity(1)
15+
t.is(testConsole2.verbosity(), 1)
16+
})
17+
18+
test('Set verbosity 2 on one instance and globally affects another', t => {
19+
const testConsole1 = createConsole({outStream: StreamProxy1})
20+
const testConsole2 = createConsole({outStream: StreamProxy2})
21+
testConsole1.verbosity(2)
22+
t.is(testConsole2.verbosity(), 2)
23+
})
24+
25+
test('Set verbosity 3 on one instance and globally affects another', t => {
26+
const testConsole1 = createConsole({outStream: StreamProxy1})
27+
const testConsole2 = createConsole({outStream: StreamProxy2})
28+
testConsole1.verbosity(3)
29+
t.is(testConsole2.verbosity(), 3)
30+
})
31+
32+
test('Set verbosity 3 on one instance mutes debug output on another', t => {
33+
const testConsole1 = createConsole({outStream: StreamProxy1})
34+
const testConsole2 = createConsole({outStream: StreamProxy2})
35+
testConsole1.verbosity(3)
36+
testConsole2.debug('testing')
37+
const result = StreamProxy2.read()
38+
t.is(result, null)
39+
})
40+
41+
test('Set verbosity 5 on one instance allows info output on another', t => {
42+
const testConsole1 = createConsole({outStream: StreamProxy1})
43+
const testConsole2 = createConsole({outStream: StreamProxy2})
44+
testConsole1.verbosity(5)
45+
testConsole2.info('testing')
46+
const result = StreamProxy2.read()
47+
t.is(result, 'testing\n')
48+
})

‎test/logging.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
'use strict'
21
import stream from 'stream'
32
import test from 'ava'
43
import {createConsole} from '..'
@@ -71,22 +70,24 @@ const levels = [{
7170
critical: logbold('Damn you Chell, not again.', 'red', 'CRITICAL: ', true)
7271
}]
7372

74-
function runSuite(title_, console_, stamp_ = '') {
75-
let targetLevel = 5
76-
levels.forEach(suite => {
77-
console_.verbosity(targetLevel)
73+
function runSuite(
74+
title_,
75+
console_,
76+
levels_ = [5, 4, 3, 2, 1],
77+
stamp_ = '') {
78+
levels.forEach((suite, index) => {
79+
console_.verbosity(levels_[index])
7880
Object.keys(suite).forEach(level => {
7981
console_[level](suite[level].src)
8082
const result = StreamProxy.read()
81-
test(`${title_} @ ${targetLevel}, Level: ${level}: ${['-', stamp_ + suite[level].raw][0 | suite[level].willRead]}`, t => {
83+
test(`${title_} @ ${levels_[index]}, Level: ${level}: ${['-', stamp_ + suite[level].raw][0 | suite[level].willRead]}`, t => {
8284
if (suite[level].willRead) {
8385
t.deepEqual(`${stamp_}${suite[level].dest}`, result)
8486
} else {
8587
t.is(result, null)
8688
}
8789
})
8890
})
89-
targetLevel--
9091
})
9192
}
9293

@@ -95,8 +96,15 @@ runSuite(
9596
createConsole({outStream: StreamProxy})
9697
)
9798

99+
runSuite(
100+
'Named',
101+
createConsole({outStream: StreamProxy}),
102+
['debug', 'info', 'log', 'warn', 'error']
103+
)
104+
98105
runSuite(
99106
'Timestamp',
100107
createConsole({outStream: StreamProxy, timestamp: 'XX:XX:XX'}),
108+
undefined,
101109
`[${chalk.dim('XX:XX:XX')}] `
102110
)

‎test/module.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict'
2-
31
import stream from 'stream'
42
import test from 'ava'
53
import {createConsole, getVersion, Verbosity} from '..'
@@ -8,11 +6,11 @@ import pkg from '../package'
86
const StreamProxy = new stream.PassThrough()
97
StreamProxy.setEncoding('utf8')
108

11-
test(`Module version is '${pkg.version}'.`, t => {
9+
test(`Module version is '${pkg.version}'`, t => {
1210
t.is(`${pkg.version}`, getVersion())
1311
})
1412

15-
test(`Module long version is '${pkg.name} v${pkg.version}'.`, t => {
13+
test(`Module long version is '${pkg.name} v${pkg.version}'`, t => {
1614
t.is(`${pkg.name} v${pkg.version}`, getVersion(2))
1715
})
1816

‎test/obejcts.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/* eslint quotes:0 */
2-
'use strict'
3-
42
import stream from 'stream'
53
import test from 'ava'
64
import {createConsole} from '..'

0 commit comments

Comments
 (0)
Please sign in to comment.