Skip to content

Instantly share code, notes, and snippets.

@slav0nic
Last active October 3, 2016 19:24
Show Gist options
  • Save slav0nic/4c3650591e3edc6ee1d8 to your computer and use it in GitHub Desktop.
Save slav0nic/4c3650591e3edc6ee1d8 to your computer and use it in GitHub Desktop.
Opbeat logger for twisted
from opbeat import Client
from twisted.logger import ILogObserver
from zope.interface import implementer
@implementer(ILogObserver)
class OpbeatLogObserver(object):
"""
A twisted log observer which logs errors to Opbeat service.
"""
def __init__(self, dsn, client=None):
if client is None:
client = Client
self.client = client(async_mode=True, **dsn)
def __call__(self, event):
failure = event.get('log_failure')
if failure:
self.client.capture_exception(
(failure.type, failure.value, failure.getTracebackObject()),
extra=event)
#### Example
from twisted.logger import Logger
opbeat_logobserver = OpbeatLogObserver(
dict(organization_id='...',
app_id='...',
secret_token='...'
)
)
log = Logger(observer=opbeat_logobserver)
try:
1 / 0
except:
log.failure("Math is hard!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment