[Zconfig] SVN: ZConfig/trunk/ - Added a convenience function, ``ZConfig.configureLoggers(text)`` for
Jim Fulton
jim at zope.com
Wed Jun 10 05:56:22 EDT 2009
Log message for revision 100789:
- Added a convenience function, ``ZConfig.configureLoggers(text)`` for
configuring loggers.
- Relaxed the requirement for a logger name in logger sections,
allowing the logger section to be used for both root and non-root
loggers.
Changed:
U ZConfig/trunk/NEWS.txt
U ZConfig/trunk/ZConfig/__init__.py
U ZConfig/trunk/ZConfig/components/logger/logger.xml
U ZConfig/trunk/ZConfig/components/logger/tests/test_logger.py
-=-
Modified: ZConfig/trunk/NEWS.txt
===================================================================
--- ZConfig/trunk/NEWS.txt 2009-06-10 09:43:10 UTC (rev 100788)
+++ ZConfig/trunk/NEWS.txt 2009-06-10 09:56:22 UTC (rev 100789)
@@ -2,6 +2,16 @@
Change History for ZConfig
==========================
+ZConfig 2.7.0 (2009/06/10)
+--------------------------
+
+- Relaxed the requirement for a logger name in logger sections,
+ allowing the logger section to be used for both root and non-root
+ loggers.
+
+- Added a convenience function, ``ZConfig.configureLoggers(text)`` for
+ configuring loggers.
+
ZConfig 2.6.1 (2008/12/05)
--------------------------
@@ -19,7 +29,7 @@
--------------------------
- Added support for file rotation by time by specifying when and
- interval, rather than max-size, for log files.
+ interval, rather than max-size, for log files.
- Removed dependency on setuptools from the setup.py.
Modified: ZConfig/trunk/ZConfig/__init__.py
===================================================================
--- ZConfig/trunk/ZConfig/__init__.py 2009-06-10 09:43:10 UTC (rev 100788)
+++ ZConfig/trunk/ZConfig/__init__.py 2009-06-10 09:56:22 UTC (rev 100789)
@@ -159,3 +159,16 @@
self.name = name
ConfigurationSyntaxError.__init__(
self, "no replacement for " + `name`, url, lineno)
+
+def configureLoggers(text):
+ """Configure one or more loggers from configuration text
+ """
+ import StringIO
+ schema = loadSchemaFile(StringIO.StringIO("""
+ <schema>
+ <import package='ZConfig.components.logger'/>
+ <multisection type='logger' name='*' attribute='loggers'/>
+ </schema>
+ """))
+ for factory in loadConfigFile(schema, StringIO.StringIO(text))[0].loggers:
+ factory()
Modified: ZConfig/trunk/ZConfig/components/logger/logger.xml
===================================================================
--- ZConfig/trunk/ZConfig/components/logger/logger.xml 2009-06-10 09:43:10 UTC (rev 100788)
+++ ZConfig/trunk/ZConfig/components/logger/logger.xml 2009-06-10 09:56:22 UTC (rev 100789)
@@ -25,7 +25,7 @@
<key name="name"
datatype="dotted-name"
- required="yes">
+ >
<description>
The dotted name of the logger. This give it a location in the
logging hierarchy. Most applications provide a specific set
Modified: ZConfig/trunk/ZConfig/components/logger/tests/test_logger.py
===================================================================
--- ZConfig/trunk/ZConfig/components/logger/tests/test_logger.py 2009-06-10 09:43:10 UTC (rev 100788)
+++ ZConfig/trunk/ZConfig/components/logger/tests/test_logger.py 2009-06-10 09:56:22 UTC (rev 100789)
@@ -15,6 +15,7 @@
"""Tests for logging configuration via ZConfig."""
import cStringIO as StringIO
+import doctest
import logging
import os
import sys
@@ -578,9 +579,61 @@
logger.removeHandler(handler)
handler.close()
+def test_logger_convenience_function_and_ommiting_name_to_get_root_logger():
+ """
+The ZConfig.loggers function can be used to configure one or more loggers.
+We'll configure the rot logger and a non-root logger.
+
+ >>> old_level = logging.getLogger().getEffectiveLevel()
+ >>> old_handler_count = len(logging.getLogger().handlers)
+
+ >>> ZConfig.configureLoggers('''
+ ... <logger>
+ ... level INFO
+ ... <logfile>
+ ... PATH STDOUT
+ ... format root %(levelname)s %(name)s %(message)s
+ ... </logfile>
+ ... </logger>
+ ...
+ ... <logger>
+ ... name ZConfig.TEST
+ ... level DEBUG
+ ... <logfile>
+ ... PATH STDOUT
+ ... format test %(levelname)s %(name)s %(message)s
+ ... </logfile>
+ ... </logger>
+ ... ''')
+
+ >>> logging.getLogger('ZConfig.TEST').debug('test message')
+ test DEBUG ZConfig.TEST test message
+ root DEBUG ZConfig.TEST test message
+
+ >>> logging.getLogger().getEffectiveLevel() == logging.INFO
+ True
+ >>> len(logging.getLogger().handlers) == old_handler_count + 1
+ True
+ >>> logging.getLogger('ZConfig.TEST').getEffectiveLevel() == logging.DEBUG
+ True
+ >>> len(logging.getLogger('ZConfig.TEST').handlers) == 1
+ True
+
+.. cleanup
+
+ >>> logging.getLogger('ZConfig.TEST').setLevel(logging.NOTSET)
+ >>> logging.getLogger('ZConfig.TEST').removeHandler(
+ ... logging.getLogger('ZConfig.TEST').handlers[-1])
+ >>> logging.getLogger().setLevel(old_level)
+ >>> logging.getLogger().removeHandler(logging.getLogger().handlers[-1])
+
+
+ """
+
def test_suite():
suite = unittest.TestSuite()
+ suite.addTest(doctest.DocTestSuite())
suite.addTest(unittest.makeSuite(TestConfig))
if os.name != "nt":
suite.addTest(unittest.makeSuite(TestReopeningLogfiles))
More information about the ZConfig
mailing list