Skip to content

Commit 0dc1e45

Browse files
committed
Added a TYPE arg to message logging.
1 parent 22f6c81 commit 0dc1e45

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

django_socketio/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
PORT = getattr(settings, "SOCKETIO_PORT", 9000)
66
MESSAGE_LOG_FORMAT = getattr(settings, "SOCKETIO_MESSAGE_LOG_FORMAT",
77
'%(REMOTE_ADDR)s - - [%(TIME)s] '
8-
'"Socket.IO message: %(MESSAGE)s"')
8+
'"Socket.IO %(TYPE)s: %(MESSAGE)s"')

django_socketio/views.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ def cleanup():
2424
for client in CLIENTS.values():
2525
events.on_finish.send(*client)
2626

27-
def format_log(request, message):
27+
def format_log(request, message_type, message):
2828
"""
2929
Formats a log message similar to gevent's pywsgi request logging.
3030
"""
3131
now = datetime.now().replace(microsecond=0)
32-
log = MESSAGE_LOG_FORMAT % dict(request.META, MESSAGE=message, TIME=now)
33-
return log + "\n"
32+
args = dict(request.META, TYPE=message_type, MESSAGE=message, TIME=now)
33+
return (MESSAGE_LOG_FORMAT % args) + "\n"
3434

3535
def socketio(request):
3636
"""
@@ -58,26 +58,27 @@ def socketio(request):
5858
messages = iter(messages)
5959
for message in messages:
6060
if message == "__subscribe__":
61-
chan = messages.next()
62-
socket.subscribe(chan)
63-
events.on_subscribe.send(request, socket, context, chan)
64-
message = "[subscribe] %s" % chan
61+
message = messages.next()
62+
message_type = "subscribe"
63+
socket.subscribe(message)
64+
events.on_subscribe.send(request, socket, context, message)
6565
elif message == "__unsubscribe__":
66-
chan = messages.next()
67-
socket.unsubscribe(chan)
68-
events.on_unsubscribe.send(request, socket, context, chan)
69-
message = "[unsubscribe] %s" % chan
66+
message = messages.next()
67+
message_type = "unsubscribe"
68+
socket.unsubscribe(message)
69+
events.on_unsubscribe.send(request, socket, context, message)
7070
else:
71-
# Socket.IO transfers arrays as individual messages, so
71+
# Socket.IO sends arrays as individual messages, so
7272
# they're put into an object in socketio_scripts.html
73-
# and given the __array__ key so that they can be treated
74-
# consistently here.
73+
# and given the __array__ key so that they can be
74+
# handled consistently in the on_message event.
75+
message_type = "message"
7576
if message == "__array__":
7677
message = messages.next()
7778
events.on_message.send(request, socket, context, message)
7879
if MESSAGE_LOG_FORMAT is not None:
79-
formatted = format_log(request, message)
80-
socket.handler.server.log.write(formatted)
80+
log_message = format_log(request, message_type, message)
81+
socket.handler.server.log.write(log_message)
8182
except Exception, exception:
8283
print_exc()
8384
events.on_error.send(request, socket, context, exception)

0 commit comments

Comments
 (0)