diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/logging/__init__.py | 2 | ||||
-rw-r--r-- | Lib/logging/config.py | 16 | ||||
-rw-r--r-- | Lib/test/test_logging.py | 73 |
3 files changed, 84 insertions, 7 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 552ce18..499c954 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -753,7 +753,7 @@ class StreamHandler(Handler): self.stream.write(fs % msg) else: try: - if hasattr(self.stream, 'encoding'): + if getattr(self.stream, 'encoding', None) is not None: self.stream.write(fs % msg.encode(self.stream.encoding)) else: self.stream.write(fs % msg) diff --git a/Lib/logging/config.py b/Lib/logging/config.py index a9a4e62..93d68a3 100644 --- a/Lib/logging/config.py +++ b/Lib/logging/config.py @@ -19,7 +19,7 @@ Configuration functions for the logging package for Python. The core package is based on PEP 282 and comments thereto in comp.lang.python, and influenced by Apache's log4j system. -Copyright (C) 2001-2007 Vinay Sajip. All Rights Reserved. +Copyright (C) 2001-2008 Vinay Sajip. All Rights Reserved. To use, simply 'import logging' and log away! """ @@ -98,6 +98,8 @@ def _resolve(name): found = getattr(found, n) return found +def _strip_spaces(alist): + return map(lambda x: x.strip(), alist) def _create_formatters(cp): """Create and return formatters""" @@ -105,9 +107,10 @@ def _create_formatters(cp): if not len(flist): return {} flist = flist.split(",") + flist = _strip_spaces(flist) formatters = {} for form in flist: - sectname = "formatter_%s" % form.strip() + sectname = "formatter_%s" % form opts = cp.options(sectname) if "format" in opts: fs = cp.get(sectname, "format", 1) @@ -133,10 +136,11 @@ def _install_handlers(cp, formatters): if not len(hlist): return {} hlist = hlist.split(",") + hlist = _strip_spaces(hlist) handlers = {} fixups = [] #for inter-handler references for hand in hlist: - sectname = "handler_%s" % hand.strip() + sectname = "handler_%s" % hand klass = cp.get(sectname, "class") opts = cp.options(sectname) if "formatter" in opts: @@ -189,8 +193,9 @@ def _install_loggers(cp, handlers, disable_existing_loggers): hlist = cp.get(sectname, "handlers") if len(hlist): hlist = hlist.split(",") + hlist = _strip_spaces(hlist) for hand in hlist: - log.addHandler(handlers[hand.strip()]) + log.addHandler(handlers[hand]) #and now the others... #we don't want to lose the existing loggers, @@ -240,8 +245,9 @@ def _install_loggers(cp, handlers, disable_existing_loggers): hlist = cp.get(sectname, "handlers") if len(hlist): hlist = hlist.split(",") + hlist = _strip_spaces(hlist) for hand in hlist: - logger.addHandler(handlers[hand.strip()]) + logger.addHandler(handlers[hand]) #Disable any old loggers. There's no point deleting #them as other threads may continue to hold references diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 55502d6..75d1366 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -587,6 +587,48 @@ class ConfigFileTest(BaseTest): # config5 specifies a custom handler class to be loaded config5 = config1.replace('class=StreamHandler', 'class=logging.StreamHandler') + # config6 uses ', ' delimiters in the handlers and formatters sections + config6 = """ + [loggers] + keys=root,parser + + [handlers] + keys=hand1, hand2 + + [formatters] + keys=form1, form2 + + [logger_root] + level=WARNING + handlers= + + [logger_parser] + level=DEBUG + handlers=hand1 + propagate=1 + qualname=compiler.parser + + [handler_hand1] + class=StreamHandler + level=NOTSET + formatter=form1 + args=(sys.stdout,) + + [handler_hand2] + class=FileHandler + level=NOTSET + formatter=form1 + args=('test.blah', 'a') + + [formatter_form1] + format=%(levelname)s ++ %(message)s + datefmt= + + [formatter_form2] + format=%(message)s + datefmt= + """ + def apply_config(self, conf): try: fn = tempfile.mktemp(".ini") @@ -653,6 +695,9 @@ class ConfigFileTest(BaseTest): def test_config5_ok(self): self.test_config1_ok(config=self.config5) + def test_config6_ok(self): + self.test_config1_ok(config=self.config6) + class LogRecordStreamHandler(StreamRequestHandler): """Handler for a streaming logging request. It saves the log message in the @@ -814,6 +859,31 @@ class MemoryTest(BaseTest): ('foo', 'DEBUG', '3'), ]) +class EncodingTest(BaseTest): + def test_encoding_plain_file(self): + # In Python 2.x, a plain file object is treated as having no encoding. + log = logging.getLogger("test") + fn = tempfile.mktemp(".log") + # the non-ascii data we write to the log. + data = "foo\x80" + try: + handler = logging.FileHandler(fn, encoding="utf8") + log.addHandler(handler) + try: + # write non-ascii data to the log. + log.warning(data) + finally: + log.removeHandler(handler) + handler.close() + # check we wrote exactly those bytes, ignoring trailing \n etc + f = open(fn, encoding="utf8") + try: + self.failUnlessEqual(f.read().rstrip(), data) + finally: + f.close() + finally: + if os.path.isfile(fn): + os.remove(fn) # Set the locale to the platform-dependent default. I have no idea # why the test does this, but in any case we save the current locale @@ -822,7 +892,8 @@ class MemoryTest(BaseTest): def test_main(): run_unittest(BuiltinLevelsTest, BasicFilterTest, CustomLevelsAndFiltersTest, MemoryHandlerTest, - ConfigFileTest, SocketHandlerTest, MemoryTest) + ConfigFileTest, SocketHandlerTest, MemoryTest, + EncodingTest) if __name__ == "__main__": test_main() |