From fc19b1e47d805684ee70a9d8318abe7b7393ba0f Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 16 Dec 2016 16:13:26 -0500 Subject: [PATCH] Add can-util/log/log 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. --- js/dev/dev.js | 38 +++++++---------------------------- js/log/log-test.js | 19 ++++++++++++++++++ js/log/log.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++ js/log/test.html | 2 ++ js/tests.js | 1 + 5 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 js/log/log-test.js create mode 100644 js/log/log.js create mode 100644 js/log/test.html diff --git a/js/dev/dev.js b/js/dev/dev.js index c15c36c6..a4c34b00 100644 --- a/js/dev/dev.js +++ b/js/dev/dev.js @@ -1,3 +1,5 @@ +var canLog = require("../log/log"); + module.exports = { warnTimeout: 5000, logLevel: 0, @@ -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 }, /** @@ -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 }; diff --git a/js/log/log-test.js b/js/log/log-test.js new file mode 100644 index 00000000..6f772886 --- /dev/null +++ b/js/log/log-test.js @@ -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"); + }); +} diff --git a/js/log/log.js b/js/log/log.js new file mode 100644 index 00000000..111a6444 --- /dev/null +++ b/js/log/log.js @@ -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); + } +}; diff --git a/js/log/test.html b/js/log/test.html new file mode 100644 index 00000000..90b2d2b9 --- /dev/null +++ b/js/log/test.html @@ -0,0 +1,2 @@ + + diff --git a/js/tests.js b/js/tests.js index ac9f2370..98674bd5 100644 --- a/js/tests.js +++ b/js/tests.js @@ -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');