Skip to content

Commit

Permalink
Add can-util/log/log
Browse files Browse the repository at this point in the history
This adds a new module, can-util/log/log which can be used for logging.
This is like can-util/dev/dev, but is useful in production as well.

This also changes can-util/dev/dev to use can-util/log/log, but to
become noops in production.
  • Loading branch information
matthewp committed Dec 16, 2016
1 parent 23012c1 commit fc19b1e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 31 deletions.
38 changes: 7 additions & 31 deletions js/dev/dev.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var canLog = require("../log/log");

module.exports = {
warnTimeout: 5000,
logLevel: 0,
Expand All @@ -8,19 +10,9 @@ module.exports = {
* ```
* @param {String} out the message
*/
warn: function (out) {
warn: function() {
//!steal-remove-start
var ll = this.logLevel;
if (ll < 2) {
Array.prototype.unshift.call(arguments, 'WARN:');
if (typeof console !== "undefined" && console.warn) {
this._logger("warn", Array.prototype.slice.call(arguments));
} else if (typeof console !== "undefined" && console.log) {
this._logger("log", Array.prototype.slice.call(arguments));
} else if (window && window.opera && window.opera.postError) {
window.opera.postError("steal.js WARNING: " + out);
}
}
canLog.warn.apply(this, arguments);
//!steal-remove-end
},
/**
Expand All @@ -30,26 +22,10 @@ module.exports = {
* ```
* @param {String} out the message
*/
log: function (out) {
log: function() {
//!steal-remove-start
var ll = this.logLevel;
if (ll < 1) {
if (typeof console !== "undefined" && console.log) {
Array.prototype.unshift.call(arguments, 'Info:');
this._logger("log", Array.prototype.slice.call(arguments));
} else if (window && window.opera && window.opera.postError) {
window.opera.postError("steal.js INFO: " + out);
}
}
canLog.log.apply(this, arguments);
//!steal-remove-end
},
_logger: function (type, arr) {
//!steal-remove-start
try {
console[type].apply(console, arr);
} catch(e) {
console[type](arr);
}
//!steal-remove-end
}
_logger: canLog._logger
};
19 changes: 19 additions & 0 deletions js/log/log-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var QUnit = require("../../test/qunit");
var canLog = require("./log");

if(typeof console !== "undefined") {

QUnit.module("can-util/js/log");

QUnit.test("log.log works", function(){
QUnit.expect(2);
var log = console.log;
console.log = function(type, msg){
QUnit.equal(type, "Info:");
QUnit.equal(msg, "it worked");
console.log = log;
};

canLog.log("it worked");
});
}
50 changes: 50 additions & 0 deletions js/log/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
exports.warnTimeout = 5000;
exports.logLevel = 0;

/**
* Adds a warning message to the console.
* ```
* canLog.warn("something evil");
* ```
* @param {String} out the message
*/
exports.warn = function (out) {
var ll = this.logLevel;
if (ll < 2) {
Array.prototype.unshift.call(arguments, 'WARN:');
if (typeof console !== "undefined" && console.warn) {
this._logger("warn", Array.prototype.slice.call(arguments));
} else if (typeof console !== "undefined" && console.log) {
this._logger("log", Array.prototype.slice.call(arguments));
} else if (window && window.opera && window.opera.postError) {
window.opera.postError("steal.js WARNING: " + out);
}
}
};

/**
* Adds a message to the console.
* ```
* canLog.log("hi");
* ```
* @param {String} out the message
*/
exports.log = function (out) {
var ll = this.logLevel;
if (ll < 1) {
if (typeof console !== "undefined" && console.log) {
Array.prototype.unshift.call(arguments, 'Info:');
this._logger("log", Array.prototype.slice.call(arguments));
} else if (window && window.opera && window.opera.postError) {
window.opera.postError("steal.js INFO: " + out);
}
}
};

exports._logger = function (type, arr) {
try {
console[type].apply(console, arr);
} catch(e) {
console[type](arr);
}
};
2 changes: 2 additions & 0 deletions js/log/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<script src="../../node_modules/steal/steal.js" main="can-util/js/log/log-test"></script>
1 change: 1 addition & 0 deletions js/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require('./cid-set/cid-set-test');
require('./deep-assign/deep-assign-test');
require('./defaults/defaults-test');
require('./dev/dev-test');
require('./log/log-test');
require('./diff/diff-test');
require('./diff-array/diff-array-test');
require('./diff-object/diff-object-test');
Expand Down

0 comments on commit fc19b1e

Please sign in to comment.