Simple Remote Console, All Devices, All Platforms. Mac/Windows/Linux

I don’t know how many people are like me, but I own a Mac and I hate using it. I do all of my development on Windows or Linux, because of this though the amount of crap I had to go through to debug on my iPhone was horrendous. I didn’t need much, just the console output, so I figured I would create a simple script to do it.

Requirements: NodeJS, Socket.IO 1.4.8

Installation Instructions:

  • Create a new directory.
  • Execute ‘npm init’ command
  • Execute ‘npm install socket.io --save’ command
  • Create index.js

Add the following code to index.js

var io = require('socket.io')(5000);

io.on('connection', function(socket){
    socket.on('error', function(data) {
        if(data.arguments == null) {
            console.error("[ERROR - "+new Date().toLocaleString()+"]: ", data.val);
        } else console.error("[ERROR - "+new Date().toLocaleString()+"]: " + data.val, data.arguments);
    });
    socket.on('log', function(data) {
        if(data.arguments == null) {
            console.log("[LOG - "+new Date().toLocaleString()+"]: ", data.val);
        } else console.log("[LOG - "+new Date().toLocaleString()+"]: " + data.val, data.arguments);
    });

    socket.on('warn', function(data) {
        if(data.arguments == null) {
            console.warn("[WARN - "+new Date().toLocaleString()+"]: ", data.val);
        } else console.warn("[WARN - "+new Date().toLocaleString()+"]: " + data.val, data.arguments);
    });
});

(function () {
  var log = console.log;
  console.log = function (str) {
    log.apply(this, Array.prototype.slice.call(arguments));
  };
}());

(function () {
  var error = console.error;
  console.error = function (str) {
    error.apply(this, Array.prototype.slice.call(arguments));
  };
}());

(function () {
  var warn = console.warn
  console.warn = function (str) {
    warn.apply(this, Array.prototype.slice.call(arguments));
  };
}());

console.log('Listening');

In your applications index file, add this to the top of the <head> but not above it. It should look like this:

<head>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.8/socket.io.js"></script>
      <script>
          var socket = io.connect('http://192.168.1.2:5000');
          (function () {
            var log = console.log;
            console.log = function (str) {
              log.call(this, str);
              log.apply(this, Array.prototype.slice.call(arguments));
              socket.emit('log', { val: str, args: arguments});
            };
          }());
          (function () {
            var error = console.error;
            console.error = function (str) {
              error.call(this, str);
              error.apply(this, Array.prototype.slice.call(arguments));
              socket.emit('error', { val: str, args: arguments});
            };
          }());
          (function () {
            var warn = console.warn;
            console.error = function (str) {
              warn.call(this, str);
              warn.apply(this, Array.prototype.slice.call(arguments));
              socket.emit('warn', { val: str, args: arguments});
            };
          }());
      </script>
      
      ...

You will need to change your IP: var socket = io.connect('http://192.168.1.2:5000'); to be your local IP address (if you’re testing on the network) or your server that you’re hosting the NodeJS server on. Leave the port as 5000 unless you changed it in the NodeJS file.

Here’s what it looks like:

As you can see it logs errors, warnings, etc.

NOTE: TypeError was sent as a warning because it wasn’t detrimental to my application, I have my logging modified so I am notified if anything serious breaks. In normal cases this would be an ERROR

NOTE2: Obviously, to run the NodeJS server you need to execute node index