Console subscriber
A drop-in tool to subscribe to the console output. Useful for debugging JS scripts on a device where the console is not easily accessible like mobile device browsers, or even in Node environments.
Installation
npm
npm install console-subscriberscript
<script src="https://raw.githubusercontent.com/samsonradu/console-subscriber/63e69cded71aa4c9823d869cb97080b37bc48ce7/node_modules/console-subscriber/index.js"></script>
Usage
let cs = require('console-subscriber'); // window.ConsoleSubscriber object is available in browsers
/**
* Function to be called on console output
* WARNING: calling console.log inside the callback would lead to an infinite recursion
*
* @param string $category info|warn|error|debug
* @param array $args initial arguments of the call
*/
let callback = (category, args) => {
// In a browser env you could write to a DOM element
let message = category + ": " + JSON.stringify(args) + "\n";
document.getElementById('console').innerHTML += message;
// In a Node env you could store the console output (errors)
if (category === "error"){
redisClient.sadd("console:error", JSON.stringify(args));
}
};
// Bind callback fn. Multiple functions can be bound.
cs.bind(callback);
// Unbind a previously bound callback
cs.unbind(callback);
// Restore defaults
cs.unbind();
License
MIT