Tornado websocket logging
Question: I'm trying to implement websockets using Tornado webserver.

My setup looks as follows:

    
    
    from tornado.options import options, define, parse_command_line
    import django.core.handlers.wsgi
    import logging
    import tornado.httpserver
    import tornado.ioloop
    import tornado.web
    import tornado.wsgi
    
    from pogows.tornado_sockets import GetSocketHandler, UpdateSocketHandler
    from mobile.cleaner import start_cleaning
    
    define('port', type=int, default=8080)
    tornado.options.options['log_file_prefix'].set('/var/www/pogo_django/logs/tornado_server.log')
    tornado.options.parse_command_line()
    
    <snip>
    
    def main():
    
        logger = logging.getLogger(__name__)
        wsgi_app = tornado.wsgi.WSGIContainer(
            django.core.handlers.wsgi.WSGIHandler())
        tornado_app = tornado.web.Application(
            [
                ('/hello-tornado', HelloHandler),
                ('/socket/get', GetSocketHandler),
                ('/socket/update', UpdateSocketHandler),
                ('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),
            ], debug=True)
        logger.info("Tornado POGO server starting...")
        server = tornado.httpserver.HTTPServer(tornado_app)
        server.listen(options.port)
        start_cleaning()
        tornado.ioloop.IOLoop.instance().start()
    

So far everything looks fine, tornado logs, I see the info message. Now, I'm
trying to log some stuff from websocket handler classes.

    
    
    class GetSocketHandler(tornado.websocket.WebSocketHandler):
    
        def open(self):
            print "opening"
    
        def on_closed(self):
            print "closing"
    
        def on_message(self, message):
            last_update=datetime.datetime.utcnow().replace(tzinfo=utc)
            try:
                print "getting_user"
        ...
    

Tornado is governed by [supervisord](http://supervisord.org/), with the
following configuration:

> [program:pogo_tornado] command=/var/www/pogo_django/tornado_server.py
> user=www-data stdout_logfile=/var/www/pogo_django/logs/pogo_stdout.log
> stderr_logfile=/var/www/pogo_django/logs/pogo_stderr.log
> environment=PYTHONPATH="/var/www/pogo_django/",DJANGO_SETTINGS_MODULE="pogo.settings"

I tried a few things.

  1. Just use `print` statements, as you see from the above snippet, hoping for supervisord to catch it and send to stdout/stderr logs.
  2. Create a separate `logging.getLogger()` instance inside the websocket class and use that.

None of it produces desired results.

When I run tornado from commandline by hand, I do see the `print` version
printed to console, but `logging` doesn't work anyway.

Where do I go wrong?


Answer: Bah, I got it. I was using `getLogger()` without setting logging level and
just blindly logging to `DEBUG`.

Explicitly using `logger.setLevel(logging.DEBUG)` showed me my messages in the
logs.

Apparently Tornado sets some other level by defaults.. Stupid me.

