Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
Fixed bug with snooping on loggers that are disabled.

Set the default buffer size to zero. It's now opt-in via config().

Added isEnabled() for checking if namespace is enabled.

Updated NPM deps.
  • Loading branch information
cb1kenobi committed Feb 13, 2017
1 parent 1fb142f commit c58f1d9
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 38 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snooplogg",
"version": "1.1.1",
"version": "1.2.0",
"description": "Your mind on your logs and your logs on your mind",
"main": "./dist/index.js",
"author": "Chris Barber <chris@cb1inc.com> (https://github.com/cb1kenobi)",
Expand All @@ -12,7 +12,9 @@
"logs",
"levels",
"debug",
"console"
"console",
"snoop",
"snooping"
],
"scripts": {
"build": "gulp build",
Expand Down Expand Up @@ -44,7 +46,7 @@
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-babel-istanbul": "^1.6.0",
"gulp-debug": "^3.0.0",
"gulp-debug": "^3.1.0",
"gulp-esdoc": "^0.4.0",
"gulp-eslint": "^3.0.1",
"gulp-filter": "^5.0.0",
Expand Down
60 changes: 38 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,7 @@ class Logger extends Function {
* @type {Boolean}
*/
get enabled() {
let root = this._root || this;

const allow = root._allow;
if (allow === null) {
// all logging is silenced
return false;
}

if (!this._namespace || allow === '*') {
// nothing to filter
return true;
}

const ignore = root._ignore;
if (allow && allow.test(this._namespace) && (!ignore || !ignore.test(this._namespace))) {
return true;
}

return false;
return (this._root || this).isEnabled(this._namespace);
}

/**
Expand Down Expand Up @@ -137,7 +119,7 @@ class SnoopLogg extends Logger {
* The log message buffer.
* @type {Array.<Object>}
*/
_buffer: { writable: true, value: new NanoBuffer(250) },
_buffer: { writable: true, value: new NanoBuffer(0) },

/**
* An array of available colors to choose from when rendering
Expand Down Expand Up @@ -560,7 +542,7 @@ class SnoopLogg extends Logger {
// flush the buffers
if (opts.flush) {
for (const msg of this._buffer) {
if (msg.enabled) {
if ((msg.id === this._id && msg.enabled) || (msg.id !== this._id && this.isEnabled(msg.ns))) {
msg.formatter = opts.theme && this._themes[opts.theme] || this._themes[this._defaultTheme];
stream.write(stream._writableState && stream._writableState.objectMode ? msg : format(msg));
}
Expand Down Expand Up @@ -629,6 +611,33 @@ class SnoopLogg extends Logger {
}
}

/**
* Determines if the specified namespace is enabled.
*
* @param {?String} namespace - The namespace.
* @returns {Boolean}
* @access public
*/
isEnabled(namespace) {
const allow = this._allow;
if (allow === null) {
// all logging is silenced
return false;
}

if (!namespace || allow === '*') {
// nothing to filter
return true;
}

const ignore = this._ignore;
if (allow && allow.test(namespace) && (!ignore || !ignore.test(namespace))) {
return true;
}

return false;
}

/**
* Stylizes text.
*
Expand Down Expand Up @@ -821,4 +830,11 @@ exports = module.exports = instance;

export default instance;

export { createInstanceWithDefaults, Format, Logger, SnoopLogg, StripColors };
export {
createInstanceWithDefaults,
Format,
Logger,
SnoopLogg,
StdioStream,
StripColors
};
3 changes: 2 additions & 1 deletion test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"sinon": false
},
"rules": {
"no-unused-vars": 0
"no-unused-vars": 0,
"no-debugger": 0
}
}
32 changes: 31 additions & 1 deletion test/test-snoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,8 @@ describe('SnoopLogg', () => {
}

const instance = createInstanceWithDefaults()
.enable('*');
.enable('*')
.config({ maxBufferSize: 10 });

for (let i = 0; i < 10; i++) {
instance.log(`foo${i}`);
Expand Down Expand Up @@ -740,6 +741,35 @@ describe('SnoopLogg', () => {
expect(count).to.equal(10);
});

it('should flush buffered snooped messages', () => {
class MockOutputStream extends Writable {
_write(msg, enc, cb) {
expect(msg.toString()).to.equal('\u001b[34mfoo\u001b[39m test!\n');
cb();
}
}

const instance = createInstanceWithDefaults()
.config({ maxBufferSize: 10 })
.enable('*')
.snoop();

const instance2 = createInstanceWithDefaults().enable();
const log = instance2('foo');

log.log('test!');

instance.pipe(new MockOutputStream, { flush: true });

try {
log('test!');
} catch (e) {
throw e;
} finally {
instance.unsnoop();
}
});

it('should unpipe a stream', () => {
let count = 0;

Expand Down
22 changes: 11 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ ajv-keywords@^1.0.0:
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-1.5.1.tgz#314dd0a4b3368fad3dfcdc54ede6171b886daf3c"

ajv@^4.7.0:
version "4.11.2"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-4.11.2.tgz#f166c3c11cbc6cb9dcc102a5bcfe5b72c95287e6"
version "4.11.3"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-4.11.3.tgz#ce30bdb90d1254f762c75af915fb3a63e7183d22"
dependencies:
co "^4.6.0"
json-stable-stringify "^1.0.1"
Expand Down Expand Up @@ -955,8 +955,8 @@ debug@2.2.0:
ms "0.7.1"

debug@2.X, debug@^2.1.1, debug@^2.2.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.0.tgz#bc596bcabe7617f11d9fa15361eded5608b8499b"
version "2.6.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.1.tgz#79855090ba2c4e3115cc7d8769491d58f0491351"
dependencies:
ms "0.7.2"

Expand Down Expand Up @@ -1656,9 +1656,9 @@ gulp-babel@^6.1.2:
through2 "^2.0.0"
vinyl-sourcemaps-apply "^0.2.0"

gulp-debug@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/gulp-debug/-/gulp-debug-3.0.0.tgz#8388308e00753b9122b0f88e4ffaf2a3e1c4d0d5"
gulp-debug@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/gulp-debug/-/gulp-debug-3.1.0.tgz#4da91568b54915be803696caaac10c895b2c0a71"
dependencies:
chalk "^1.0.0"
gulp-util "^3.0.0"
Expand Down Expand Up @@ -2126,8 +2126,8 @@ js-yaml@3.6.1, js-yaml@3.x, js-yaml@^3.5.1:
esprima "^2.6.0"

jsbn@~0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.0.tgz#650987da0dd74f4ebf5a11377a2aa2d273e97dfd"
version "0.1.1"
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"

jsdom@^7.0.2:
version "7.2.2"
Expand Down Expand Up @@ -3255,8 +3255,8 @@ supports-color@^3.2.3:
has-flag "^1.0.0"

"symbol-tree@>= 3.1.0 < 4.0.0":
version "3.2.1"
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.1.tgz#8549dd1d01fa9f893c18cc9ab0b106b4d9b168cb"
version "3.2.2"
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.2.tgz#ae27db38f660a7ae2e1c3b7d1bc290819b8519e6"

table@^3.7.8:
version "3.8.3"
Expand Down

0 comments on commit c58f1d9

Please sign in to comment.