diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2009-10-10 20:32:36 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2009-10-10 20:32:36 (GMT) |
commit | 1c77b7f84c5fca050980854a677539ba377439dd (patch) | |
tree | 05297baa544790a3fff404e9fdb969b19e94427f /Lib/logging/__init__.py | |
parent | 6e3dbbdf39f3b4eb6f18c0165e446df17218b7dc (diff) | |
download | cpython-1c77b7f84c5fca050980854a677539ba377439dd.zip cpython-1c77b7f84c5fca050980854a677539ba377439dd.tar.gz cpython-1c77b7f84c5fca050980854a677539ba377439dd.tar.bz2 |
Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Diffstat (limited to 'Lib/logging/__init__.py')
-rw-r--r-- | Lib/logging/__init__.py | 66 |
1 files changed, 34 insertions, 32 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 092ebc3..0be6ed4 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -23,6 +23,8 @@ Copyright (C) 2001-2009 Vinay Sajip. All Rights Reserved. To use, simply 'import logging' and log away! """ +import sys, os, time, cStringIO, traceback, warnings + __all__ = ['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR', 'FATAL', 'FileHandler', 'Filter', 'Formatter', 'Handler', 'INFO', 'LogRecord', 'Logger', 'LoggerAdapter', 'NOTSET', 'NullHandler', @@ -31,8 +33,6 @@ __all__ = ['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR', 'exception', 'fatal', 'getLevelName', 'getLogger', 'getLoggerClass', 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'warn', 'warning'] -import sys, os, types, time, string, cStringIO, traceback, warnings - try: import codecs except ImportError: @@ -46,12 +46,17 @@ except ImportError: __author__ = "Vinay Sajip <vinay_sajip@red-dove.com>" __status__ = "production" -__version__ = "0.5.0.8" -__date__ = "27 April 2009" +__version__ = "0.5.0.9" +__date__ = "09 October 2009" #--------------------------------------------------------------------------- # Miscellaneous module data #--------------------------------------------------------------------------- +try: + unicode + _unicode = True +except NameError: + _unicode = False # # _srcfile is used when walking the stack to check when we've got the first @@ -59,7 +64,7 @@ __date__ = "27 April 2009" # if hasattr(sys, 'frozen'): #support for py2exe _srcfile = "logging%s__init__%s" % (os.sep, __file__[-4:]) -elif string.lower(__file__[-4:]) in ['.pyc', '.pyo']: +elif __file__[-4:].lower() in ['.pyc', '.pyo']: _srcfile = __file__[:-4] + '.py' else: _srcfile = __file__ @@ -71,7 +76,7 @@ def currentframe(): try: raise Exception except: - return sys.exc_traceback.tb_frame.f_back + return sys.exc_info()[2].tb_frame.f_back if hasattr(sys, '_getframe'): currentframe = lambda: sys._getframe(3) # done filching @@ -255,9 +260,7 @@ class LogRecord: # 'Value is %d' instead of 'Value is 0'. # For the use case of passing a dictionary, this should not be a # problem. - if args and len(args) == 1 and ( - type(args[0]) == types.DictType - ) and args[0]: + if args and len(args) == 1 and isinstance(args[0], dict) and args[0]: args = args[0] self.args = args self.levelname = getLevelName(level) @@ -306,11 +309,11 @@ class LogRecord: Return the message for this LogRecord after merging any user-supplied arguments with the message. """ - if not hasattr(types, "UnicodeType"): #if no unicode support... + if not _unicode: #if no unicode support... msg = str(self.msg) else: msg = self.msg - if type(msg) not in (types.UnicodeType, types.StringType): + if not isinstance(msg, basestring): try: msg = str(self.msg) except UnicodeError: @@ -447,7 +450,7 @@ class Formatter: formatException() and appended to the message. """ record.message = record.getMessage() - if string.find(self._fmt,"%(asctime)") >= 0: + if self._fmt.find("%(asctime)") >= 0: record.asctime = self.formatTime(record, self.datefmt) s = self._fmt % record.__dict__ if record.exc_info: @@ -541,7 +544,7 @@ class Filter: return 1 elif self.name == record.name: return 1 - elif string.find(record.name, self.name, 0, self.nlen) != 0: + elif record.name.find(self.name, 0, self.nlen) != 0: return 0 return (record.name[self.nlen] == ".") @@ -667,8 +670,8 @@ class Handler(Filterer): This version is intended to be implemented by subclasses and so raises a NotImplementedError. """ - raise NotImplementedError, 'emit must be implemented '\ - 'by Handler subclasses' + raise NotImplementedError('emit must be implemented ' + 'by Handler subclasses') def handle(self, record): """ @@ -781,7 +784,7 @@ class StreamHandler(Handler): msg = self.format(record) stream = self.stream fs = "%s\n" - if not hasattr(types, "UnicodeType"): #if no unicode support... + if not _unicode: #if no unicode support... stream.write(fs % msg) else: try: @@ -903,8 +906,8 @@ def setLoggerClass(klass): """ if klass != Logger: if not issubclass(klass, Logger): - raise TypeError, "logger not derived from logging.Logger: " + \ - klass.__name__ + raise TypeError("logger not derived from logging.Logger: " + + klass.__name__) global _loggerClass _loggerClass = klass @@ -967,7 +970,7 @@ class Manager: from the specified logger to the root of the logger hierarchy. """ name = alogger.name - i = string.rfind(name, ".") + i = name.rfind(".") rv = None while (i > 0) and not rv: substr = name[:i] @@ -980,7 +983,7 @@ class Manager: else: assert isinstance(obj, PlaceHolder) obj.append(alogger) - i = string.rfind(name, ".", 0, i - 1) + i = name.rfind(".", 0, i - 1) if not rv: rv = self.root alogger.parent = rv @@ -994,7 +997,6 @@ class Manager: namelen = len(name) for c in ph.loggerMap.keys(): #The if means ... if not c.parent.name.startswith(nm) - #if string.find(c.parent.name, nm) <> 0: if c.parent.name[:namelen] != name: alogger.parent = c.parent c.parent = alogger @@ -1090,7 +1092,7 @@ class Logger(Filterer): """ Convenience method for logging an ERROR with exception information. """ - self.error(*((msg,) + args), **{'exc_info': 1}) + self.error(msg, exc_info=1, *args) def critical(self, msg, *args, **kwargs): """ @@ -1115,9 +1117,9 @@ class Logger(Filterer): logger.log(level, "We have a %s", "mysterious problem", exc_info=1) """ - if type(level) != types.IntType: + if not isinstance(level, int): if raiseExceptions: - raise TypeError, "level must be an integer" + raise TypeError("level must be an integer") else: return if self.isEnabledFor(level): @@ -1173,7 +1175,7 @@ class Logger(Filterer): else: fn, lno, func = "(unknown file)", 0, "(unknown function)" if exc_info: - if type(exc_info) != types.TupleType: + if not isinstance(exc_info, tuple): exc_info = sys.exc_info() record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra) self.handle(record) @@ -1449,7 +1451,7 @@ def critical(msg, *args, **kwargs): """ if len(root.handlers) == 0: basicConfig() - root.critical(*((msg,)+args), **kwargs) + root.critical(msg, *args, **kwargs) fatal = critical @@ -1459,14 +1461,14 @@ def error(msg, *args, **kwargs): """ if len(root.handlers) == 0: basicConfig() - root.error(*((msg,)+args), **kwargs) + root.error(msg, *args, **kwargs) def exception(msg, *args): """ Log a message with severity 'ERROR' on the root logger, with exception information. """ - error(*((msg,)+args), **{'exc_info': 1}) + error(msg, exc_info=1, *args) def warning(msg, *args, **kwargs): """ @@ -1474,7 +1476,7 @@ def warning(msg, *args, **kwargs): """ if len(root.handlers) == 0: basicConfig() - root.warning(*((msg,)+args), **kwargs) + root.warning(msg, *args, **kwargs) warn = warning @@ -1484,7 +1486,7 @@ def info(msg, *args, **kwargs): """ if len(root.handlers) == 0: basicConfig() - root.info(*((msg,)+args), **kwargs) + root.info(msg, *args, **kwargs) def debug(msg, *args, **kwargs): """ @@ -1492,7 +1494,7 @@ def debug(msg, *args, **kwargs): """ if len(root.handlers) == 0: basicConfig() - root.debug(*((msg,)+args), **kwargs) + root.debug(msg, *args, **kwargs) def log(level, msg, *args, **kwargs): """ @@ -1500,7 +1502,7 @@ def log(level, msg, *args, **kwargs): """ if len(root.handlers) == 0: basicConfig() - root.log(*((level, msg)+args), **kwargs) + root.log(level, msg, *args, **kwargs) def disable(level): """ |