Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.

This commit is contained in:
Vinay Sajip 2009-10-10 20:32:36 +00:00
parent 6e3dbbdf39
commit 1c77b7f84c
5 changed files with 90 additions and 77 deletions

View file

@ -19,15 +19,12 @@ 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.
Should work under Python versions >= 1.5.2, except that source line
information is not available unless 'sys._getframe()' is.
Copyright (C) 2001-2008 Vinay Sajip. All Rights Reserved.
Copyright (C) 2001-2009 Vinay Sajip. All Rights Reserved.
To use, simply 'import logging' and log away!
"""
import sys, logging, logging.handlers, string, socket, struct, os, traceback, types
import sys, logging, logging.handlers, socket, struct, os, traceback
try:
import thread
@ -52,7 +49,7 @@ else:
# _listener holds the server object doing the listening
_listener = None
def fileConfig(fname, defaults=None, disable_existing_loggers=1):
def fileConfig(fname, defaults=None, disable_existing_loggers=True):
"""
Read the logging configuration from a ConfigParser-format file.
@ -89,7 +86,7 @@ def fileConfig(fname, defaults=None, disable_existing_loggers=1):
def _resolve(name):
"""Resolve a dotted name to a global object."""
name = string.split(name, '.')
name = name.split('.')
used = name.pop(0)
found = __import__(used)
for n in name:
@ -102,14 +99,14 @@ def _resolve(name):
return found
def _strip_spaces(alist):
return map(lambda x: string.strip(x), alist)
return map(lambda x: x.strip(), alist)
def _create_formatters(cp):
"""Create and return formatters"""
flist = cp.get("formatters", "keys")
if not len(flist):
return {}
flist = string.split(flist, ",")
flist = flist.split(",")
flist = _strip_spaces(flist)
formatters = {}
for form in flist:
@ -138,7 +135,7 @@ def _install_handlers(cp, formatters):
hlist = cp.get("handlers", "keys")
if not len(hlist):
return {}
hlist = string.split(hlist, ",")
hlist = hlist.split(",")
hlist = _strip_spaces(hlist)
handlers = {}
fixups = [] #for inter-handler references
@ -181,8 +178,8 @@ def _install_loggers(cp, handlers, disable_existing_loggers):
# configure the root first
llist = cp.get("loggers", "keys")
llist = string.split(llist, ",")
llist = map(lambda x: string.strip(x), llist)
llist = llist.split(",")
llist = list(map(lambda x: x.strip(), llist))
llist.remove("root")
sectname = "logger_root"
root = logging.root
@ -195,7 +192,7 @@ def _install_loggers(cp, handlers, disable_existing_loggers):
root.removeHandler(h)
hlist = cp.get(sectname, "handlers")
if len(hlist):
hlist = string.split(hlist, ",")
hlist = hlist.split(",")
hlist = _strip_spaces(hlist)
for hand in hlist:
log.addHandler(handlers[hand])
@ -209,7 +206,7 @@ def _install_loggers(cp, handlers, disable_existing_loggers):
#what's left in existing is the set of loggers
#which were in the previous configuration but
#which are not in the new configuration.
existing = root.manager.loggerDict.keys()
existing = list(root.manager.loggerDict.keys())
#The list needs to be sorted so that we can
#avoid disabling child loggers of explicitly
#named loggers. With a sorted list it is easier
@ -247,7 +244,7 @@ def _install_loggers(cp, handlers, disable_existing_loggers):
logger.disabled = 0
hlist = cp.get(sectname, "handlers")
if len(hlist):
hlist = string.split(hlist, ",")
hlist = hlist.split(",")
hlist = _strip_spaces(hlist)
for hand in hlist:
logger.addHandler(handlers[hand])
@ -278,7 +275,7 @@ def listen(port=DEFAULT_LOGGING_CONFIG_PORT):
stopListening().
"""
if not thread:
raise NotImplementedError, "listen() needs threading to work"
raise NotImplementedError("listen() needs threading to work")
class ConfigStreamHandler(StreamRequestHandler):
"""
@ -321,7 +318,7 @@ def listen(port=DEFAULT_LOGGING_CONFIG_PORT):
traceback.print_exc()
os.remove(file)
except socket.error, e:
if type(e.args) != types.TupleType:
if not isinstance(e.args, tuple):
raise
else:
errcode = e.args[0]