Last active
August 29, 2015 13:58
-
-
Save jtallieu/10398252 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example MQLogHandler to emit log messages to an IronIO Queue | |
""" | |
import sys | |
import json | |
import logging | |
from iron_mq import * | |
logging.basicConfig(level=logging.INFO, | |
stream=sys.stdout, | |
format='%(asctime)s %(levelname)-8s[%(name)s] %(message)s', | |
datefmt='%m/%d %H:%M:%S') | |
class MQHandler(logging.StreamHandler): | |
def __init__(self, queue, token, project): | |
super(MQHandler, self).__init__() | |
self.active = False | |
try: | |
self.mq = IronMQ(project_id = project, token=token) | |
self.queue = self.mq.queue(queue) | |
self.active = True | |
except: | |
log.error("Unable to connect to queue") | |
def emit(self, record): | |
if self.active: | |
try: | |
m = {} | |
m["type"] = "log" | |
m["message"] = record.msg | |
_m = json.dumps(m) | |
self.queue.post(_m) | |
except: | |
pass | |
if __name__ == "__main__": | |
log = logging.getLogger("main") | |
log.info("Starting") | |
handler = MQHandler("MyQueue", "MyToken", "MyProject") | |
log.addHandler(hhandler) | |
log.info("Complete") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment