Skip to content

Instantly share code, notes, and snippets.

@krisnoble
Last active August 29, 2015 14:15
Show Gist options
  • Save krisnoble/0cd71a603e79ae76e0f2 to your computer and use it in GitHub Desktop.
Save krisnoble/0cd71a603e79ae76e0f2 to your computer and use it in GitHub Desktop.
_debug.js - a simple helper for debugging via console with customizable colours for different categories of message. More info: http://simianstudios.com/blog/post/a-simple-helper-for-javascript-debugging-via-console.log
/*!
* _debug.js (c) Kris Noble 2015 - http://simianstudios.com
* @license MIT
*/
/*
* Simple helper for debugging via console with
* customizable colours for different categories of info.
*
* Usage:
* 1. Define categoryColors e.g.
* <category>: {color: '<text color>', background: '<bg color>'}
* n.b. 'standard' is the default if the category is not provided/defined
* 2. Call debug(message, category) or just debug(message)
*
* Notes:
* - Use debugMode to turn debugging on or off temporarily
* - debugLevel is an array of categories to output, or use 'all' to output everything
* - removing _debug.js from includes will cause errors if you have
* debug() calls in your main code - this is intentional to avoid
* having debug code in production if not desired
*/
// Settings
var debugMode = true,
debugLevel = new Array('all'),
categoryColors = {
standard: {color: 'black', background: 'white'},
example: {color: 'blue', background: 'white'}
};
function debug(message, category) {
// error handling: category not provided or provided but not defined above
category = typeof category !== 'undefined' && typeof categoryColors[category] !== 'undefined' ? category : 'standard';
// check that debugging is on and that the category is selected for output
if(debugMode && (debugLevel.indexOf('all') !== -1 || debugLevel.indexOf(category) !== -1)) {
console.log("%c" + message, "color:" + categoryColors[category].color + "; background:" + categoryColors[category].background + ";");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment