Skip to content

Instantly share code, notes, and snippets.

@florianboudot
Last active January 3, 2016 21:39
Show Gist options
  • Save florianboudot/8522938 to your computer and use it in GitHub Desktop.
Save florianboudot/8522938 to your computer and use it in GitHub Desktop.
console log with custom colors
var CONSOLE_CSS = 'background:#a4aa0a; color:white; padding:0 4px'; // default
function CustomConsole (prefix, console_css) {
// choose passed arg or default constant
console_css = console_css || CONSOLE_CSS;
function processArgs () {
var args = Array.prototype.slice.call(arguments); // converts Arguments to Array
args.shift(); // remove console method
args.unshift('%c' + prefix, console_css); // adds custom css and prefix at the beginning
return args;
}
function handler (type) {
return console[type].apply(console, processArgs.apply(this, arguments));
}
// export public methods: .info(), .warn(), etc
['log', 'info', 'warn'].forEach(function(type) {
this[type] = handler.bind(handler, type);
}, this);
}
var console_css = 'background:#baff00; color:black; padding:0 4px';
var _console = new CustomConsole('gameManager', console_css);
var truc = 'hello you';
var machin = 'hello machin';
_console.warn('Neo', truc, machin);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment