Last active
December 16, 2015 15:59
-
-
Save bryanforbes/5460120 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// ice/logging/advanced.js | |
define(function (require, exports) { | |
exports.log = function (message, options) { | |
// ... | |
}; | |
exports.error = function (message, options) { | |
// ... | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ice/assert.js | |
define(function (require) { | |
var logger = require('./logging'); | |
return function (condition, text, custom) { | |
if (condition) { | |
return; | |
} | |
if (typeof text !== 'string') { | |
custom = text || custom || null; | |
text = null; | |
} | |
return logger.error(text, { | |
name: 'assert', | |
condition: condition, | |
custom: custom, | |
exception: new Error('LOG') | |
}); | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ice/logging/console.js | |
define(function () { | |
return console; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ice/logging/loader.js | |
define(function (require, exports, module) { | |
var defaultLogger = module.config().logger || './console'; | |
exports.load = function (id, parentRequire, loaded) { | |
var require = id.indexOf('./') === 0 ? parentRequire : require; | |
require(id, loaded); | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ice/logging.js | |
define(function (require) { | |
return require('./loader!'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Sample</title> | |
</head> | |
<body> | |
<script src="dojo/dojo.js"></script> | |
<script> | |
require({ | |
config: { | |
'ice/loader': { | |
logger: 'ice/logging/advanced' | |
} | |
} | |
}, ['ice/assert'], function (assert) { | |
// when assert is used, it uses the advanced | |
// logging facilities | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Sample</title> | |
</head> | |
<body> | |
<script src="dojo/dojo.js"></script> | |
<script> | |
require(['ice/assert'], function (assert) { | |
// when assert is used, it uses the native | |
// console object for logging | |
}); | |
</script> | |
</body> | |
</html> |
Very cool! Essentially it shows how to masquerade a logging facility as a primordial console object, and how to switch between them rather seamlessly. Besides the ability to use a basic logging practically for free, demonstrates the other side of the coin: it allows to retrofit an existing code with a flexible logging.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent example of how to solve the problem at hand as well as demonstrate a powerful AMD idiom.
In this case, I think it could be simplified by configuring the module ice/logging/loader and then require that module instead of ice/logging in ice assert. This would eliminate the logging module.