Skip to content

Commit

Permalink
[Log] Provide a default log category for Y.log calls
Browse files Browse the repository at this point in the history
Set a default log category of info if no category was specified, or the
category specified is not in the list of valid categories.
  • Loading branch information
andrewnicols committed Feb 6, 2014
1 parent 3d336a7 commit dacc8fe
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/yui/js/yui-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ INSTANCE.log = function(msg, cat, src, silent) {
bail = excl[src];
}

// Set a default category of info if the category was not defined or was not
// a real category.
if ((typeof cat === 'undefined') || !(cat in LEVELS)) {

This comment has been minimized.

Copy link
@rgrove

rgrove Apr 2, 2014

Contributor

@andrewnicols This line breaks log messages from YUI Test.

Prior to this commit, Y.log() allowed arbitrary log levels, which was useful in places like YUI Test where we want to log messages with level 'pass' to indicate passing tests, 'fail' to indicate failing tests, etc. It's also useful in developer-land for logging and filtering based on custom criteria.

As of this commit, test results from YUI Test are all logged as 'info', which means test failures are not properly reported in the browser test console, grover, or yeti. What was the reason for restricting log categories to a hard-coded set of categories?

This comment has been minimized.

Copy link
@triptych

triptych Apr 2, 2014

Contributor

This looks like a release blocker for 3.16.0 - we need to either fix or revert this commit.

This comment has been minimized.

Copy link
@triptych

triptych Apr 2, 2014

Contributor

Created an issue for this : #1745

cat = 'info';
}

// Determine the current minlevel as defined in configuration
Y.config.logLevel = Y.config.logLevel || 'debug';
minlevel = LEVELS[Y.config.logLevel.toLowerCase()];
Expand Down
63 changes: 63 additions & 0 deletions src/yui/tests/unit/assets/core-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ YUI.add('core-tests', function(Y) {
'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 @@ -438,6 +439,68 @@ YUI.add('core-tests', function(Y) {
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() {
Expand Down

0 comments on commit dacc8fe

Please sign in to comment.