2015-06-18 02:01:18 +00:00
|
|
|
import json
|
2015-06-08 17:20:30 +00:00
|
|
|
import logging
|
2015-08-28 15:43:33 +00:00
|
|
|
import os
|
2015-06-08 17:20:30 +00:00
|
|
|
import unittest
|
2015-06-18 02:01:18 +00:00
|
|
|
import uuid
|
2015-06-08 17:20:30 +00:00
|
|
|
|
2015-09-14 11:27:47 +00:00
|
|
|
from tornado import web, testing
|
2015-06-23 21:48:34 +00:00
|
|
|
|
2015-06-08 17:20:30 +00:00
|
|
|
import sprockets.logging
|
|
|
|
|
|
|
|
|
2015-09-14 11:27:47 +00:00
|
|
|
def setup_module():
|
|
|
|
os.environ.setdefault('ENVIRONMENT', 'development')
|
2015-06-08 17:20:30 +00:00
|
|
|
|
|
|
|
|
2015-09-14 11:27:47 +00:00
|
|
|
class SimpleHandler(web.RequestHandler):
|
2015-06-08 17:20:30 +00:00
|
|
|
|
2015-09-14 11:27:47 +00:00
|
|
|
def get(self):
|
|
|
|
if self.get_query_argument('runtime_error', default=None):
|
|
|
|
raise RuntimeError(self.get_query_argument('runtime_error'))
|
|
|
|
if self.get_query_argument('status_code', default=None) is not None:
|
|
|
|
self.set_status(int(self.get_query_argument('status_code')))
|
|
|
|
else:
|
|
|
|
self.set_status(204)
|
2015-06-08 17:20:30 +00:00
|
|
|
|
|
|
|
|
2015-09-14 11:27:47 +00:00
|
|
|
class RecordingHandler(logging.Handler):
|
2015-06-18 02:01:18 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2015-09-14 11:27:47 +00:00
|
|
|
super(RecordingHandler, self).__init__()
|
|
|
|
self.emitted = []
|
2015-06-18 02:01:18 +00:00
|
|
|
|
2015-09-14 11:27:47 +00:00
|
|
|
def emit(self, record):
|
|
|
|
self.emitted.append((record, self.format(record)))
|
2015-06-18 02:01:18 +00:00
|
|
|
|
|
|
|
|
2015-09-14 11:27:47 +00:00
|
|
|
class TornadoLoggingTestMixin(object):
|
2015-06-18 02:01:18 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2015-09-14 11:27:47 +00:00
|
|
|
super(TornadoLoggingTestMixin, self).setUp()
|
|
|
|
self.access_log = logging.getLogger('tornado.access')
|
|
|
|
self.app_log = logging.getLogger('tornado.application')
|
|
|
|
self.gen_log = logging.getLogger('tornado.general')
|
|
|
|
for logger in (self.access_log, self.app_log, self.gen_log):
|
|
|
|
logger.disabled = False
|
2015-08-28 21:11:54 +00:00
|
|
|
|
|
|
|
self.recorder = RecordingHandler()
|
2015-09-14 11:27:47 +00:00
|
|
|
root_logger = logging.getLogger()
|
|
|
|
root_logger.addHandler(self.recorder)
|
2015-08-28 21:11:54 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2015-09-14 11:27:47 +00:00
|
|
|
super(TornadoLoggingTestMixin, self).tearDown()
|
|
|
|
logging.getLogger().removeHandler(self.recorder)
|
2015-08-28 21:11:54 +00:00
|
|
|
|
|
|
|
|
2015-09-14 11:27:47 +00:00
|
|
|
class TornadoLogFunctionTests(TornadoLoggingTestMixin,
|
|
|
|
testing.AsyncHTTPTestCase):
|
2015-08-28 21:11:54 +00:00
|
|
|
|
2015-09-14 11:27:47 +00:00
|
|
|
def get_app(self):
|
|
|
|
return web.Application(
|
|
|
|
[web.url('/', SimpleHandler)],
|
|
|
|
log_function=sprockets.logging.tornado_log_function)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def access_record(self):
|
|
|
|
for record, _ in self.recorder.emitted:
|
|
|
|
if record.name == 'tornado.access':
|
|
|
|
return record
|
|
|
|
|
|
|
|
def test_that_redirect_logged_as_info(self):
|
|
|
|
self.fetch('?status_code=303')
|
|
|
|
self.assertEqual(self.access_record.levelno, logging.INFO)
|
|
|
|
|
|
|
|
def test_that_client_error_logged_as_warning(self):
|
|
|
|
self.fetch('?status_code=400')
|
|
|
|
self.assertEqual(self.access_record.levelno, logging.WARNING)
|
|
|
|
|
|
|
|
def test_that_exception_is_logged_as_error(self):
|
|
|
|
self.fetch('/?runtime_error=something%20bad%20happened')
|
|
|
|
self.assertEqual(self.access_record.levelno, logging.ERROR)
|
|
|
|
|
|
|
|
def test_that_log_includes_correlation_id(self):
|
|
|
|
self.fetch('/?runtime_error=something%20bad%20happened')
|
|
|
|
self.assertIn('correlation_id', self.access_record.args)
|
|
|
|
|
|
|
|
def test_that_log_includes_duration(self):
|
|
|
|
self.fetch('/?runtime_error=something%20bad%20happened')
|
|
|
|
self.assertIn('duration', self.access_record.args)
|
|
|
|
|
|
|
|
def test_that_log_includes_headers(self):
|
|
|
|
self.fetch('/?runtime_error=something%20bad%20happened')
|
|
|
|
self.assertIn('headers', self.access_record.args)
|
|
|
|
|
|
|
|
def test_that_log_includes_method(self):
|
|
|
|
self.fetch('/?runtime_error=something%20bad%20happened')
|
|
|
|
self.assertEqual(self.access_record.args['method'], 'GET')
|
|
|
|
|
|
|
|
def test_that_log_includess_path(self):
|
|
|
|
self.fetch('/?runtime_error=something%20bad%20happened')
|
|
|
|
self.assertEqual(self.access_record.args['path'], '/')
|
|
|
|
|
|
|
|
def test_that_log_includes_protocol(self):
|
|
|
|
self.fetch('/?runtime_error=something%20bad%20happened')
|
|
|
|
self.assertEqual(self.access_record.args['protocol'], 'http')
|
|
|
|
|
|
|
|
def test_that_log_includes_query_arguments(self):
|
|
|
|
self.fetch('/?runtime_error=something%20bad%20happened')
|
|
|
|
self.assertEqual(self.access_record.args['query_args'],
|
|
|
|
{'runtime_error': ['something bad happened']})
|
|
|
|
|
|
|
|
def test_that_log_includes_remote_ip(self):
|
|
|
|
self.fetch('/?runtime_error=something%20bad%20happened')
|
|
|
|
self.assertIn('remote_ip', self.access_record.args)
|
|
|
|
|
|
|
|
def test_that_log_includes_status_code(self):
|
|
|
|
self.fetch('/?runtime_error=something%20bad%20happened')
|
|
|
|
self.assertEqual(self.access_record.args['status_code'], 500)
|
|
|
|
|
|
|
|
def test_that_log_includes_environment(self):
|
|
|
|
self.fetch('/?runtime_error=something%20bad%20happened')
|
|
|
|
self.assertEqual(self.access_record.args['environment'],
|
|
|
|
os.environ['ENVIRONMENT'])
|
|
|
|
|
|
|
|
def test_that_log_includes_correlation_id_from_header(self):
|
|
|
|
cid = str(uuid.uuid4())
|
|
|
|
self.fetch('/?runtime_error=something%20bad%20happened',
|
|
|
|
headers={'Correlation-ID': cid})
|
|
|
|
self.assertEqual(self.access_record.args['correlation_id'], cid)
|