summaryrefslogtreecommitdiffstats
path: root/Lib/logging
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-12-07 15:30:06 (GMT)
committerGeorg Brandl <georg@python.org>2008-12-07 15:30:06 (GMT)
commitf9734076cf12444fb1b5e4296993bf6df3b1e7f2 (patch)
tree1a07422957dfa4cafe9868efd8965ab9abd6cf86 /Lib/logging
parent2080ea5f4b7ab8bd9ab0385c7ac925731d78405e (diff)
downloadcpython-f9734076cf12444fb1b5e4296993bf6df3b1e7f2.zip
cpython-f9734076cf12444fb1b5e4296993bf6df3b1e7f2.tar.gz
cpython-f9734076cf12444fb1b5e4296993bf6df3b1e7f2.tar.bz2
Merged revisions 67511,67536-67537,67543 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r67511 | vinay.sajip | 2008-12-04 00:22:58 +0100 (Thu, 04 Dec 2008) | 1 line Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging. ........ r67536 | gregory.p.smith | 2008-12-04 21:21:09 +0100 (Thu, 04 Dec 2008) | 3 lines Adds a subprocess.check_call_output() function to return the output from a process on success or raise an exception on error. ........ r67537 | vinay.sajip | 2008-12-04 21:32:18 +0100 (Thu, 04 Dec 2008) | 1 line Took Nick Coghlan's advice about importing warnings globally in logging, to avoid the possibility of race conditions: "This could deadlock if a thread spawned as a side effect of importing a module happens to trigger a warning. warnings is pulled into sys.modules as part of the interpreter startup - having a global 'import warnings' shouldn't have any real effect on logging's import time." ........ r67543 | gregory.p.smith | 2008-12-05 03:27:01 +0100 (Fri, 05 Dec 2008) | 2 lines rename the new check_call_output to check_output. its less ugly. ........
Diffstat (limited to 'Lib/logging')
-rw-r--r--Lib/logging/__init__.py60
1 files changed, 57 insertions, 3 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 499c954..cce6ea0 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -23,7 +23,8 @@ Copyright (C) 2001-2008 Vinay Sajip. All Rights Reserved.
To use, simply 'import logging' and log away!
"""
-import sys, os, time, io, traceback
+import sys, os, time, io, traceback, warnings
+
__all__ = ['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR',
'FATAL', 'FileHandler', 'Filter', 'Filterer', 'Formatter', 'Handler',
'INFO', 'LogRecord', 'Logger', 'Manager', 'NOTSET', 'PlaceHolder',
@@ -42,8 +43,8 @@ except ImportError:
__author__ = "Vinay Sajip <vinay_sajip@red-dove.com>"
__status__ = "production"
-__version__ = "0.5.0.5"
-__date__ = "24 January 2008"
+__version__ = "0.5.0.6"
+__date__ = "03 December 2008"
#---------------------------------------------------------------------------
# Miscellaneous module data
@@ -1483,3 +1484,56 @@ except ImportError: # for Python versions < 2.0
old_exit(status)
sys.exit = exithook
+
+# Null handler
+
+class NullHandler(Handler):
+ """
+ This handler does nothing. It's intended to be used to avoid the
+ "No handlers could be found for logger XXX" one-off warning. This is
+ important for library code, which may contain code to log events. If a user
+ of the library does not configure logging, the one-off warning might be
+ produced; to avoid this, the library developer simply needs to instantiate
+ a NullHandler and add it to the top-level logger of the library module or
+ package.
+ """
+ def emit(self, record):
+ pass
+
+# Warnings integration
+
+_warnings_showwarning = None
+
+def _showwarning(message, category, filename, lineno, file=None, line=None):
+ """
+ Implementation of showwarnings which redirects to logging, which will first
+ check to see if the file parameter is None. If a file is specified, it will
+ delegate to the original warnings implementation of showwarning. Otherwise,
+ it will call warnings.formatwarning and will log the resulting string to a
+ warnings logger named "py.warnings" with level logging.WARNING.
+ """
+ if file is not None:
+ if _warnings_showwarning is not None:
+ _warnings_showwarning(message, category, filename, lineno, file, line)
+ else:
+ s = warnings.formatwarning(message, category, filename, lineno, line)
+ logger = getLogger("py.warnings")
+ if not logger.handlers:
+ logger.addHandler(NullHandler())
+ logger.warning("%s", s)
+
+def captureWarnings(capture):
+ """
+ If capture is true, redirect all warnings to the logging package.
+ If capture is False, ensure that warnings are not redirected to logging
+ but to their original destinations.
+ """
+ global _warnings_showwarning
+ if capture:
+ if _warnings_showwarning is None:
+ _warnings_showwarning = warnings.showwarning
+ warnings.showwarning = _showwarning
+ else:
+ if _warnings_showwarning is not None:
+ warnings.showwarning = _warnings_showwarning
+ _warnings_showwarning = None