From 596495de4aa1ab0f3a3752d21c14ac08631e8457 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sun, 27 Nov 2011 00:42:14 -0500 Subject: - Added ``setup_logging`` API function to the ``pyramid.paster`` module. This function sets up Python logging according to the logging configuration in a PasteDeploy ini file. --- CHANGES.txt | 10 +++++++--- docs/api/paster.rst | 1 + pyramid/paster.py | 24 +++++++++++++++++++++++- pyramid/scripts/pserve.py | 4 ++-- pyramid/scripts/pshell.py | 4 ++-- pyramid/tests/test_paster.py | 29 ++++++++++++++++++++++++++++- 6 files changed, 63 insertions(+), 9 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 7060492f1..efc0dbb64 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -17,9 +17,13 @@ Features - ``bpython`` interpreter compatibility in ``pshell``. See the "Command-Line Pyramid" narrative docs chapter for more information. -- Added ``get_appsettings`` function to ``pyramid.paster`` API. This function - returns the settings defined within an ``[app:...]`` section in a - PasteDeploy ini file. +- Added ``get_appsettings`` API function to the ``pyramid.paster`` module. + This function returns the settings defined within an ``[app:...]`` section + in a PasteDeploy ini file. + +- Added ``setup_logging`` API function to the ``pyramid.paster`` module. + This function sets up Python logging according to the logging configuration + in a PasteDeploy ini file. Bug Fixes --------- diff --git a/docs/api/paster.rst b/docs/api/paster.rst index 5cf8bbe2c..3f7a1c364 100644 --- a/docs/api/paster.rst +++ b/docs/api/paster.rst @@ -11,3 +11,4 @@ .. autofunction:: get_appsettings(config_uri, name=None) + .. autofunction:: setup_logging(config_uri) diff --git a/pyramid/paster.py b/pyramid/paster.py index 2abb3876c..cee437ec4 100644 --- a/pyramid/paster.py +++ b/pyramid/paster.py @@ -7,9 +7,11 @@ from paste.deploy import ( appconfig, ) +from pyramid.compat import configparser +from logging.config import fileConfig from pyramid.scripting import prepare - from pyramid.scaffolds import PyramidTemplate # bw compat + PyramidTemplate = PyramidTemplate # pyflakes zope.deprecation.deprecated( @@ -42,6 +44,26 @@ def get_appsettings(config_uri, name=None, appconfig=appconfig): here_dir = os.getcwd() return appconfig(config_name, name=section, relative_to=here_dir) +def setup_logging(config_uri, fileConfig=fileConfig, + configparser=configparser): + """ + Set up logging via the logging module's fileConfig function with the + filename specified via ``config_uri`` (a string in the form + ``filename#sectionname``). + + ConfigParser defaults are specified for the special ``__file__`` + and ``here`` variables, similar to PasteDeploy config loading. + """ + path, _ = _getpathsec(config_uri, None) + parser = configparser.ConfigParser() + parser.read([path]) + if parser.has_section('loggers'): + config_file = os.path.abspath(path) + return fileConfig( + config_file, + dict(__file__=config_file, here=os.path.dirname(config_file)) + ) + def _getpathsec(config_uri, name): if '#' in config_uri: path, section = config_uri.split('#', 1) diff --git a/pyramid/scripts/pserve.py b/pyramid/scripts/pserve.py index c6e8f0374..a0a3a8a70 100644 --- a/pyramid/scripts/pserve.py +++ b/pyramid/scripts/pserve.py @@ -22,7 +22,7 @@ import traceback from paste.deploy import loadapp, loadserver -from pyramid.scripts.common import logging_file_config +from pyramid.paster import setup_logging MAXFD = 1024 @@ -266,7 +266,7 @@ class PServeCommand(object): log_fn = None if log_fn: log_fn = os.path.join(base, log_fn) - logging_file_config(log_fn) + setup_logging(log_fn) server = self.loadserver(server_spec, name=server_name, relative_to=base, global_conf=vars) diff --git a/pyramid/scripts/pshell.py b/pyramid/scripts/pshell.py index 72e2c0a89..dfac9dbce 100644 --- a/pyramid/scripts/pshell.py +++ b/pyramid/scripts/pshell.py @@ -6,7 +6,7 @@ from pyramid.compat import configparser from pyramid.util import DottedNameResolver from pyramid.paster import bootstrap -from pyramid.scripts.common import logging_file_config +from pyramid.paster import setup_logging def main(argv=sys.argv, quiet=False): command = PShellCommand(argv, quiet) @@ -85,7 +85,7 @@ class PShellCommand(object): return config_uri = self.args[0] config_file = config_uri.split('#', 1)[0] - logging_file_config(config_file) + setup_logging(config_file) self.pshell_file_config(config_file) # bootstrap the environ diff --git a/pyramid/tests/test_paster.py b/pyramid/tests/test_paster.py index dc7e4f21f..5901c0416 100644 --- a/pyramid/tests/test_paster.py +++ b/pyramid/tests/test_paster.py @@ -70,7 +70,22 @@ class Test_get_appsettings(unittest.TestCase): self.assertEqual(appconfig.relative_to, os.getcwd()) self.assertEqual(result, values) -class TestBootstrap(unittest.TestCase): +class Test_setup_logging(unittest.TestCase): + def _callFUT(self, config_file): + from pyramid.paster import setup_logging + dummy_cp = DummyConfigParserModule + return setup_logging(config_file, self.fileConfig, dummy_cp) + + def test_it(self): + config_file, dict = self._callFUT('/abc') + self.assertEqual(config_file, '/abc') + self.assertEqual(dict['__file__'], '/abc') + self.assertEqual(dict['here'], '/') + + def fileConfig(self, config_file, dict): + return config_file, dict + +class Test_bootstrap(unittest.TestCase): def _callFUT(self, config_uri, request=None): from pyramid.paster import bootstrap return bootstrap(config_uri, request) @@ -138,3 +153,15 @@ class DummyRequest: self.environ = environ self.matchdict = {} +class DummyConfigParser(object): + def read(self, x): + pass + + def has_section(self, name): + return True + +class DummyConfigParserModule(object): + ConfigParser = DummyConfigParser + + + -- cgit v1.2.3