debian-python-raven/raven/contrib/zope/__init__.py

102 lines
3.9 KiB
Python
Raw Normal View History

2012-06-14 12:15:18 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
raven.contrib.zope
~~~~~~~~~~~~~~~~~~
:copyright: (c) 2010-2013 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import
2012-06-14 12:15:18 +02:00
from inspect import getouterframes, currentframe, getinnerframes
2012-06-14 11:35:37 +02:00
from raven.handlers.logging import SentryHandler
from ZConfig.components.logger.factory import Factory
2012-06-14 12:15:18 +02:00
import logging
2012-06-14 11:35:37 +02:00
from raven.utils.stacks import iter_stack_frames
2012-06-19 19:48:44 +02:00
logger = logging.getLogger(__name__)
2012-06-14 11:35:37 +02:00
class ZopeSentryHandlerFactory(Factory):
2012-06-14 12:15:18 +02:00
2012-06-14 11:35:37 +02:00
def getLevel(self):
2012-06-14 12:15:18 +02:00
return self.section.level
2012-06-14 11:35:37 +02:00
def create(self):
return ZopeSentryHandler(**self.section.__dict__)
def __init__(self, section):
Factory.__init__(self)
self.section = section
2012-06-14 12:15:18 +02:00
2012-06-14 11:35:37 +02:00
class ZopeSentryHandler(SentryHandler):
2012-06-19 19:48:44 +02:00
'''
2012-06-14 12:15:18 +02:00
Zope unfortunately eats the stack trace information.
To get the stack trace information and other useful information
from the request object, this class looks into the different stack
frames when the emit method is invoked.
2012-06-19 19:48:44 +02:00
'''
2012-06-14 12:15:18 +02:00
2012-06-14 11:35:37 +02:00
def __init__(self, *args, **kw):
super(ZopeSentryHandler, self).__init__(*args, **kw)
level = kw.get('level', logging.ERROR)
self.setLevel(level)
def emit(self, record):
if record.levelno <= logging.ERROR:
request = None
exc_info = None
for frame_info in getouterframes(currentframe()):
frame = frame_info[0]
2012-06-19 19:48:44 +02:00
if not request:
request = frame.f_locals.get('request', None)
if not request:
view = frame.f_locals.get('self', None)
try:
request = getattr(view, 'request', None)
except RuntimeError:
request = None
2012-06-19 19:48:44 +02:00
if not exc_info:
exc_info = frame.f_locals.get('exc_info', None)
if not hasattr(exc_info, '__getitem__'):
exc_info = None
2012-06-14 11:35:37 +02:00
if request and exc_info:
break
2012-06-19 19:48:44 +02:00
2012-06-14 11:35:37 +02:00
if exc_info:
record.exc_info = exc_info
2012-06-14 12:15:18 +02:00
record.stack = \
iter_stack_frames(getinnerframes(exc_info[2]))
2012-06-14 11:35:37 +02:00
if request:
try:
body_pos = request.stdin.tell()
request.stdin.seek(0)
body = request.stdin.read()
request.stdin.seek(body_pos)
2012-06-19 19:48:44 +02:00
http = dict(headers=request.environ,
2012-06-14 11:35:37 +02:00
url=request.getURL(),
method=request.method,
2012-06-14 12:15:18 +02:00
host=request.environ.get('REMOTE_ADDR',
''), data=body)
2012-06-19 19:48:44 +02:00
if 'HTTP_USER_AGENT' in http['headers']:
if 'User-Agent' not in http['headers']:
http['headers']['User-Agent'] = \
http['headers']['HTTP_USER_AGENT']
if 'QUERY_STRING' in http['headers']:
http['query_string'] = http['headers']['QUERY_STRING']
setattr(record, 'request', http)
user = request.get('AUTHENTICATED_USER', None)
if user is not None:
2012-10-13 19:04:23 +02:00
user_dict = dict(id=user.getId(),
is_authenticated=user.has_role('Authenticated'),
email=user.getProperty('email') or '')
else:
2012-10-13 19:04:23 +02:00
user_dict = {'is_authenticated': False}
setattr(record, 'user', user_dict)
2012-06-14 11:35:37 +02:00
except (AttributeError, KeyError):
2012-06-21 11:44:31 +02:00
logger.warning('Could not extract data from request', exc_info=True)
2012-06-14 11:35:37 +02:00
return super(ZopeSentryHandler, self).emit(record)