Skip to content

Commit

Permalink
Merge pull request #93 from wagenet/limit-logging
Browse files Browse the repository at this point in the history
Limit logging
  • Loading branch information
mixonic authored May 27, 2021
2 parents a320feb + 13526c9 commit ff81357
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
59 changes: 59 additions & 0 deletions tests/acceptance/workflow-config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,63 @@ module('workflow config', function (hooks) {
};
deprecate(message, false, options);
});

test('deprecation limits each id to 100 console.logs', (assert) => {
let limit = 100;

let message = 'log-id';
let id = 'ember.workflow';
let options = { id, since: '2.0.0', until: '3.0.0', for: 'testing' };
let expected = `DEPRECATION: ${message}`;

let count = 0;
window.Testem.handleConsoleMessage = function (passedMessage) {
count++;
if (count <= limit) {
assert.equal(
passedMessage.substr(0, expected.length),
expected,
'deprecation logs'
);
} else if (count === limit + 1) {
assert.equal(
passedMessage,
'To avoid console overflow, this deprecation will not be logged any more in this run.'
);
} else {
assert.ok(false, 'No further logging expected');
}
};

// Run one more time than the limit
for (let i = 0; i <= limit; i++) {
deprecate(message, false, options);
}

assert.equal(count, limit + 1, 'logged 101 times, including final notice');

let secondMessage = 'log-id2';
let secondId = 'ember.workflow2';
let secondOptions = {
id: secondId,
since: '2.0.0',
until: '3.0.0',
for: 'testing',
};
let secondExpected = `DEPRECATION: ${secondMessage}`;

let secondCount = 0;
window.Testem.handleConsoleMessage = function (passedMessage) {
secondCount++;
assert.equal(
passedMessage.substr(0, secondExpected.length),
secondExpected,
'second deprecation logs'
);
};

deprecate(secondMessage, false, secondOptions);

assert.equal(secondCount, 1, 'logged deprecation with different id');
});
});
15 changes: 14 additions & 1 deletion vendor/ember-cli-deprecation-workflow/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* globals self */

const LOG_LIMIT = 100;

(function(){
self.deprecationWorkflow = self.deprecationWorkflow || {};
self.deprecationWorkflow.deprecationLog = {
messages: { }
};
self.deprecationWorkflow.logCounts = {};

function detectWorkflow(config, message, options) {
if (!config || !config.workflow) {
Expand Down Expand Up @@ -43,7 +46,17 @@
// no-op
break;
case 'log':
console.warn('DEPRECATION: ' + message);
var key = options && options.id || message;
let count = self.deprecationWorkflow.logCounts[key] || 0;
self.deprecationWorkflow.logCounts[key] = count + 1;

if (count <= LOG_LIMIT) {
console.warn('DEPRECATION: ' + message);
if (count === LOG_LIMIT) {
console.warn('To avoid console overflow, this deprecation will not be logged any more in this run.');
}
}

break;
case 'throw':
throw new Error(message);
Expand Down

0 comments on commit ff81357

Please sign in to comment.