summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Abramowitz <marc@marc-abramowitz.com>2016-03-07 08:20:39 -0800
committerMarc Abramowitz <marc@marc-abramowitz.com>2016-04-06 11:03:28 -0700
commit1514ea003dfe39fa79a0ec07bbbc14f239cb4eb2 (patch)
treed9f257b9edb5699b1eb3886f71797a11217d555e
parenta9cdf5be43d9dd915f8611f540e2f839c2e8d6a0 (diff)
downloadpyramid-1514ea003dfe39fa79a0ec07bbbc14f239cb4eb2.tar.gz
pyramid-1514ea003dfe39fa79a0ec07bbbc14f239cb4eb2.tar.bz2
pyramid-1514ea003dfe39fa79a0ec07bbbc14f239cb4eb2.zip
Pass vars to logging.config.fileConfig
This allows one to set up a logging configuration that is parameterized based on variables specified on the command-line. e.g.: the application .ini file could have: ```ini [logger_root] level = %(LOGGING_LOGGER_ROOT_LEVEL)s handlers = console [handler_console] class = StreamHandler args = (sys.stderr,) level = %(LOGGING_HANDLER_CONSOLE_LEVEL)s formatter = generic ``` This app could be launched with: ``` pserve development.ini LOGGING_LOGGER_ROOT_LEVEL=DEBUG LOGGING_HANDLER_CONSOLE_LEVEL=DEBUG ```
-rw-r--r--CHANGES.txt6
-rw-r--r--docs/api/paster.rst2
-rw-r--r--pyramid/paster.py17
-rw-r--r--pyramid/scripts/pserve.py2
-rw-r--r--pyramid/tests/test_paster.py26
5 files changed, 43 insertions, 10 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index a17f4aab5..4a61dbffa 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -27,6 +27,12 @@ unreleased
where you may need more control over the request.
See https://github.com/Pylons/pyramid/pull/2393
+- Allow using variable substitutions like ``%(LOGGING_LOGGER_ROOT_LEVEL)s``
+ for logging sections of the .ini file and populate these variables from
+ the ``pserve`` command line -- e.g.:
+ ``pserve development.ini LOGGING_LOGGER_ROOT_LEVEL=DEBUG``
+ See https://github.com/Pylons/pyramid/pull/2399
+
1.6 (2015-04-14)
================
diff --git a/docs/api/paster.rst b/docs/api/paster.rst
index edc3738fc..27bc81a1f 100644
--- a/docs/api/paster.rst
+++ b/docs/api/paster.rst
@@ -11,4 +11,4 @@
.. autofunction:: get_appsettings(config_uri, name=None, options=None)
- .. autofunction:: setup_logging(config_uri)
+ .. autofunction:: setup_logging(config_uri, global_conf=None)
diff --git a/pyramid/paster.py b/pyramid/paster.py
index 967543849..5e2200ea7 100644
--- a/pyramid/paster.py
+++ b/pyramid/paster.py
@@ -52,7 +52,8 @@ def get_appsettings(config_uri, name=None, options=None, appconfig=appconfig):
relative_to=here_dir,
global_conf=options)
-def setup_logging(config_uri, fileConfig=fileConfig,
+def setup_logging(config_uri, global_conf=None,
+ fileConfig=fileConfig,
configparser=configparser):
"""
Set up logging via the logging module's fileConfig function with the
@@ -61,16 +62,22 @@ def setup_logging(config_uri, fileConfig=fileConfig,
ConfigParser defaults are specified for the special ``__file__``
and ``here`` variables, similar to PasteDeploy config loading.
+ Extra defaults can optionally be specified as a dict in ``global_conf``.
"""
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))
- )
+ if global_conf:
+ # Copy to avoid side effects
+ global_conf = dict(global_conf)
+ else:
+ global_conf = {}
+ global_conf.update(
+ __file__=config_file,
+ here=os.path.dirname(config_file))
+ return fileConfig(config_file, global_conf)
def _getpathsec(config_uri, name):
if '#' in config_uri:
diff --git a/pyramid/scripts/pserve.py b/pyramid/scripts/pserve.py
index 431afe6f4..74bda1dce 100644
--- a/pyramid/scripts/pserve.py
+++ b/pyramid/scripts/pserve.py
@@ -362,7 +362,7 @@ a real process manager for your processes like Systemd, Circus, or Supervisor.
log_fn = None
if log_fn:
log_fn = os.path.join(base, log_fn)
- setup_logging(log_fn)
+ setup_logging(log_fn, global_conf=vars)
server = self.loadserver(server_spec, name=server_name,
relative_to=base, global_conf=vars)
diff --git a/pyramid/tests/test_paster.py b/pyramid/tests/test_paster.py
index 5e341172c..22a5cde3d 100644
--- a/pyramid/tests/test_paster.py
+++ b/pyramid/tests/test_paster.py
@@ -105,18 +105,38 @@ class Test_get_appsettings(unittest.TestCase):
self.assertEqual(result['foo'], 'baz')
class Test_setup_logging(unittest.TestCase):
- def _callFUT(self, config_file):
+ def _callFUT(self, config_file, global_conf=None):
from pyramid.paster import setup_logging
dummy_cp = DummyConfigParserModule
- return setup_logging(config_file, self.fileConfig, dummy_cp)
+ return setup_logging(
+ config_uri=config_file,
+ global_conf=global_conf,
+ fileConfig=self.fileConfig,
+ configparser=dummy_cp,
+ )
- def test_it(self):
+ def test_it_no_global_conf(self):
config_file, dict = self._callFUT('/abc')
# os.path.abspath is a sop to Windows
self.assertEqual(config_file, os.path.abspath('/abc'))
self.assertEqual(dict['__file__'], os.path.abspath('/abc'))
self.assertEqual(dict['here'], os.path.abspath('/'))
+ def test_it_global_conf_empty(self):
+ config_file, dict = self._callFUT('/abc', global_conf={})
+ # os.path.abspath is a sop to Windows
+ self.assertEqual(config_file, os.path.abspath('/abc'))
+ self.assertEqual(dict['__file__'], os.path.abspath('/abc'))
+ self.assertEqual(dict['here'], os.path.abspath('/'))
+
+ def test_it_global_conf_not_empty(self):
+ config_file, dict = self._callFUT('/abc', global_conf={'key': 'val'})
+ # os.path.abspath is a sop to Windows
+ self.assertEqual(config_file, os.path.abspath('/abc'))
+ self.assertEqual(dict['__file__'], os.path.abspath('/abc'))
+ self.assertEqual(dict['here'], os.path.abspath('/'))
+ self.assertEqual(dict['key'], 'val')
+
def fileConfig(self, config_file, dict):
return config_file, dict