diff --git a/README.md b/README.md index 8121d10..e4ef8ca 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,23 @@ keys: msg, result. For successful calls, result will be "success" and msg will be the empty string. On error, result will be "error" and msg will describe what went wrong. +#### Logging +The Zulip API comes with a ZulipStream class which can be used with the +logging module: + +``` +import zulip +import logging +stream = zulip.ZulipStream(type="stream", to=["support"], subject="your subject") +logger = logging.getLogger("your_logger") +logger.addHandler(logging.StreamHandler(stream)) +logger.setLevel(logging.DEBUG) +logger.info("This is an INFO test.") +logger.debug("This is a DEBUG test.") +logger.warn("This is a WARN test.") +logger.error("This is a ERROR test.") +``` + #### Sending messages You can use the included `zulip-send` script to send messages via the diff --git a/zulip/__init__.py b/zulip/__init__.py index c801328..b2d5f0d 100644 --- a/zulip/__init__.py +++ b/zulip/__init__.py @@ -31,6 +31,7 @@ import optparse from distutils.version import LooseVersion from ConfigParser import SafeConfigParser +import logging __version__ = "0.2.1" @@ -293,6 +294,27 @@ def _mk_events(event_types=None): def _kwargs_to_dict(**kwargs): return kwargs +class ZulipStream(object): + """ + A Zulip stream-like object + """ + + def __init__(self, type, to, subject, **kwargs): + self.client = Client(**kwargs) + self.type = type + self.to = to + self.subject = subject + + def write(self, content): + message = {"type": self.type, + "to": self.to, + "subject": self.subject, + "content": content} + self.client.send_message(message) + + def flush(self): + pass + Client._register('send_message', url='messages', make_request=(lambda request: request)) Client._register('update_message', method='PATCH', url='messages', make_request=(lambda request: request)) Client._register('get_messages', method='GET', url='messages/latest', longpolling=True)