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.
npm install console-subscriber
<script src="node_modules/console-subscriber/index.js"></script>
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();
MIT