diff options
author | Christian Heimes <christian@cheimes.de> | 2008-01-18 18:40:46 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2008-01-18 18:40:46 (GMT) |
commit | 04c420f605b2bb7902f1afc3d62a45fbb0295bd0 (patch) | |
tree | 1f020f11a29cf7646f22275583fb6a9be1a8925c /Lib/logging | |
parent | 679db4aa99352abc3c9d93dcfc30e772760a43da (diff) | |
download | cpython-04c420f605b2bb7902f1afc3d62a45fbb0295bd0.zip cpython-04c420f605b2bb7902f1afc3d62a45fbb0295bd0.tar.gz cpython-04c420f605b2bb7902f1afc3d62a45fbb0295bd0.tar.bz2 |
Merged revisions 60043-60052 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60043 | christian.heimes | 2008-01-18 10:51:43 +0100 (Fri, 18 Jan 2008) | 2 lines
Build _ctypes after the other extensions. Its build process depends
on the _weakref extension (and maybe other modules, too)
........
r60048 | christian.heimes | 2008-01-18 12:58:50 +0100 (Fri, 18 Jan 2008) | 2 lines
Added win_add2path.py to Tools/scripts/
Added builddoc.bat to Doc/
........
r60049 | vinay.sajip | 2008-01-18 16:54:14 +0100 (Fri, 18 Jan 2008) | 1 line
Added section on passing contextual information to logging and documentation for the LoggerAdapter class.
........
r60050 | vinay.sajip | 2008-01-18 16:55:57 +0100 (Fri, 18 Jan 2008) | 1 line
Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
........
r60051 | georg.brandl | 2008-01-18 17:42:57 +0100 (Fri, 18 Jan 2008) | 2 lines
Note that genexps are function scopes too and therefore won't see class attributes.
........
r60052 | christian.heimes | 2008-01-18 19:24:07 +0100 (Fri, 18 Jan 2008) | 1 line
Added bytes and b'' as aliases for str and ''
........
Diffstat (limited to 'Lib/logging')
-rw-r--r-- | Lib/logging/__init__.py | 100 |
1 files changed, 95 insertions, 5 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 04e05b9..68fd10c 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1,4 +1,4 @@ -# Copyright 2001-2007 by Vinay Sajip. All Rights Reserved. +# Copyright 2001-2008 by Vinay Sajip. All Rights Reserved. # # Permission to use, copy, modify, and distribute this software and its # documentation for any purpose and without fee is hereby granted, @@ -18,7 +18,7 @@ Logging package for Python. 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! """ @@ -38,8 +38,8 @@ except ImportError: __author__ = "Vinay Sajip <vinay_sajip@red-dove.com>" __status__ = "production" -__version__ = "0.5.0.3" -__date__ = "26 September 2007" +__version__ = "0.5.0.4" +__date__ = "18 January 2008" #--------------------------------------------------------------------------- # Miscellaneous module data @@ -1076,7 +1076,7 @@ class Logger(Filterer): specialized LogRecords. """ rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func) - if extra: + if extra is not None: for key in extra: if (key in ["message", "asctime"]) or (key in rv.__dict__): raise KeyError("Attempt to overwrite %r in LogRecord" % key) @@ -1189,6 +1189,96 @@ class RootLogger(Logger): _loggerClass = Logger +class LoggerAdapter: + """ + An adapter for loggers which makes it easier to specify contextual + information in logging output. + """ + + def __init__(self, logger, extra): + """ + Initialize the adapter with a logger and a dict-like object which + provides contextual information. This constructor signature allows + easy stacking of LoggerAdapters, if so desired. + + You can effectively pass keyword arguments as shown in the + following example: + + adapter = LoggerAdapter(someLogger, dict(p1=v1, p2="v2")) + """ + self.logger = logger + self.extra = extra + + def process(self, msg, kwargs): + """ + Process the logging message and keyword arguments passed in to + a logging call to insert contextual information. You can either + manipulate the message itself, the keyword args or both. Return + the message and kwargs modified (or not) to suit your needs. + + Normally, you'll only need to override this one method in a + LoggerAdapter subclass for your specific needs. + """ + kwargs["extra"] = self.extra + return msg, kwargs + + def debug(self, msg, *args, **kwargs): + """ + Delegate a debug call to the underlying logger, after adding + contextual information from this adapter instance. + """ + msg, kwargs = self.process(msg, kwargs) + self.logger.debug(msg, *args, **kwargs) + + def info(self, msg, *args, **kwargs): + """ + Delegate an info call to the underlying logger, after adding + contextual information from this adapter instance. + """ + msg, kwargs = self.process(msg, kwargs) + self.logger.info(msg, *args, **kwargs) + + def warning(self, msg, *args, **kwargs): + """ + Delegate a warning call to the underlying logger, after adding + contextual information from this adapter instance. + """ + msg, kwargs = self.process(msg, kwargs) + self.logger.warning(msg, *args, **kwargs) + + def error(self, msg, *args, **kwargs): + """ + Delegate an error call to the underlying logger, after adding + contextual information from this adapter instance. + """ + msg, kwargs = self.process(msg, kwargs) + self.logger.error(msg, *args, **kwargs) + + def exception(self, msg, *args, **kwargs): + """ + Delegate an exception call to the underlying logger, after adding + contextual information from this adapter instance. + """ + msg, kwargs = self.process(msg, kwargs) + kwargs["exc_info"] = 1 + self.logger.error(msg, *args, **kwargs) + + def critical(self, msg, *args, **kwargs): + """ + Delegate a critical call to the underlying logger, after adding + contextual information from this adapter instance. + """ + msg, kwargs = self.process(msg, kwargs) + self.logger.critical(msg, *args, **kwargs) + + def log(self, level, msg, *args, **kwargs): + """ + Delegate a log call to the underlying logger, after adding + contextual information from this adapter instance. + """ + msg, kwargs = self.process(msg, kwargs) + self.logger.log(level, msg, *args, **kwargs) + root = RootLogger(WARNING) Logger.root = root Logger.manager = Manager(Logger.root) |