-
-
Save renemonroy/d63e332ec620e408add3 to your computer and use it in GitHub Desktop.
Revisions
-
bgrins revised this gist
May 5, 2013 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,8 +7,8 @@ // Your code here... log() away function log () { /* jshint -W021 */ if (window.console) { // Only run on the first time through - reset this function to the appropriate console.log helper @@ -29,5 +29,5 @@ // All at once (minified version): function log(){/* jshint -W021 */if(window.console){if(Function.prototype.bind)log=Function.prototype.bind.call(console.log,console);else log=function(){Function.prototype.apply.call(console.log,console,arguments);};log.apply(this,arguments);}} -
bgrins revised this gist
May 5, 2013 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,6 +7,7 @@ // Your code here... log() away /* jshint -W021 */ function log () { if (window.console) { @@ -26,6 +27,7 @@ })(); // All at once (minified version): /* jshint -W021 */ function log(){if(window.console){if(Function.prototype.bind)log=Function.prototype.bind.call(console.log,console);else log=function(){Function.prototype.apply.call(console.log,console,arguments);};log.apply(this,arguments);}} -
bgrins revised this gist
Apr 15, 2013 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ /* Log statements with `log` and `console.log` should appear the same, and show the correct line numbers */ function Test() {} Test.prototype.extraMethod = function() { } Test.methodAttachedToFunction = function(withParam) { } @@ -18,7 +20,8 @@ function logAllWithWrapper() { log(document.body.childNode); } function logAllWithNative() { console.log("A single string"); console.log(123); console.log(["An", "Array", "Of", "Strings"]); console.log("The %s jumped over %d tall buildings", "person", 100); -
bgrins revised this gist
Apr 15, 2013 . 1 changed file with 26 additions and 24 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,33 +2,35 @@ function Test() {} Test.prototype.extraMethod = function() { } Test.methodAttachedToFunction = function(withParam) { } function logAllWithWrapper() { log("A single string"); log(123); log(["An", "Array", "Of", "Strings"]); log("The %s jumped over %d tall buildings", "person", 100); log("The", "person", "jumped over ", 100, " tall buildings"); log("The object %o is inspectable!", { person: { jumpedOver: [100, "tall buildings"]}}); log('%cThis is red text on a green background', 'color:red; background-color:green'); log({ an: "obj", withNested: { objects: { inside: "of", it: true }}}); log(Test, Test.methodAttachedToFunction); log(new Test()); log(document); log(document.body); log(document.body.childNode); } function logAllWithNative() {console.log("A single string"); console.log(123); console.log(["An", "Array", "Of", "Strings"]); console.log("The %s jumped over %d tall buildings", "person", 100); console.log("The", "person", "jumped over ", 100, " tall buildings"); console.log("The object %o is inspectable!", { person: { jumpedOver: [100, "tall buildings"]}}); console.log('%cThis is red text on a green background', 'color:red; background-color:green'); console.log({ an: "obj", withNested: { objects: { inside: "of", it: true }}}); console.log(Test, Test.methodAttachedToFunction); console.log(new Test()); console.log(document); console.log(document.body); console.log(document.body.childNode); } log("\n-----------"); -
bgrins revised this gist
Apr 2, 2013 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,5 @@ })(); /* All at once (minified version): function log(){if(window.console){if(Function.prototype.bind)log=Function.prototype.bind.call(console.log,console);else log=function(){Function.prototype.apply.call(console.log,console,arguments);};log.apply(this,arguments);}} */ -
bgrins revised this gist
Mar 8, 2013 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -24,4 +24,10 @@ } } })(); /* All at once (minified version): function log(){window.console&&(log=Function.prototype.bind?Function.prototype.bind.call(console.log,console):function(){Function.prototype.apply.call(console.log,console,arguments)},log.apply(this,arguments))}; */ -
bgrins renamed this gist
Mar 7, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
bgrins renamed this gist
Mar 7, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
bgrins renamed this gist
Mar 7, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
bgrins renamed this gist
Mar 7, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
bgrins revised this gist
Mar 7, 2013 . 2 changed files with 0 additions and 1 deletion.There are no files selected for viewing
File renamed without changes.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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ // Full version of `log` that: // * Prevents errors on console methods when no console present. // * Exposes a global 'log' function that preserves line numbering and formatting. -
bgrins revised this gist
Mar 7, 2013 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,7 @@ // Full version of `log` that: // * Prevents errors on console methods when no console present. // * Exposes a global 'log' function that preserves line numbering and formatting. (function () { var method; var noop = function () { }; -
bgrins revised this gist
Mar 7, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ There are a lot of ways to do this, but many are lacking. A common problem with This is an attempt to once and for all document the function that I pull in to new projects. There are two different options: * **The full version**: Inspired by the [plugin in HTML5 Boilerplate](https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js). Use this **if you are writing an application and want to create a window.log function**. Additionally, this will set all the `console` methods to an empty function if they don't exist to prevent errors from accidental `console.log` calls left in your code. Put this snippet at the top of the rest of your scripts - it will need to be evaluated first thing to work. * **The portable version**: Use this **if you want to use it inside a plugin** and/or don't want to muck with the global namespace. Just drop it at the bottom of your plugin and log away. `log` can be called before the function declaration, and will not add anything to the window namespace or modify the console objects. View a live demo here: http://jsfiddle.net/bgrins/MZWtG/ -
bgrins revised this gist
Mar 7, 2013 . 3 changed files with 2 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,11 +4,9 @@ Every time I start a new project, I want to pull in a `log` function that allows There are a lot of ways to do this, but many are lacking. A common problem with wrapper functions is that the line number that shows up next to the log is the line number of the log function itself, not where `log` was invoked. There are also times where the arguments get logged in a way that isn't quite the same as the native function. This is an attempt to once and for all document the function that I pull in to new projects. There are two different options: * **The full version**: Inspired by the [plugin in HTML5 Boilerplate](https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js), this will set all the `console` methods to an empty function if they don't exist and then create the handy log function. *Put this snippet at the top of the rest of your scripts*. It will need to be evaluated first thing to work. * **The portable version**: Use this **if you want to use it inside a plugin** and/or don't want to muck with the global namespace. Just drop it at the bottom of your plugin and log away. `log` can be called before the function declaration, and will not add anything to the window namespace or modify the console objects. View a live demo here: http://jsfiddle.net/bgrins/MZWtG/ 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ // Portable version of `log` that: // * Doesn't expose log to the window. // * Allows log() to be called above the function declaration. 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ function Test() {} Test.prototype.extraMethod = function() { } Test.methodAttachedToFunction = function(withParam) { } -
bgrins revised this gist
Mar 7, 2013 . 4 changed files with 31 additions and 28 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ This is an attempt to once and for all document the function that I pull in to n There are two different options * **The full version**: Inspired by the [plugin in HTML5 Boilerplate](https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js), this will set all the `console` methods to an empty function if they don't exist and then create the handy log function. *Put this snippet at the top of the rest of your scripts*. It will need to be evaluated first thing to work. * **The portable version**: Use this *if you want to use it inside a plugin* or don't want to muck with the global namespace. Just drop it at the bottom of your plugin and log away. It can be called before the function declaration, and will not add anything to the window namespace or modify the console objects. View a live demo here: http://jsfiddle.net/bgrins/MZWtG/ 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 charactersOriginal file line number Diff line number Diff line change @@ -1,31 +1,32 @@ // Prevent errors on console methods when no console present and expose a global 'log' function. (function () { var method; var noop = function () { }; var methods = [ 'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn' ]; var length = methods.length; var console = (window.console = window.console || {}); while (length--) { method = methods[length]; // Only stub undefined methods. if (!console[method]) { console[method] = noop; } } if (Function.prototype.bind) { window.log = Function.prototype.bind.call(console.log, console); } else { window.log = function() { Function.prototype.apply.call(console.log, console, arguments); }; } })(); 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ // Portable version of `log` that: // * Doesn't expose log to the window. // * Allows log() to be called above the function declaration. @@ -8,16 +9,16 @@ // Your code here... log() away function log () { if (window.console) { // Only run on the first time through - reset this function to the appropriate console.log helper if (Function.prototype.bind) { log = Function.prototype.bind.call(console.log, console); } else { log = function() { Function.prototype.apply.call(console.log, console, arguments); }; } log.apply(this, arguments); 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ function Test() {} Test.prototype.extraMethod = function() { } Test.methodAttachedToFunction = function(withParam) { } -
bgrins revised this gist
Mar 7, 2013 . 1 changed file with 12 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,14 @@ ## Javascript `log` Function Every time I start a new project, I want to pull in a `log` function that allows the same functionality as the `console.log`, including the full functionality of the [Console API](http://getfirebug.com/wiki/index.php/Console_API#console.log.28object.5B.2C_object.2C_....5D.29). There are a lot of ways to do this, but many are lacking. A common problem with wrapper functions is that the line number that shows up next to the log is the line number of the log function itself, not where `log` was invoked. There are also times where the arguments get logged in a way that isn't quite the same as the native function. This is an attempt to once and for all document the function that I pull in to new projects. There are two different options * *The full version*: Inspired by the [plugin in HTML5 Boilerplate](https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js), this will set all the `console` methods to an empty function if they don't exist and then create the handy log function. *Put this snippet at the top of the rest of your scripts*. It will need to be evaluated first thing to work. * *The portable version*: Use this *if you want to use it inside a plugin* or don't want to muck with the global namespace. Just drop it at the bottom of your plugin and log away. It can be called before the function declaration, and will not add anything to the window namespace or modify the console objects. View a live demo here: http://jsfiddle.net/bgrins/MZWtG/ -
bgrins revised this gist
Mar 7, 2013 . 2 changed files with 0 additions and 1 deletion.There are no files selected for viewing
File renamed without changes.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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ // Portable version of `log` that: // * Doesn't expose log to the window. // * Allows log() to be called above the function declaration. -
bgrins revised this gist
Mar 7, 2013 . 3 changed files with 28 additions and 1 deletion.There are no files selected for viewing
File renamed without changes.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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ // Portable version of `log` that: // * Doesn't expose log to the window. // * Allows log() to be called above the function declaration. // Because of this, you can just throw it in the bottom of a plugin and it won't mess with global scope or clutter your code (function() { // Your code here... log() away function log () { if (window.console) { if (Function.prototype.bind) { log = Function.prototype.bind.call(console.log, console); } else { log = function() { Function.prototype.apply.call(console.log, console, arguments); } } log.apply(this, arguments); } } })(); 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ View a live demo: http://jsfiddle.net/bgrins/MZWtG/ The overriding console code is taken from: https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js -
bgrins renamed this gist
Mar 7, 2013 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ View a live demo: http://jsfiddle.net/bgrins/MZWtG/ The overriding console code is taken from: https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js -
bgrins revised this gist
Mar 7, 2013 . 2 changed files with 15 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ The overriding console code is taken from: https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js Some of the test log statements are taken from here: http://getfirebug.com/wiki/index.php/Console_API#console.log.28object.5B.2C_object.2C_....5D.29 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 charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,21 @@ function Test() {} Test.prototype.extraMethod = function() { } Test.methodAttachedToFunction = function(withParam) { } var expressions = [ ["A single string"], [123], [["An", "Array", "Of", "Strings"]], ["The %s jumped over %d tall buildings", "person", 100], ["The", "person", "jumped over ", 100, " tall buildings"], ["The object %o is inspectable!", { person: { jumpedOver: [100, "tall buildings"]}}], ['%cThis is red text on a green background', 'color:red; background-color:green'], [{ an: "obj", withNested: { objects: { inside: "of", it: true }}}], [Test, Test.methodAttachedToFunction], [new Test()], [document], [document.body], [document.body.childNodes] ]; -
bgrins revised this gist
Mar 7, 2013 . 2 changed files with 1 addition and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -25,7 +25,7 @@ } else { window.log = function() { Function.prototype.apply.call(console.log, console, arguments); } } })(); 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ function Test() {} Test.prototype.extraMethod = function() { } -
bgrins revised this gist
Mar 7, 2013 . 2 changed files with 35 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ // Prevent errors on console methods when no console present and expose a global 'log' function. (function () { var method; 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ function Test() {} Test.prototype.extraMethod = function() { } var expressions = [ ["A single string"], [123], [["An", "Array", "Of", "Strings"]], [{ an: "obj", withNested: { objects: { inside: "of", it: true }}}], [Test], [new Test()] ]; function logAllWithWrapper() { for (var i = 0; i < expressions.length; i++) { log.apply(log, expressions[i]); } } function logAllWithNative() { for (var i = 0; i < expressions.length; i++) { Function.prototype.apply.call(console.log, console, expressions[i]); } } log("\n-----------"); log("Logging all expressions with wrapper log function"); log("-----------\n"); logAllWithWrapper(); log("\n-----------"); log("Logging all expressions with native console.log function"); log("-----------\n"); logAllWithNative(); -
bgrins created this gist
Mar 7, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ // Prevent errors on console methods when no console present and expose a global 'log' function. (function () { var method; var noop = function () { }; var methods = [ 'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn' ]; var length = methods.length; var console = (window.console = window.console || {}); while (length--) { method = methods[length]; // Only stub undefined methods. if (!console[method]) { console[method] = noop; } } if (Function.prototype.bind) { window.log = Function.prototype.bind.call(console.log, console); } else { window.log = function() { (typeof console.log === 'object' ? log.apply.call(console.log, console, arguments) : console.log.apply(console, arguments)); } } })();