Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differential loglevel #1678

Open
wants to merge 2 commits into
base: dev-3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/yui/js/yui-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,27 @@ INSTANCE.log = function(msg, cat, src, silent) {
if (typeof src !== "undefined") {
excl = c.logExclude;
incl = c.logInclude;
if (incl && !(src in incl)) {
bail = 1;
} else if (incl && (src in incl)) {
bail = !incl[src];
} else if (excl && (src in excl)) {
bail = excl[src];
if (incl) {
if (src in incl) {
// Exact match found -g rab the log level and bail setting.
bail = !incl[src];
minlevel = incl[src];
} else if (incl['*']) {
// Allow specification of differential logging.
bail = !incl['*'];
minlevel = incl['*'];
} else {
// No matching log categories, bail
bail = 1;
}
} else if (excl) {
if (src in excl) {
// Exact match found - grab the log level and bail setting.
bail = excl[src];
} else if (excl['*']) {
// Allow specification of differential logging.
bail = excl['*'];
}
}

// Set a default category of info if the category was not defined or was not
Expand All @@ -62,8 +77,10 @@ INSTANCE.log = function(msg, cat, src, silent) {
}

// Determine the current minlevel as defined in configuration
Y.config.logLevel = Y.config.logLevel || 'debug';
minlevel = LEVELS[Y.config.logLevel.toLowerCase()];
if (typeof minlevel !== 'string') {
minlevel = Y.config.logLevel || 'debug';
}
minlevel = LEVELS[minlevel.toLowerCase()];

if (cat in LEVELS && LEVELS[cat] < minlevel) {
// Skip this message if the we don't meet the defined minlevel
Expand Down
68 changes: 68 additions & 0 deletions src/yui/js/yui.js
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,47 @@ supported console.
A hash of log sources that should be logged. If specified, only messages from
these sources will be logged. Others will be discarded.

The object key consists of:
<ul>
<li>An exact match for the `src` specified when calling Y.log; or</li>
<li>The `*` source - to indicate a logLevel for all other sources.</li>
</ul>

Each object value in the hash can take either:
<ul>
<li>`true` - in which case, the log threshold defined in Y.config.logLevel is used;</li>
<li>`false` - in which case, that source is excluded; or</li>
<li>a valid logLevel.</li>
</ul>

See <a href="../classes/config.html#property_logLevel" class="crosslink">Y.config.logLevel</a> for details of the full list of log levels.

If <a href="#property_logExclude" class="crosslink">logExclude</a> has also been specified, then this logInclude setting
will take precedent and logExclude will be ignored entirely.

An example configuration is shown below:

YUI_config = {
// The default logLevel will be warn:
logLevel: 'warn',
logInclude: {
// Reduce the logging threshold to debug for event-delegate:
'event-delegate': 'debug',

// Reduce the logging threshold to info for event-logged-at-info:
'event-logged-at-info': 'info',

// Reduce the logging threshold to Y.config.logLevel for event-logged-at-true:
'event-logged-at-true': true,

// Do not log anything for 'event-not-logged':
'event-not-logged': false,

// Do not log anything for everything else:
'*': false
}
};

@property {Object} logInclude
@type object
**/
Expand All @@ -1681,6 +1722,33 @@ these sources will be logged. Others will be discarded.
A hash of log sources that should be not be logged. If specified, all sources
will be logged *except* those on this list.

The object key consists of:
<ul>
<li>An exact match for the `src` specified when calling Y.log; or</li>
<li>The `*` source - to indicate a setting for all other sources.</li>
</ul>

The object value must be a boolean, unlike in logInclude.

If <a href="#property_logInclude" class="crosslink">logInclude</a> has been specified, then it will take precedent and
logExclude setting will be ignored entirely.

An example configuration which filters out all sources with the exception of event-delegate.

YUI_config = {
// The default logLevel will be warn:
logLevel: 'warn',
logExclude: {
// Do not exclude event-delegate:
'event-delegate': false,

// Exclude everything else:
'*': true,
}
};

<em>Note: The above example can be more better achieved using logInclude, but is displayed for completeness.</em>

@property {Object} logExclude
**/

Expand Down
238 changes: 0 additions & 238 deletions src/yui/tests/unit/assets/core-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ YUI.add('core-tests', function(Y) {
ignore: {
'getLocation() should return the location object': (Y.UA.nodejs ? true : false),
'getLocation() should return `null` when executing in node.js': (!Y.UA.nodejs || (Y.UA.nodejs && Y.config.win)), //If there is a window object, ignore too
test_log_params: (typeof console == "undefined" || !console.info || Y.UA.nodejs),
test_log_default_category: (typeof console == "undefined" || !console.info || Y.UA.nodejs),
'test: domready delay': !Y.config.win,
'test: window.onload delay': !Y.config.win,
'test: contentready delay': !Y.config.win,
Expand Down Expand Up @@ -267,242 +265,6 @@ YUI.add('core-tests', function(Y) {
//Assert.areSame(info.filter, core_urls[i].filter, 'Filters do not match (' + core_urls[i].path + ')');
}
},
test_log_params: function() {
if (typeof console == "undefined" || !console.info) {
return;
}
var l = console.info,
Assert = Y.Assert,
last;

// Override all of the console functions so that we can check
// their return values.
console.error = console.log = console.warn = console.debug = console.info = function(str) {
last = str.split(':')[0];
};

YUI().use(function (Y) {
Y.applyConfig({
logInclude: {
logMe: true,
butNotMe: false
}
});

Y.log('test logInclude logMe','info','logMe');
Assert.areEqual(last, 'logMe', 'logInclude (true) Failed');
last = undefined;

Y.log('test logInclude butNotMe','info','butNotMe');
Assert.isUndefined(last, 'logInclude (false) Failed');

Y.applyConfig({
logInclude: '',
logExclude: {
excludeMe: true,
butDontExcludeMe: false
}
});
Y.log('test logExclude excludeMe','info','excludeMe');
Assert.isUndefined(last, 'excludeInclude (true) Failed');
Y.log('test logExclude butDontExcludeMe','info','butDontExcludeMe');
Assert.areEqual(last, 'butDontExcludeMe', 'logExclue (false) Failed');

Y.applyConfig({
logInclude: {
davglass: true
},
logExclude: {
'': true
}
});
last = undefined;
Y.log('This should be ignored', 'info');
Assert.isUndefined(last, 'Failed to exclude log param with empty string');
Y.log('This should NOT be ignored', 'info', 'davglass');
Assert.areEqual(last, 'davglass', 'Failed to include log param');

// Default logLevel is debug
Y.applyConfig({
logInclude: {
'logleveltest': true
}
});
last = undefined;
Y.log('This should be logged', 'debug', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
last = undefined;
Y.log('This should be logged', 'info', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
last = undefined;
Y.log('This should be logged', 'warn', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
last = undefined;
Y.log('This should be logged', 'error', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');

// Debug should also take effect when actively specified
Y.applyConfig({
logLevel: 'debug'
});
last = undefined;
Y.log('This should be logged', 'debug', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
last = undefined;
Y.log('This should be logged', 'info', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
last = undefined;
Y.log('This should be logged', 'warn', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
last = undefined;
Y.log('This should be logged', 'error', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');

// An invalid log level has the same effect as 'debug'
Y.applyConfig({
logLevel: 'invalidloglevel'
});
last = undefined;
Y.log('This should be logged', 'debug', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
last = undefined;
Y.log('This should be logged', 'info', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
last = undefined;
Y.log('This should be logged', 'warn', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
last = undefined;
Y.log('This should be logged', 'error', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');

Y.applyConfig({
logLevel: 'info'
});
last = undefined;
Y.log('This should NOT be logged', 'debug', 'logleveltest');
Assert.isUndefined(last, 'Failed to exclude log level below threshold');
last = undefined;
Y.log('This should be logged', 'info', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
last = undefined;
Y.log('This should be logged', 'warn', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
last = undefined;
Y.log('This should be logged', 'error', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');

Y.applyConfig({
logLevel: 'warn'
});
last = undefined;
Y.log('This should NOT be logged', 'debug', 'logleveltest');
Assert.isUndefined(last, 'Failed to exclude log level below threshold');
last = undefined;
Y.log('This should NOT be logged', 'info', 'logleveltest');
Assert.isUndefined(last, 'Failed to exclude log level below threshold');
last = undefined;
Y.log('This should be logged', 'warn', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
last = undefined;
Y.log('This should be logged', 'error', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');

Y.applyConfig({
logLevel: 'error'
});
last = undefined;
Y.log('This should NOT be logged', 'debug', 'logleveltest');
Assert.isUndefined(last, 'Failed to exclude log level below threshold');
last = undefined;
Y.log('This should NOT be logged', 'info', 'logleveltest');
Assert.isUndefined(last, 'Failed to exclude log level below threshold');
last = undefined;
Y.log('This should NOT be ignored', 'warn', 'logleveltest');
Assert.isUndefined(last, 'Failed to exclude log level below threshold');
last = undefined;
Y.log('This should NOT be ignored', 'error', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');

Y.applyConfig({
logLevel: 'ERROR'
});
last = undefined;
Y.log('This should NOT be logged', 'debug', 'logleveltest');
Assert.isUndefined(last, 'Failed to exclude log level below threshold - case insensitivty possibly ignored');
last = undefined;
Y.log('This should NOT be logged', 'info', 'logleveltest');
Assert.isUndefined(last, 'Failed to exclude log level below threshold - case insensitivty possibly ignored');
last = undefined;
Y.log('This should NOT be ignored', 'warn', 'logleveltest');
Assert.isUndefined(last, 'Failed to exclude log level below threshold - case insensitivty possibly ignored');
last = undefined;
Y.log('This should NOT be ignored', 'error', 'logleveltest');
Assert.areEqual(last, 'logleveltest', 'Failed to include log param');
});

console.info = l;
},
test_log_default_category: function() {
if (typeof console == "undefined" || !console.info) {
return;
}
var l = console.info,
Assert = Y.Assert,
consoleFn,
last, lastCategory;

// Override all of the console functions so that we can check
// their return values.
consoleFn = function(str) {
last = str.split(':')[0];
};
console.error = function(str) {
lastCategory = 'error';
consoleFn(str);
};
console.log = function(str) {
lastCategory = 'log';
consoleFn(str);
};
console.warn = function(str) {
lastCategory = 'warn';
consoleFn(str);
};
console.debug = function(str) {
lastCategory = 'debug';
consoleFn(str);
};
console.info = function(str) {
lastCategory = 'info';
consoleFn(str);
};

YUI().use(function (Y) {
Y.applyConfig({
logLevel: 'debug'
});
lastCategory = undefined;
Y.log('This has a valid log level', 'debug');
Assert.areEqual(lastCategory, 'debug', 'Failed to log at debug log category');
lastCategory = undefined;
Y.log('This has a valid log level', 'info');
Assert.areEqual(lastCategory, 'info', 'Failed to log at info log category');
lastCategory = undefined;
Y.log('This has a valid log level', 'warn');
Assert.areEqual(lastCategory, 'warn', 'Failed to log at warn log category');
lastCategory = undefined;
Y.log('This has a valid log level', 'error');
Assert.areEqual(lastCategory, 'error', 'Failed to log at error log category');
lastCategory = undefined;
Y.log('This has no log level and should use the default');
Assert.areEqual(lastCategory, 'info', 'Failed to log at default log category of info');
lastCategory = undefined;
Y.log('This has an invalid log level and should use the default', 'notice');
Assert.areEqual(lastCategory, 'info', 'Failed to log at default info log category');
});

console.info = l;
},
test_global_apply_config: function() {
var Assert = Y.Assert,
test = this;
Expand Down
Loading