Last active
June 7, 2018 04:16
-
-
Save jareware/4681398 to your computer and use it in GitHub Desktop.
Revisions
-
jareware revised this gist
Aug 12, 2013 . 1 changed file with 5 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 @@ -23,12 +23,13 @@ define(function() { var ENABLED = true; var TIMESTAMP = true; var topLabelLength = 0; // this is shared across the application so that log lines align return { create: function(label, enabled) { enabled = !!enabled || enabled === undefined; // allow enabling/disabling individual loggers topLabelLength = window.Math.max(topLabelLength, label.length); var boxedLabel = '[' + label + ']'; @@ -41,8 +42,9 @@ define(function() { */ var logger = function() { if (!ENABLED || !enabled || !window.console || !window.console.log) { return; } var now = new Date(); var secPadded = now.getSeconds() > 9 ? now.getSeconds() : '0' + now.getSeconds(); -
jareware created this gist
Jan 31, 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,86 @@ /** * Simple utility for unified logging to the console, with optional timestamping. * * @example // Create several loggers around the application: * var log = logger.create('main'); * var logInAnotherModule = logger.create('anotherModule'); * * // Call the created loggers: * log('Application started'); * logInAnotherModule('Module started'); * * // Produces the following in the console: * // 59.771 [main] Application started logger.js:53 * // 59.775 [anotherModule] Module started * * @author Jarno Rantanen <[email protected]> * @license http://opensource.org/licenses/MIT */ define(function() { "use strict"; var ENABLED = true; var TIMESTAMP = true; var topLabelLength = 0; return { create: function(label) { topLabelLength = window.Math.max(topLabelLength, label.length); var boxedLabel = '[' + label + ']'; /** * Main logging method returned by create(). Writes any arguments directly to window.console. * * @example log('foobar', { baz: 123 }); * // => "[main] foobar Object {baz: 123}" */ var logger = function() { if (!ENABLED || !window.console || !window.console.log) return; var now = new Date(); var secPadded = now.getSeconds() > 9 ? now.getSeconds() : '0' + now.getSeconds(); var ms = now.getMilliseconds(); var msPadded = ms > 99 ? ms : (ms > 9 ? '0' + ms : '00' + ms); var timestamp = TIMESTAMP ? (secPadded + '.' + msPadded + ' ') : ''; var tagPadding = new Array(topLabelLength - label.length + 1).join(' '); var argsToLog = Array.prototype.slice.call(arguments); window.console.log.apply(window.console, [ timestamp + tagPadding + boxedLabel ].concat(argsToLog)); }; /** * Helper for writing out objects. * * @example log.hash({ herp: 123, derp: 456 }); * // => "[main] herp: 123 , derp: 456" */ logger.hash = function(hash) { var pieces = []; Object.keys(hash).forEach(function(key) { pieces.push(key + ':'); pieces.push(hash[key]); pieces.push(','); }); pieces.pop(); logger.apply(logger, pieces); }; return logger; } }; });