Add support for request logging

This commit is contained in:
Yoann Roman
2011-04-20 15:02:13 -04:00
parent 1581c005e2
commit c992823db3
4 changed files with 144 additions and 9 deletions

View File

@@ -4,6 +4,7 @@
from paste.cascade import Cascade
from paste.errordocument import make_errordocument
from paste.recursive import RecursiveMiddleware
from paste.translogger import TransLogger
from paste.urlparser import StaticURLParser
from weberror.errormiddleware import ErrorMiddleware
from weberror.evalexception import EvalException
@@ -19,7 +20,7 @@ __all__ = [
'make_app', 'Pecan', 'request', 'response', 'override_template', 'expose', 'conf', 'set_config'
]
def make_app(root, static_root=None, debug=False, errorcfg={}, wrap_app=None, **kw):
def make_app(root, static_root=None, debug=False, errorcfg={}, wrap_app=None, logging=False, **kw):
'''
'''
@@ -35,4 +36,6 @@ def make_app(root, static_root=None, debug=False, errorcfg={}, wrap_app=None, **
app = make_errordocument(app, conf, **conf.app.errors)
if static_root:
app = Cascade([StaticURLParser(static_root), app])
if isinstance(logging, dict) or logging == True:
app = TransLogger(app, **(isinstance(logging, dict) and logging or {}))
return app

View File

@@ -8,7 +8,8 @@ def setup_app(config):
return make_app(
config.app.root,
static_root = config.app.static_root,
template_path = config.app.template_path,
debug = config.app.debug,
logging = config.app.logging,
template_path = config.app.template_path,
force_canonical = config.app.force_canonical
)

View File

@@ -10,14 +10,15 @@ server = {
# Pecan Application Configurations
app = {
'root' : RootController(),
'modules' : [${package}],
'static_root' : '%(confdir)s/public',
'root' : RootController(),
'modules' : [${package}],
'static_root' : '%(confdir)s/public',
'template_path' : '%(confdir)s/${package}/templates',
'reload': True,
'debug' : True,
'errors' : {
'404' : '/error/404',
'reload' : True,
'debug' : True,
'logging' : False,
'errors' : {
'404' : '/error/404',
'__force_dict__' : True
}
}

View File

@@ -1,5 +1,6 @@
from formencode import Schema, validators
from paste.recursive import ForwardRequestException
from paste.translogger import TransLogger
from unittest import TestCase
from webtest import TestApp
@@ -665,6 +666,135 @@ class TestBase(TestCase):
assert r.status_int == 200
class TestLogging(TestCase):
"""
Mocks logging calls so we can make sure they get called. We could use
Fudge for this, but it would add an additional dependency to Pecan for
a single set of tests.
"""
def setUp(self):
self._write_log = TransLogger.write_log
def tearDown(self):
TransLogger.write_log = self._write_log
def test_default(self):
class RootController(object):
@expose()
def index(self):
return '/'
# monkeypatch the logger
writes = []
def _write_log(self, *args, **kwargs):
writes.append(1)
TransLogger.write_log = _write_log
# check the request
app = TestApp(make_app(RootController(), debug=True))
r = app.get('/')
assert r.status_int == 200
assert writes == []
def test_default(self):
class RootController(object):
@expose()
def index(self):
return '/'
# monkeypatch the logger
writes = []
def _write_log(self, *args, **kwargs):
writes.append(1)
TransLogger.write_log = _write_log
# check the request
app = TestApp(make_app(RootController(), debug=True))
r = app.get('/')
assert r.status_int == 200
assert len(writes) == 0
def test_no_logging(self):
class RootController(object):
@expose()
def index(self):
return '/'
# monkeypatch the logger
writes = []
def _write_log(self, *args, **kwargs):
writes.append(1)
TransLogger.write_log = _write_log
# check the request
app = TestApp(make_app(RootController(), debug=True, logging=False))
r = app.get('/')
assert r.status_int == 200
assert len(writes) == 0
def test_basic_logging(self):
class RootController(object):
@expose()
def index(self):
return '/'
# monkeypatch the logger
writes = []
def _write_log(self, *args, **kwargs):
writes.append(1)
TransLogger.write_log = _write_log
# check the request
app = TestApp(make_app(RootController(), debug=True, logging=True))
r = app.get('/')
assert r.status_int == 200
assert len(writes) == 1
def test_empty_config(self):
class RootController(object):
@expose()
def index(self):
return '/'
# monkeypatch the logger
writes = []
def _write_log(self, *args, **kwargs):
writes.append(1)
TransLogger.write_log = _write_log
# check the request
app = TestApp(make_app(RootController(), debug=True, logging={}))
r = app.get('/')
assert r.status_int == 200
assert len(writes) == 1
def test_custom_config(self):
class RootController(object):
@expose()
def index(self):
return '/'
# create a custom logger
writes = []
class FakeLogger(object):
def log(self, *args, **kwargs):
writes.append(1)
# check the request
app = TestApp(make_app(RootController(), debug=True,
logging={'logger': FakeLogger()}))
r = app.get('/')
assert r.status_int == 200
assert len(writes) == 1
class TestEngines(object):
template_path = os.path.join(os.path.dirname(__file__), 'templates')