Log JavaScript to a Python HTTPServer

In this tutorial, I am going to teach you how to log JavaScript to a Python HTTPServer[1]. For a front-end developer, this becomes very useful in certain environment:

  • when console.log is not working in some legacy browsers;
  • when the development tool is not available in certain browsers, e.g. firebug to Firefox;
  • when working with browser extensions, different feature scope may have their own console, e.g. background.html, window.html and button.html in Google Chrome. Developers have to switch around to look for the right information;
  • when working with a project involves different layers of work, which involves different programming language, a central log platform is extremely useful to debug.
So here we go:

  1. Install Python on your development environment.[2]
  2. Install BaseHTTPServer module.[3]
  3. Create Logger.py in your project root directory. Copy and past the following code:
    #!/usr/bin/env python
    import cgi
    from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    import inspect
    class LoggerHTTPHandler(BaseHTTPRequestHandler) :
      def do_POST(self):
        content_len = int(self.headers.getheader('content-length'))
        print self.rfile.read(content_len);self.send_response(200)
        self.send_header("Content-type", "text/plain")
    def main():
        server = HTTPServer(('', 8000), LoggerHTTPHandler);
        print "Started Logger HTTPServer, using <Ctrl-C> to stop"
      except KeyboardInterrupt:
        print "<Ctrl-C> received, shutting down server"
    if __name__ == '__main__':
  4. Create logger.js in your project src/lib directory. Copy and past the following code:
    var padding = " ", index = 0;
    function paddedString(str, n) {
        return (str + padding).substr(0, n);
    function GC_Logger() {
        console.log("======GC_Logger constructor======");
    GC_Logger.prototype.log = function(module, propertyName, logdata) {
        var time = (new Date()).toLocaleTimeString();
        var message = paddedString(time, 15) + paddedString(index++, 5) + paddedString(module, 40) + paddedString(propertyName, 20) + logdata + "n";
            type : "POST",
            url : "",
            data : message,
            dataType : "text"
  5. Include logger.js file into the html page, which you want to load and log.
    <script type="text/javascript" src="../../lib/jquery.js"></script>
    <script type="text/javascript" src="../../lib/logger.js"></script>
  6. Add your debug code in your JavaScript file. For example, in my window.html file:
    var logger = new GC_Logger();
    logger.log("Window HTML", "Window Init", "Done");
  7. Open a terminal, and run the following code: 
    python Logger.py
    If you are a windows user, please intall Mingw or Cygwin first. [4]
  8. Open the browser, and run your JavaScript code, you will see the log info display on the Python HTTPServer console.


[1] Learn Python Basic HTTP Server, check out BaseHTTPServer.

[2] To get Python, click here.

[3] How to install Python Modules, check out Install Python Modules.

[4] MingW or CygWin, click here.

Yang Zhao

Read more posts by this author.