Skip to content

Instantly share code, notes, and snippets.

@Lokua
Created September 6, 2014 12:11
Show Gist options
  • Save Lokua/dc030596c4acea0f0b45 to your computer and use it in GitHub Desktop.
Save Lokua/dc030596c4acea0f0b45 to your computer and use it in GitHub Desktop.
Better max logging
/**
* Better max logging.
*
* @see http://compusition.com/writings/js-live-api-basics
* @see http://stackoverflow.com/a/4673436/2416000
*/
autowatch = 1;
Logger = new Global('Logger');
Logger.on = true;
function _formatHelper(m) {
if (m && m.toString) {
var s = m.toString();
return (s.indexOf("[object ") >= 0) ? JSON.stringify(m) : s;
}
return (m === null) ? '<null>' : m;
}
function _format(type, format, args) {
if (!Logger.on) return;
args = Array.prototype.slice.call(args, 1);
var mess = format.replace(/{(\d+)}/g, function(match, number) {
return (typeof args[number] != 'undefined') ?
_formatHelper(args[number]) : match
}) + '\n';
(type === 'log') ? post(mess) : error(mess);
}
Logger.log = function(format) { _format('log', format, arguments); }
Logger.err = function(format) { _format('err', format, arguments); }
Logger.log('\t\tHello from log.js | contact: [email protected]');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment