Last active
December 21, 2015 17:51
-
-
Save ClementNerma/4a0d7558735fc137c02f to your computer and use it in GitHub Desktop.
A micro-library to debug your code without opening the inspector
This file contains 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 characters
'use strict'; | |
var Console = function(parent) { | |
var DOM = document.createElement('div'); | |
DOM.setAttribute('class', 'console'); | |
DOM.setAttribute('style', 'border:1px solid gray;max-height:200px;overflow:auto;'); | |
(parent || document.body).appendChild(DOM); | |
// Here are the logs | |
this.logs = {}; | |
// You can change the displaying colors here | |
this.colors = { | |
error: 'red', | |
info: 'blue', | |
log: 'black', | |
warn: 'yellow' | |
} | |
function _append(cl, message, that) { | |
var msg = document.createElement('div'); | |
msg.setAttribute('class', 'console-log console-' + cl); | |
msg.setAttribute('style', 'padding:5px;border-bottom:1px solid gray;color:' + (that.colors[cl] || 'black') + ';'); | |
msg.innerHTML = message; | |
msg.innerHTML = (msg.textContent || msg.innerText || message).replace(/\r|\n|\r\n/g, '<br/>'); | |
DOM.appendChild(msg); | |
if(!Array.isArray(that.logs[cl])) | |
that.logs[cl] = []; | |
that.logs[cl].push(message); | |
} | |
this.log = function(message) { _append('log' , message, this); }; | |
this.info = function(message) { _append('info' , message, this); }; | |
this.warn = function(message) { _append('warn' , message, this); }; | |
this.error = function(message) { _append('error', message, this); }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A short example :