diff --git a/pecan/__init__.py b/pecan/__init__.py index 3dd9433..5eedf29 100644 --- a/pecan/__init__.py +++ b/pecan/__init__.py @@ -9,7 +9,7 @@ from middleware.errordocument import ErrorDocumentMiddleware from middleware.recursive import RecursiveMiddleware from middleware.static import StaticFileMiddleware -from configuration import set_config +from configuration import set_config, Config from configuration import _runtime_conf as conf from logging.config import dictConfig as load_logging_config @@ -46,7 +46,8 @@ def make_app(root, static_root=None, logging={}, debug=False, # Pass logging configuration (if it exists) on to the Python logging module if logging: - logging = logging.as_dict() + if isinstance(logging, Config): + logging = logging.as_dict() if 'version' not in logging: logging['version'] = 1 load_logging_config(logging) diff --git a/pecan/tests/test_base.py b/pecan/tests/test_base.py index 452a084..0b5bc88 100644 --- a/pecan/tests/test_base.py +++ b/pecan/tests/test_base.py @@ -1073,6 +1073,68 @@ class TestNonCanonical(unittest.TestCase): assert len(wrapped_apps) == 1 +class TestLogging(unittest.TestCase): + + def test_logging_setup(self): + class RootController(object): + @expose() + def index(self): + import logging + logging.getLogger('pecantesting').info('HELLO WORLD') + return "HELLO WORLD" + + from cStringIO import StringIO + f = StringIO() + + app = TestApp(make_app(RootController(), logging={ + 'loggers': { + 'pecantesting': { + 'level': 'INFO', 'handlers': ['memory'] + } + }, + 'handlers': { + 'memory': { + 'level': 'INFO', + 'class': 'logging.StreamHandler', + 'stream': f + } + } + })) + + app.get('/') + assert f.getvalue() == 'HELLO WORLD\n' + + def test_logging_setup_with_config_obj(self): + class RootController(object): + @expose() + def index(self): + import logging + logging.getLogger('pecantesting').info('HELLO WORLD') + return "HELLO WORLD" + + from cStringIO import StringIO + f = StringIO() + + from pecan.configuration import conf_from_dict + app = TestApp(make_app(RootController(), logging=conf_from_dict({ + 'loggers': { + 'pecantesting': { + 'level': 'INFO', 'handlers': ['memory'] + } + }, + 'handlers': { + 'memory': { + 'level': 'INFO', + 'class': 'logging.StreamHandler', + 'stream': f + } + } + }))) + + app.get('/') + assert f.getvalue() == 'HELLO WORLD\n' + + class TestEngines(unittest.TestCase): template_path = os.path.join(os.path.dirname(__file__), 'templates')