Skip to content

Instantly share code, notes, and snippets.

@codekoala
Last active December 12, 2015 06:59
Show Gist options
  • Save codekoala/4733583 to your computer and use it in GitHub Desktop.
Save codekoala/4733583 to your computer and use it in GitHub Desktop.
Unfinished weechat plugin to automatically log messages to a RethinkDB server.
"""
RethinkLog - Log messages to a rethinkdb instance.
Copyright (C) 2013 Josh VanderLinden <[email protected]>
Date: 2013-02-07
Version: 0.0.2
* 0.0.2 - 2013-02-07: bulk inserts; disconnect from rethinkdb
* 0.0.1 - 2013-02-07: initial plugin
"""
from collections import deque
from rethinkdb import r
from weechat import buffer_get_string as get, config_get_plugin as cfg
import weechat
__name__ = 'RethinkLog'
__author__ = 'codekoala'
__version__ = '0.0.2'
__license__ = 'MIT'
__description__ = 'Log messages to a RethinkDB cluster'
weechat.register(__name__, __author__, __version__, __license__, __description__, 'close', '')
defaults = dict(
host='localhost',
port='28015',
db='weechat',
table='messages',
)
# initialize config
for o, v in defaults.items():
if not cfg(o):
weechat.config_set_plugin(o, v)
# TODO: handle error cases when RethinkDB is inaccessible
# TODO: handle errors inserting records (retries?)
# TODO: configuration changes withour requiring a reload
# TODO: show most recent logs for a given buffer when opened
class RethinkLogger(object):
"""Log messages to a RethinkDB cluster"""
def __init__(self):
self.cnx = None
self.queue = deque()
@property
def r(self):
if self.cnx is None:
self.cnx = r.connect(cfg('host'), int(cfg('port')))
return r.db(cfg('db')).table(cfg('table'))
def log(self, data, buf, time, tags, display, hilight, prefix, msg):
buffer_name = get(buf, 'short_name') or get(buf, 'name')
record = dict(
data=data.strip(),
buffer=buffer_name.strip(),
timestamp=int(time),
tags=[t.strip() for t in tags.split(',') if t],
display=bool(int(display)),
hilight=bool(int(hilight)),
prefix=prefix.strip(),
message=msg.strip().decode('utf-8')
)
self.queue.append(record)
return weechat.WEECHAT_RC_OK
def flush(self, data, remaining_calls):
"""Send multiple records to RethinkDB at one time"""
return_code = weechat.WEECHAT_RC_OK
try:
self.r.insert(self.queue).run()
except Exception as e:
weechat.prnt('', 'Failed to log messages to rethinkdb: %s' % str(e))
return_code = weechat.WEECHAT_RC_ERROR
# TODO: handle errors instead of postentially dropping a ton of records
self.queue.clear()
return return_code
def close(self):
"""Disconnect from the RethinkDB"""
if self.cnx:
self.cnx.close()
plugin = RethinkLogger()
log = plugin.log
flush = plugin.flush
close = plugin.close
weechat.hook_print('', '', '', 1, 'log', '')
weechat.hook_timer(5000, 0, 0, 'flush', '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment