[Checkins] SVN: Sandbox/J1m/resumelb/src/zc/resumelb/zk. Added support for basic worker logging config.

Jim Fulton jim at zope.com
Mon Mar 5 13:20:39 UTC 2012


Log message for revision 124510:
  Added support for basic worker logging config.
  

Changed:
  U   Sandbox/J1m/resumelb/src/zc/resumelb/zk.py
  U   Sandbox/J1m/resumelb/src/zc/resumelb/zk.test

-=-
Modified: Sandbox/J1m/resumelb/src/zc/resumelb/zk.py
===================================================================
--- Sandbox/J1m/resumelb/src/zc/resumelb/zk.py	2012-03-05 12:49:14 UTC (rev 124509)
+++ Sandbox/J1m/resumelb/src/zc/resumelb/zk.py	2012-03-05 13:20:38 UTC (rev 124510)
@@ -17,6 +17,7 @@
 import gevent.pool
 import logging
 import os
+import re
 import sys
 import zc.parse_addr
 import zc.zk
@@ -27,8 +28,13 @@
     """
     # XXX support log level
     if loggers:
-        import ZConfig
-        ZConfig.configureLoggers(loggers)
+        if re.match(r'\d+$', loggers):
+            logging.basicConfig(level=int(loggers))
+        elif loggers in ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'):
+            logging.basicConfig(level=getattr(logging, loggers))
+        else:
+            import ZConfig
+            ZConfig.configureLoggers(loggers)
 
     zk = zc.zk.ZooKeeper(zookeeper)
     address = zc.parse_addr.parse_addr(address)

Modified: Sandbox/J1m/resumelb/src/zc/resumelb/zk.test
===================================================================
--- Sandbox/J1m/resumelb/src/zc/resumelb/zk.test	2012-03-05 12:49:14 UTC (rev 124509)
+++ Sandbox/J1m/resumelb/src/zc/resumelb/zk.test	2012-03-05 13:20:38 UTC (rev 124510)
@@ -50,7 +50,7 @@
   This defaults to ":0", to bind to a dynamic port on all IPv4 addresses.
 
 loggers
-  A ZConfig loggers-definition string
+  A ZConfig loggers-definition string, or a log-level
 
 Let's create a worker, making sure that ZConfig.configureLoggers was called.
 
@@ -59,6 +59,7 @@
     >>> with open('resume.mar', 'w') as f:
     ...     marshal.dump(dict(a=1.0, b=2.0), f)
     >>> with mock.patch('ZConfig.configureLoggers') as configureLoggers:
+    ...   with mock.patch('logging.basicConfig') as basicConfig:
     ...     worker = zc.resumelb.zk.worker(
     ...         app, None,
     ...         zookeeper='zookeeper.example.com:2181', path='/test/lb/workers',
@@ -66,6 +67,7 @@
     ...         resume_file='resume.mar',
     ...         )
     ...     configureLoggers.assert_called_with('loggers')
+    ...     if basicConfig.called: print 'basicConfig'
 
 Normally, when used with paste, the worker function runs forever.  We
 passed the run argument with a false value. The run argument exists
@@ -99,6 +101,49 @@
     >>> bool(meta['ephemeralOwner'])
     True
 
+Let's try again, but this time, don't set up logging:
+
+    >>> worker.stop(); worker.zk.close()
+    >>> with mock.patch('ZConfig.configureLoggers') as configureLoggers:
+    ...   with mock.patch('logging.basicConfig') as basicConfig:
+    ...     worker = zc.resumelb.zk.worker(
+    ...         app, None,
+    ...         zookeeper='zookeeper.example.com:2181', path='/test/lb/workers',
+    ...         address='127.0.0.1:0', run=False,
+    ...         resume_file='resume.mar',
+    ...         )
+    ...     if configureLoggers.called: print 'configureLoggers'
+    ...     if basicConfig.called: print 'basicConfig'
+
+Let's try again, but this time, set up basic logging:
+
+    >>> worker.stop(); worker.zk.close()
+    >>> with mock.patch('ZConfig.configureLoggers') as configureLoggers:
+    ...   with mock.patch('logging.basicConfig') as basicConfig:
+    ...     worker = zc.resumelb.zk.worker(
+    ...         app, None,
+    ...         zookeeper='zookeeper.example.com:2181', path='/test/lb/workers',
+    ...         address='127.0.0.1:0', run=False, loggers='INFO',
+    ...         resume_file='resume.mar',
+    ...         )
+    ...     if configureLoggers.called: print 'configureLoggers'
+    ...     basicConfig.assert_called_with(level=20)
+
+Let's try again, but this time, set up basic logging with a numeric level:
+
+    >>> worker.stop(); worker.zk.close()
+    >>> with mock.patch('ZConfig.configureLoggers') as configureLoggers:
+    ...   with mock.patch('logging.basicConfig') as basicConfig:
+    ...     worker = zc.resumelb.zk.worker(
+    ...         app, None,
+    ...         zookeeper='zookeeper.example.com:2181', path='/test/lb/workers',
+    ...         address='127.0.0.1:0', run=False, loggers='42',
+    ...         resume_file='resume.mar',
+    ...         )
+    ...     if configureLoggers.called: print 'configureLoggers'
+    ...     basicConfig.assert_called_with(level=42)
+
+
 LB
 ==
 



More information about the checkins mailing list