add facility to override PrintingListener output with logging instead of print statements
"""
import logging
+import os
import sys
import threading
import time
@@ -12,6 +13,13 @@ import stomp.utils as utils
from stomp.constants import *
+def print_out(msg, *args):
+ if os.environ.get('STOMP_PRINT_TO_LOG'):
+ logging.info(msg, *args)
+ else:
+ print(msg % args)
+
+
class Publisher(object):
"""
Simply a registry of listeners.
@@ -449,27 +457,27 @@ class PrintingListener(ConnectionListener):
"""
:param (str,int) host_and_port:
"""
- print('on_connecting %s %s' % host_and_port)
+ print_out('on_connecting %s %s', *host_and_port)
def on_connected(self, headers, body):
"""
:param dict headers:
:param body:
"""
- print('on_connected %s %s' % (headers, body))
+ print_out('on_connected %s %s', headers, body)
def on_disconnected(self):
- print('on_disconnected')
+ print_out('on_disconnected')
def on_heartbeat_timeout(self):
- print('on_heartbeat_timeout')
+ print_out('on_heartbeat_timeout')
def on_before_message(self, headers, body):
"""
:param dict headers:
:param body:
"""
- print('on_before_message %s %s' % (headers, body))
+ print_out('on_before_message %s %s', headers, body)
return headers, body
def on_message(self, headers, body):
@@ -477,30 +485,30 @@ class PrintingListener(ConnectionListener):
:param dict headers:
:param body:
"""
- print('on_message %s %s' % (headers, body))
+ print_out('on_message %s %s', headers, body)
def on_receipt(self, headers, body):
"""
:param dict headers:
:param body:
"""
- print('on_receipt %s %s' % (headers, body))
+ print_out('on_receipt %s %s', headers, body)
def on_error(self, headers, body):
"""
:param dict headers:
:param body:
"""
- print('on_error %s %s' % (headers, body))
+ print_out('on_error %s %s', headers, body)
def on_send(self, frame):
"""
:param Frame frame:
"""
- print('on_send %s %s %s' % (frame.cmd, utils.clean_headers(frame.headers), frame.body))
+ print_out('on_send %s %s %s', frame.cmd, utils.clean_headers(frame.headers), frame.body)
def on_heartbeat(self):
- print('on_heartbeat')
+ print_out('on_heartbeat')
class TestListener(StatsListener, WaitingListener, PrintingListener):
