Skip to content
Browse files

Merge pull request #65 from Morgul/master

Improvements to logging
  • Loading branch information...
2 parents 96c787e + 3dc857a commit b4632099a58fc672aaebc55bc3b850da34f87b4e @mrjoes committed Feb 19, 2013
Showing with 51 additions and 29 deletions.
  1. +7 −4 tornadio2/conn.py
  2. +4 −1 tornadio2/periodic.py
  3. +8 −5 tornadio2/persistent.py
  4. +8 −5 tornadio2/polling.py
  5. +5 −1 tornadio2/proto.py
  6. +7 −4 tornadio2/server.py
  7. +12 −9 tornadio2/session.py
View
11 tornadio2/conn.py
@@ -27,6 +27,9 @@
from tornadio2 import proto
+logger = logging.getLogger('tornadio2.conn')
+
+
def event(name_or_func):
"""Event handler decorator.
@@ -194,16 +197,16 @@ def on_event(self, name, args=[], kwargs=dict()):
return handler(self, **kwargs)
except TypeError:
if args:
- logging.error(('Attempted to call event handler %s ' +
+ logger.error(('Attempted to call event handler %s ' +
'with %s arguments.') % (handler,
repr(args)))
else:
- logging.error(('Attempted to call event handler %s ' +
+ logger.error(('Attempted to call event handler %s ' +
'with %s arguments.') % (handler,
repr(kwargs)))
raise
else:
- logging.error('Invalid event name: %s' % name)
+ logger.error('Invalid event name: %s' % name)
def on_close(self):
"""Default on_close handler."""
@@ -295,7 +298,7 @@ def deque_ack(self, msg_id, ack_data):
callback(message, ack_data)
else:
- logging.error('Received invalid msg_id for ACK: %s' % msg_id)
+ logger.error('Received invalid msg_id for ACK: %s' % msg_id)
# Endpoint factory
def get_endpoint(self, endpoint):
View
5 tornadio2/periodic.py
@@ -25,6 +25,9 @@
import logging
+logger = logging.getLogger('tornadio2.periodic')
+
+
class Callback(object):
"""Custom implementation of the Tornado.Callback with support
of callback timeout delays.
@@ -83,7 +86,7 @@ def _run(self):
except (KeyboardInterrupt, SystemExit):
raise
except:
- logging.error("Error in periodic callback", exc_info=True)
+ logger.error("Error in periodic callback", exc_info=True)
if self._running:
self.start(next_call)
View
13 tornadio2/persistent.py
@@ -32,6 +32,9 @@
from tornadio2 import proto
+logger = logging.getLogger('tornadio2.persistent')
+
+
class TornadioWebSocketHandler(WebSocketHandler):
"""Websocket protocol handler"""
@@ -45,7 +48,7 @@ def initialize(self, server):
self._is_active = not self.server.settings['websocket_check']
self._global_heartbeats = self.server.settings['global_heartbeats']
- logging.debug('Initializing %s handler.' % self.name)
+ logger.debug('Initializing %s handler.' % self.name)
# Additional verification of the websocket handshake
# For now it will stay here, till https://github.com/facebook/tornado/pull/415
@@ -142,7 +145,7 @@ def on_message(self, message):
try:
self.session.raw_message(message)
except Exception, ex:
- logging.error('Failed to handle message: ' + traceback.format_exc(ex))
+ logger.error('Failed to handle message: ' + traceback.format_exc(ex))
# Close session on exception
if self.session is not None:
@@ -160,15 +163,15 @@ def send_messages(self, messages):
self.write_message(m)
except IOError:
if self.ws_connection and self.ws_connection.client_terminated:
- logging.debug('Dropping active websocket connection due to IOError.')
+ logger.debug('Dropping active websocket connection due to IOError.')
self._detach()
def session_closed(self):
try:
self.close()
except Exception:
- logging.debug('Exception', exc_info=True)
+ logger.debug('Exception', exc_info=True)
finally:
self._detach()
@@ -177,7 +180,7 @@ def _handle_websocket_exception(self, type, value, traceback):
self.server.io_loop.add_callback(self.on_connection_close)
# raise (type, value, traceback)
- logging.debug('Exception', exc_info=(type, value, traceback))
+ logger.debug('Exception', exc_info=(type, value, traceback))
return True
# Websocket overrides
View
13 tornadio2/polling.py
@@ -29,13 +29,16 @@
from tornadio2 import proto, preflight, stats
+logger = logging.getLogger('tornadio2.polling')
+
+
class TornadioPollingHandlerBase(preflight.PreflightHandler):
"""Polling handler base"""
def initialize(self, server):
self.server = server
self.session = None
- logging.debug('Initializing %s transport.' % self.name)
+ logger.debug('Initializing %s transport.' % self.name)
def _get_session(self, session_id):
"""Get session if exists and checks if session is closed.
@@ -167,7 +170,7 @@ def _polling_timeout(self):
try:
self.send_messages([proto.noop()])
except Exception:
- logging.debug('Exception', exc_info=True)
+ logger.debug('Exception', exc_info=True)
finally:
self._detach()
@@ -199,7 +202,7 @@ def session_closed(self):
try:
self.finish()
except Exception:
- logging.debug('Exception', exc_info=True)
+ logger.debug('Exception', exc_info=True)
finally:
self._detach()
@@ -251,7 +254,7 @@ def session_closed(self):
try:
self.finish()
except Exception:
- logging.debug('Exception', exc_info=True)
+ logger.debug('Exception', exc_info=True)
finally:
self._detach()
@@ -288,7 +291,7 @@ def post(self, session_id):
# IE XDomainRequest support
if not data.startswith('d='):
- logging.error('Malformed JSONP POST request')
+ logger.error('Malformed JSONP POST request')
raise HTTPError(403)
# Grab data
View
6 tornadio2/proto.py
@@ -22,6 +22,10 @@
"""
import logging
+
+logger = logging.getLogger('tornadio2.proto')
+
+
try:
import simplejson as json
json_decimal_args = {"use_decimal": True}
@@ -136,7 +140,7 @@ def event(endpoint, name, message_id, *args, **kwargs):
)
if kwargs:
- logging.error('Can not generate event() with args and kwargs.')
+ logger.error('Can not generate event() with args and kwargs.')
else:
evt = dict(
name=name,
View
11 tornadio2/server.py
@@ -30,6 +30,9 @@
from tornadio2.flashserver import FlashPolicyServer
+logger = logging.getLogger('tornadio2.server')
+
+
class SocketServer(HTTPServer):
"""HTTP Server which does some configuration and automatic setup
of Socket.IO based on configuration.
@@ -82,23 +85,23 @@ def __init__(self, application,
xheaders,
ssl_options)
- logging.info('Starting up tornadio server on port \'%s\'',
+ logger.info('Starting up tornadio server on port \'%s\'',
socket_io_port)
self.listen(socket_io_port, socket_io_address)
if flash_policy_file is not None and flash_policy_port is not None:
try:
- logging.info('Starting Flash policy server on port \'%d\'',
+ logger.info('Starting Flash policy server on port \'%d\'',
flash_policy_port)
FlashPolicyServer(
io_loop=io_loop,
port=flash_policy_port,
policy_file=flash_policy_file)
except Exception, ex:
- logging.error('Failed to start Flash policy server: %s', ex)
+ logger.error('Failed to start Flash policy server: %s', ex)
if auto_start:
- logging.info('Entering IOLoop...')
+ logger.info('Entering IOLoop...')
io_loop.start()
View
21 tornadio2/session.py
@@ -24,6 +24,9 @@
import urlparse
import logging
+
+logger = logging.getLogger('tornadio2.session')
+
from tornado.web import HTTPError
from tornadio2 import sessioncontainer, proto, periodic, stats
@@ -146,7 +149,7 @@ def set_handler(self, handler):
# If IP address don't match - refuse connection
if self.server.settings['verify_remote_ip'] and handler.request.remote_ip != self.remote_ip:
- logging.error('Attempted to attach to session %s (%s) from different IP (%s)' % (
+ logger.error('Attempted to attach to session %s (%s) from different IP (%s)' % (
self.session_id,
self.remote_ip,
handler.request.remote_ip
@@ -183,7 +186,7 @@ def send_message(self, pack):
`pack`
Encoded socket.io message
"""
- logging.debug('<<< ' + pack)
+ logger.debug('<<< ' + pack)
# TODO: Possible optimization if there's on-going connection - there's no
# need to queue messages?
@@ -294,7 +297,7 @@ def connect_endpoint(self, url):
if conn is None:
conn_class = self.conn.get_endpoint(endpoint)
if conn_class is None:
- logging.error('There is no handler for endpoint %s' % endpoint)
+ logger.error('There is no handler for endpoint %s' % endpoint)
return
conn = conn_class(self, endpoint)
@@ -312,7 +315,7 @@ def disconnect_endpoint(self, endpoint):
endpoint name
"""
if endpoint not in self.endpoints:
- logging.error('Invalid endpoint for disconnect %s' % endpoint)
+ logger.error('Invalid endpoint for disconnect %s' % endpoint)
return
conn = self.endpoints[endpoint]
@@ -341,7 +344,7 @@ def raw_message(self, msg):
Raw socket.io message to handle
"""
try:
- logging.debug('>>> ' + msg)
+ logger.debug('>>> ' + msg)
parts = msg.split(':', 3)
if len(parts) == 3:
@@ -362,13 +365,13 @@ def raw_message(self, msg):
self.connect_endpoint(msg_endpoint)
else:
# TODO: Disconnect?
- logging.error('Invalid connect without endpoint')
+ logger.error('Invalid connect without endpoint')
return
# All other packets need endpoints
conn = self.get_connection(msg_endpoint)
if conn is None:
- logging.error('Invalid endpoint: %s' % msg_endpoint)
+ logger.error('Invalid endpoint: %s' % msg_endpoint)
return
if msg_type == proto.HEARTBEAT:
@@ -423,11 +426,11 @@ def raw_message(self, msg):
conn.deque_ack(int(ack_data[0]), data)
elif msg_type == proto.ERROR:
# TODO: Pass it to handler?
- logging.error('Incoming error: %s' % msg_data)
+ logger.error('Incoming error: %s' % msg_data)
elif msg_type == proto.NOOP:
pass
except Exception, ex:
- logging.exception(ex)
+ logger.exception(ex)
# TODO: Add global exception callback?

0 comments on commit b463209

Please sign in to comment.
Something went wrong with that request. Please try again.