-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
79 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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"); | ||
}); | ||
} |
This file contains 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
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); | ||
} | ||
}; |
This file contains 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
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> |
This file contains 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