Skip to content

Instantly share code, notes, and snippets.

@renemonroy
Forked from bgrins/Log-.md
Last active August 29, 2015 14:14
Show Gist options
  • Save renemonroy/d63e332ec620e408add3 to your computer and use it in GitHub Desktop.
Save renemonroy/d63e332ec620e408add3 to your computer and use it in GitHub Desktop.

Revisions

  1. @bgrins bgrins revised this gist May 5, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Log-portable.js
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,8 @@

    // Your code here... log() away

    /* jshint -W021 */
    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):

    /* 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);}}

    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);}}
  2. @bgrins bgrins revised this gist May 5, 2013. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions Log-portable.js
    Original 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):
    // 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);}}
    */
  3. @bgrins bgrins revised this gist Apr 15, 2013. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion Test.js
    Original 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");
    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);
  4. @bgrins bgrins revised this gist Apr 15, 2013. 1 changed file with 26 additions and 24 deletions.
    50 changes: 26 additions & 24 deletions Test.js
    Original file line number Diff line number Diff line change
    @@ -2,33 +2,35 @@ 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]
    ];


    function logAllWithWrapper() {
    for (var i = 0; i < expressions.length; i++) {
    log.apply(log, expressions[i]);
    }
    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() {
    for (var i = 0; i < expressions.length; i++) {
    Function.prototype.apply.call(console.log, console, expressions[i]);
    }
    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-----------");
  5. @bgrins bgrins revised this gist Apr 2, 2013. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions Log-portable.js
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,5 @@
    })();

    /* 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))};
    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);}}
    */
  6. @bgrins bgrins revised this gist Mar 8, 2013. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion Log-portable.js
    Original 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))};
    */
  7. @bgrins bgrins renamed this gist Mar 7, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. @bgrins bgrins renamed this gist Mar 7, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. @bgrins bgrins renamed this gist Mar 7, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. @bgrins bgrins renamed this gist Mar 7, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. @bgrins bgrins revised this gist Mar 7, 2013. 2 changed files with 0 additions and 1 deletion.
    File renamed without changes.
    1 change: 0 additions & 1 deletion Log-full.js
    Original 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.
  12. @bgrins bgrins revised this gist Mar 7, 2013. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion Log-full.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    // Prevent errors on console methods when no console present and expose a global 'log' function.

    // 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 () { };
  13. @bgrins bgrins revised this gist Mar 7, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Description.md
    Original 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), 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 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/
  14. @bgrins bgrins revised this gist Mar 7, 2013. 3 changed files with 2 additions and 6 deletions.
    6 changes: 2 additions & 4 deletions Description.md
    Original 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
    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.
    * **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/
    1 change: 0 additions & 1 deletion Log-portable.js
    Original 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.
    1 change: 0 additions & 1 deletion Test.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    function Test() {}
    Test.prototype.extraMethod = function() { }
    Test.methodAttachedToFunction = function(withParam) { }
  15. @bgrins bgrins revised this gist Mar 7, 2013. 4 changed files with 31 additions and 28 deletions.
    4 changes: 2 additions & 2 deletions Description.md
    Original 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.
    * **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/
    49 changes: 25 additions & 24 deletions Log-full.js
    Original 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 || {});
    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];
    while (length--) {
    method = methods[length];

    // Only stub undefined methods.
    if (!console[method]) {
    console[method] = noop;
    }
    // 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);
    }
    }

    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);
    };
    }
    })();
    5 changes: 3 additions & 2 deletions Log-portable.js
    Original 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);
    1 change: 1 addition & 0 deletions Test.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@

    function Test() {}
    Test.prototype.extraMethod = function() { }
    Test.methodAttachedToFunction = function(withParam) { }
  16. @bgrins bgrins revised this gist Mar 7, 2013. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions Description.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,14 @@
    View a live demo: http://jsfiddle.net/bgrins/MZWtG/
    ## Javascript `log` Function

    The overriding console code is taken from: https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js
    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).

    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
    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/
  17. @bgrins bgrins revised this gist Mar 7, 2013. 2 changed files with 0 additions and 1 deletion.
    File renamed without changes.
    1 change: 0 additions & 1 deletion Log-portable → Log-portable.js
    Original 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.
  18. @bgrins bgrins revised this gist Mar 7, 2013. 3 changed files with 28 additions and 1 deletion.
    File renamed without changes.
    28 changes: 28 additions & 0 deletions Log-portable
    Original 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);
    }
    }

    })();
    1 change: 0 additions & 1 deletion _README.md
    Original 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
  19. @bgrins bgrins renamed this gist Mar 7, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md → _README.md
    Original 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

  20. @bgrins bgrins revised this gist Mar 7, 2013. 2 changed files with 15 additions and 2 deletions.
    5 changes: 5 additions & 0 deletions README.md
    Original 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
    12 changes: 10 additions & 2 deletions Test.js
    Original 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],
    [new Test()]
    [Test, Test.methodAttachedToFunction],
    [new Test()],
    [document],
    [document.body],
    [document.body.childNodes]
    ];


  21. @bgrins bgrins revised this gist Mar 7, 2013. 2 changed files with 1 addition and 2 deletions.
    2 changes: 1 addition & 1 deletion Log.js
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@
    }
    else {
    window.log = function() {
    (typeof console.log === 'object' ? log.apply.call(console.log, console, arguments) : console.log.apply(console, arguments));
    Function.prototype.apply.call(console.log, console, arguments);
    }
    }
    })();
    1 change: 0 additions & 1 deletion Test.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    function Test() {}
    Test.prototype.extraMethod = function() { }

  22. @bgrins bgrins revised this gist Mar 7, 2013. 2 changed files with 35 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Log.js
    Original 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;
    35 changes: 35 additions & 0 deletions Test.js
    Original 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();
  23. @bgrins bgrins created this gist Mar 7, 2013.
    32 changes: 32 additions & 0 deletions Log.js
    Original 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));
    }
    }
    })();