[Zodb-checkins] CVS: Zope/lib/python/zLOG/tests -
testzLogConfig.py:1.13
Fred L. Drake, Jr.
fred at zope.com
Mon Jan 5 12:04:14 EST 2004
Update of /cvs-repository/Zope/lib/python/zLOG/tests
In directory cvs.zope.org:/tmp/cvs-serv2498/zLOG/tests
Modified Files:
testzLogConfig.py
Log Message:
update zLOG configuration component to make use of the ZConfig support for
the logging package; this avoids lots of code duplication
=== Zope/lib/python/zLOG/tests/testzLogConfig.py 1.12 => 1.13 ===
--- Zope/lib/python/zLOG/tests/testzLogConfig.py:1.12 Wed Dec 31 14:56:41 2003
+++ Zope/lib/python/zLOG/tests/testzLogConfig.py Mon Jan 5 12:04:13 2004
@@ -16,13 +16,11 @@
import cStringIO as StringIO
import logging
-import sys
-import tempfile
import unittest
import ZConfig
-import zLOG.datatypes
-import zLOG.LogHandlers
+
+from ZConfig.components.logger import loghandler
class TestzLOGConfig(unittest.TestCase):
@@ -34,7 +32,7 @@
self._old_logger = logging.getLogger("event")
self._old_level = self._old_logger.level
self._old_handlers = self._old_logger.handlers[:]
- self._old_logger.handlers[:] = [zLOG.LogHandlers.NullHandler()]
+ self._old_logger.handlers[:] = [loghandler.NullHandler()]
def tearDown(self):
for h in self._old_logger.handlers:
@@ -63,36 +61,6 @@
self.assert_(not handler)
return conf
- def test_logging_level(self):
- # Make sure the expected names are supported; it's not clear
- # how to check the values in a meaningful way.
- # Just make sure they're case-insensitive.
- convert = zLOG.datatypes.logging_level
- for name in ["notset", "all", "trace", "debug", "blather",
- "info", "warn", "warning", "error", "fatal",
- "critical"]:
- self.assertEqual(convert(name), convert(name.upper()))
- self.assertRaises(ValueError, convert, "hopefully-not-a-valid-value")
-
- def test_http_method(self):
- convert = zLOG.datatypes.get_or_post
- self.assertEqual(convert("get"), "GET")
- self.assertEqual(convert("GET"), "GET")
- self.assertEqual(convert("post"), "POST")
- self.assertEqual(convert("POST"), "POST")
- self.assertRaises(ValueError, convert, "")
- self.assertRaises(ValueError, convert, "foo")
-
- def test_syslog_facility(self):
- convert = zLOG.datatypes.syslog_facility
- for name in ["auth", "authpriv", "cron", "daemon", "kern",
- "lpr", "mail", "news", "security", "syslog",
- "user", "uucp", "local0", "local1", "local2",
- "local3", "local4", "local5", "local6", "local7"]:
- self.assertEqual(convert(name), name)
- self.assertEqual(convert(name.upper()), name)
- self.assertRaises(ValueError, convert, "hopefully-never-a-valid-value")
-
def test_config_without_logger(self):
conf = self.get_config("")
self.assert_(conf.eventlog is None)
@@ -103,109 +71,7 @@
# printed if there are no handlers:
self.assertEqual(len(logger.handlers), 1)
self.assert_(isinstance(logger.handlers[0],
- zLOG.LogHandlers.NullHandler))
-
- def test_with_logfile(self):
- import os
- fn = tempfile.mktemp()
- logger = self.check_simple_logger("<eventlog>\n"
- " <logfile>\n"
- " path %s\n"
- " level debug\n"
- " </logfile>\n"
- "</eventlog>" % fn)
- logfile = logger.handlers[0]
- self.assertEqual(logfile.level, logging.DEBUG)
- self.assert_(isinstance(logfile, zLOG.LogHandlers.FileHandler))
- logfile.close()
- os.remove(fn)
-
- def test_with_stderr(self):
- self.check_standard_stream("stderr")
-
- def test_with_stdout(self):
- self.check_standard_stream("stdout")
-
- def check_standard_stream(self, name):
- old_stream = getattr(sys, name)
- conf = self.get_config("""
- <eventlog>
- <logfile>
- level info
- path %s
- </logfile>
- </eventlog>
- """ % name.upper())
- self.assert_(conf.eventlog is not None)
- # The factory has already been created; make sure it picks up
- # the stderr we set here when we create the logger and
- # handlers:
- sio = StringIO.StringIO()
- setattr(sys, name, sio)
- try:
- logger = conf.eventlog()
- finally:
- setattr(sys, name, old_stream)
- logger.warn("woohoo!")
- self.assert_(sio.getvalue().find("woohoo!") >= 0)
-
- def test_with_syslog(self):
- logger = self.check_simple_logger("<eventlog>\n"
- " <syslog>\n"
- " level error\n"
- " facility local3\n"
- " </syslog>\n"
- "</eventlog>")
- syslog = logger.handlers[0]
- self.assertEqual(syslog.level, logging.ERROR)
- self.assert_(isinstance(syslog, zLOG.LogHandlers.SysLogHandler))
-
- def test_with_http_logger_localhost(self):
- logger = self.check_simple_logger("<eventlog>\n"
- " <http-logger>\n"
- " level error\n"
- " method post\n"
- " </http-logger>\n"
- "</eventlog>")
- handler = logger.handlers[0]
- self.assertEqual(handler.host, "localhost")
- # XXX The "url" attribute of the handler is misnamed; it
- # really means just the selector portion of the URL.
- self.assertEqual(handler.url, "/")
- self.assertEqual(handler.level, logging.ERROR)
- self.assertEqual(handler.method, "POST")
- self.assert_(isinstance(handler, zLOG.LogHandlers.HTTPHandler))
-
- def test_with_http_logger_remote_host(self):
- logger = self.check_simple_logger("<eventlog>\n"
- " <http-logger>\n"
- " method get\n"
- " url http://example.com/log/\n"
- " </http-logger>\n"
- "</eventlog>")
- handler = logger.handlers[0]
- self.assertEqual(handler.host, "example.com")
- # XXX The "url" attribute of the handler is misnamed; it
- # really means just the selector portion of the URL.
- self.assertEqual(handler.url, "/log/")
- self.assertEqual(handler.level, logging.NOTSET)
- self.assertEqual(handler.method, "GET")
- self.assert_(isinstance(handler, zLOG.LogHandlers.HTTPHandler))
-
- def test_with_email_notifier(self):
- logger = self.check_simple_logger("<eventlog>\n"
- " <email-notifier>\n"
- " to sysadmin at example.com\n"
- " to sa-pager at example.com\n"
- " from zlog-user at example.com\n"
- " level fatal\n"
- " </email-notifier>\n"
- "</eventlog>")
- handler = logger.handlers[0]
- self.assertEqual(handler.toaddrs, ["sysadmin at example.com",
- "sa-pager at example.com"])
- self.assertEqual(handler.fromaddr, "zlog-user at example.com")
- self.assertEqual(handler.level, logging.FATAL)
+ loghandler.NullHandler))
def check_simple_logger(self, text, level=logging.INFO):
conf = self.get_config(text)
More information about the Zodb-checkins
mailing list