Permalink
Browse files

Merge branch 'HH-25681'

  • Loading branch information...
2 parents 194f62a + 3f39647 commit a1a598719dacb182740dd70919513bde3eea79e5 @katraev katraev committed May 3, 2012
Showing with 49 additions and 33 deletions.
  1. +4 −1 debian/changelog
  2. +1 −0 debian/control
  3. +17 −25 frontik/handler.py
  4. +4 −0 frontik/options.py
  5. +23 −6 frontik/server.py
  6. +0 −1 tests/test_integration_non_debug_mode.py
View
@@ -10,7 +10,10 @@ frontik (2.12.0~dev) unstable; urgency=low
[ Mikhail Sabourenkov ]
* log size of received data
- -- Mikhail Sabourenkov <m.saburenkov@hh.ru> Thu, 22 Mar 2012 16:20:23 +0400
+ [ Pavel Trukhanov ]
+ * graypy log impl
+
+ -- Pavel Trukhanov <p.trukhanov@hh.ru> Thu, 29 Mar 2012 13:56:28 +0400
frontik (2.11.0) unstable; urgency=low
View
@@ -18,4 +18,5 @@ Depends: ${shlibs:Depends}, ${misc:Depends},
python-tornado (>= 2.0~hh7),
python-tornado-util (>= 0.5.1),
python,
+Suggests: python-graypy,
Description: frontik - the python based XScript alternative
View
@@ -84,27 +84,21 @@ def next_request_id(self):
stats = Stats()
-class PageLogger(logging.Logger):
- '''
- This class is supposed to fix huge memory 'leak' in logging
- module. I.e. every call to logging.getLogger(some_unique_name)
- wastes memory as resulting logger is memoized by
- module. PageHandler used to create unique logger on each request
- by call logging.getLogger('frontik.handler.%s' %
- (self.request_id,)). This lead to wasting about 10Mb per 10K
- requests.
- '''
-
- def __init__(self, name, page, zero_time):
- logging.Logger.__init__(self, 'frontik.handler.{0}'.format(name))
- self.page = page
+class ContextFilter(logging.Filter):
+ def filter(self, record):
+ record.name = '.'.join(filter(None, [record.name, getattr(record, 'request_id', None)]))
+ return True
+log.addFilter(ContextFilter())
+
+class PageLogger(logging.LoggerAdapter):
+ def __init__(self, logger_name, page, handler_name, zero_time):
+ logging.LoggerAdapter.__init__(self, log, dict(request_id = logger_name, page = page, handler = handler_name))
self._time = zero_time
self.stages = []
-
-
- def handle(self, record):
- logging.Logger.handle(self, record)
- log.handle(record)
+ self.page = page
+ #backcompatibility with logger
+ self.warn = self.warning
+ self.addHandler = self.logger.addHandler
def stage_tag(self, stage):
self._stage_tag(stage, (time.time() - self._time) * 1000)
@@ -145,12 +139,10 @@ def __init__(self, application, request, ph_globals=None, **kwargs):
raise Exception("%s need to have ph_globals" % PageHandler)
self.name = self.__class__.__name__
- self.request_id = request.headers.get('X-Request-Id', stats.next_request_id())
- if hasattr(ph_globals.config, 'app_name') and ph_globals.config.app_name:
- handler_name = '{0}.{1}'.format(ph_globals.config.app_name, self.request_id)
- else:
- handler_name = self.request_id
- self.log = PageLogger(handler_name, request.path or request.uri, self.handler_started,)
+ self.request_id = request.headers.get('X-Request-Id', str(stats.next_request_id()))
+ logger_name = '.'.join(filter(None, [self.request_id, getattr(ph_globals.config, 'app_name', None)]))
+ self.log = PageLogger(logger_name, request.path or request.uri, self.__module__, self.handler_started)
+
tornado.web.RequestHandler.__init__(self, application, request, logger = self.log, **kwargs)
self.ph_globals = ph_globals
View
@@ -19,3 +19,7 @@
tornado.options.define('timeout_multiplier', 1.0, float)
tornado.options.define('executor_pool_size', 1, int)
+
+tornado.options.define('graylog', False, bool)
+tornado.options.define('graylog_host', "localhost", str)
+tornado.options.define('graylog_port', 12201, int)
View
@@ -14,21 +14,38 @@
log = logging.getLogger("frontik.server")
-def main(config_file="/etc/frontik/frontik.cfg"):
- tornado_util.server.bootstrap(config_file=config_file)
-
+def bootstrap_all_logging():
if tornado.options.options.syslog:
- syslog_handler=MaxLenSysLogHandler(
- facility=MaxLenSysLogHandler.facility_names[tornado.options.options.syslog_facility],
+ syslog_handler = MaxLenSysLogHandler(
+ facility=MaxLenSysLogHandler.facility_names[
+ tornado.options.options.syslog_facility],
address=tornado.options.options.syslog_address,
msg_max_length=tornado.options.options.syslog_msg_max_length)
syslog_handler.setFormatter(
- logging.Formatter("[%(process)s] %(asctime)s %(levelname)s %(name)s: %(message)s"))
+ logging.Formatter(
+ "[%(process)s] %(asctime)s %(levelname)s %(name)s: %(message)s"))
logging.getLogger().addHandler(syslog_handler)
+
+ if tornado.options.options.graylog:
+ try:
+ from graypy import GELFHandler, WAN_CHUNK
+ graylog_handler = GELFHandler(tornado.options.options.graylog_host,
+ tornado.options.options.graylog_port, WAN_CHUNK, False)
+
+ logging.getLogger().addHandler(graylog_handler)
+ except ImportError:
+ log.error('Graylog option is on, but can not import graypy and start graylog logging!')
+
for log_channel_name in options.suppressed_loggers:
logging.getLogger(log_channel_name).setLevel(logging.WARN)
+
+def main(config_file="/etc/frontik/frontik.cfg"):
+ tornado_util.server.bootstrap(config_file=config_file)
+
+ bootstrap_all_logging()
+
try:
app = frontik.app.get_app(options.urls, options.apps)
except:
@@ -32,7 +32,6 @@ def test_basic_auth_fail_on_wrong_pass():
req.add_header('Authorization', 'Basic {0}'.format(base64.encodestring('user:bad')))
try:
res = urllib2.urlopen(req)
- #res = opener.open(page_url)
assert(res.getcode() == 401)
except urllib2.HTTPError, e:
assert(e.code == 401)

0 comments on commit a1a5987

Please sign in to comment.