mirror of
https://github.com/python/cpython.git
synced 2025-10-13 02:13:03 +00:00
Refactoring for fileConfig. Contributed by Shane Hathaway.
This commit is contained in:
parent
fe03bee62f
commit
989b69a519
1 changed files with 142 additions and 125 deletions
|
@ -72,9 +72,25 @@ def fileConfig(fname, defaults=None):
|
||||||
cp.readfp(fname)
|
cp.readfp(fname)
|
||||||
else:
|
else:
|
||||||
cp.read(fname)
|
cp.read(fname)
|
||||||
#first, do the formatters...
|
|
||||||
|
formatters = _create_formatters(cp)
|
||||||
|
|
||||||
|
# critical section
|
||||||
|
logging._acquireLock()
|
||||||
|
try:
|
||||||
|
logging._handlers.clear()
|
||||||
|
# Handlers add themselves to logging._handlers
|
||||||
|
handlers = _install_handlers(cp, formatters)
|
||||||
|
_install_loggers(cp, handlers)
|
||||||
|
finally:
|
||||||
|
logging._releaseLock()
|
||||||
|
|
||||||
|
|
||||||
|
def _create_formatters(cp):
|
||||||
|
"""Create and return formatters"""
|
||||||
flist = cp.get("formatters", "keys")
|
flist = cp.get("formatters", "keys")
|
||||||
if len(flist):
|
if not len(flist):
|
||||||
|
return {}
|
||||||
flist = string.split(flist, ",")
|
flist = string.split(flist, ",")
|
||||||
formatters = {}
|
formatters = {}
|
||||||
for form in flist:
|
for form in flist:
|
||||||
|
@ -90,21 +106,18 @@ def fileConfig(fname, defaults=None):
|
||||||
dfs = None
|
dfs = None
|
||||||
f = logging.Formatter(fs, dfs)
|
f = logging.Formatter(fs, dfs)
|
||||||
formatters[form] = f
|
formatters[form] = f
|
||||||
#next, do the handlers...
|
return formatters
|
||||||
#critical section...
|
|
||||||
logging._acquireLock()
|
|
||||||
try:
|
def _install_handlers(cp, formatters):
|
||||||
try:
|
"""Install and return handlers"""
|
||||||
#first, lose the existing handlers...
|
|
||||||
logging._handlers.clear()
|
|
||||||
#now set up the new ones...
|
|
||||||
hlist = cp.get("handlers", "keys")
|
hlist = cp.get("handlers", "keys")
|
||||||
if len(hlist):
|
if not len(hlist):
|
||||||
|
return {}
|
||||||
hlist = string.split(hlist, ",")
|
hlist = string.split(hlist, ",")
|
||||||
handlers = {}
|
handlers = {}
|
||||||
fixups = [] #for inter-handler references
|
fixups = [] #for inter-handler references
|
||||||
for hand in hlist:
|
for hand in hlist:
|
||||||
try:
|
|
||||||
sectname = "handler_%s" % hand
|
sectname = "handler_%s" % hand
|
||||||
klass = cp.get(sectname, "class")
|
klass = cp.get(sectname, "class")
|
||||||
opts = cp.options(sectname)
|
opts = cp.options(sectname)
|
||||||
|
@ -130,14 +143,16 @@ def fileConfig(fname, defaults=None):
|
||||||
if len(target): #the target handler may not be loaded yet, so keep for later...
|
if len(target): #the target handler may not be loaded yet, so keep for later...
|
||||||
fixups.append((h, target))
|
fixups.append((h, target))
|
||||||
handlers[hand] = h
|
handlers[hand] = h
|
||||||
except: #if an error occurs when instantiating a handler, too bad
|
|
||||||
pass #this could happen e.g. because of lack of privileges
|
|
||||||
#now all handlers are loaded, fixup inter-handler references...
|
#now all handlers are loaded, fixup inter-handler references...
|
||||||
for fixup in fixups:
|
for h, t in fixups:
|
||||||
h = fixup[0]
|
|
||||||
t = fixup[1]
|
|
||||||
h.setTarget(handlers[t])
|
h.setTarget(handlers[t])
|
||||||
#at last, the loggers...first the root...
|
return handlers
|
||||||
|
|
||||||
|
|
||||||
|
def _install_loggers(cp, handlers):
|
||||||
|
"""Create and install loggers"""
|
||||||
|
|
||||||
|
# configure the root first
|
||||||
llist = cp.get("loggers", "keys")
|
llist = cp.get("loggers", "keys")
|
||||||
llist = string.split(llist, ",")
|
llist = string.split(llist, ",")
|
||||||
llist.remove("root")
|
llist.remove("root")
|
||||||
|
@ -155,6 +170,7 @@ def fileConfig(fname, defaults=None):
|
||||||
hlist = string.split(hlist, ",")
|
hlist = string.split(hlist, ",")
|
||||||
for hand in hlist:
|
for hand in hlist:
|
||||||
log.addHandler(handlers[hand])
|
log.addHandler(handlers[hand])
|
||||||
|
|
||||||
#and now the others...
|
#and now the others...
|
||||||
#we don't want to lose the existing loggers,
|
#we don't want to lose the existing loggers,
|
||||||
#since other threads may have pointers to them.
|
#since other threads may have pointers to them.
|
||||||
|
@ -189,17 +205,13 @@ def fileConfig(fname, defaults=None):
|
||||||
hlist = string.split(hlist, ",")
|
hlist = string.split(hlist, ",")
|
||||||
for hand in hlist:
|
for hand in hlist:
|
||||||
logger.addHandler(handlers[hand])
|
logger.addHandler(handlers[hand])
|
||||||
|
|
||||||
#Disable any old loggers. There's no point deleting
|
#Disable any old loggers. There's no point deleting
|
||||||
#them as other threads may continue to hold references
|
#them as other threads may continue to hold references
|
||||||
#and by disabling them, you stop them doing any logging.
|
#and by disabling them, you stop them doing any logging.
|
||||||
for log in existing:
|
for log in existing:
|
||||||
root.manager.loggerDict[log].disabled = 1
|
root.manager.loggerDict[log].disabled = 1
|
||||||
except:
|
|
||||||
ei = sys.exc_info()
|
|
||||||
traceback.print_exception(ei[0], ei[1], ei[2], None, sys.stderr)
|
|
||||||
del ei
|
|
||||||
finally:
|
|
||||||
logging._releaseLock()
|
|
||||||
|
|
||||||
def listen(port=DEFAULT_LOGGING_CONFIG_PORT):
|
def listen(port=DEFAULT_LOGGING_CONFIG_PORT):
|
||||||
"""
|
"""
|
||||||
|
@ -247,7 +259,12 @@ def listen(port=DEFAULT_LOGGING_CONFIG_PORT):
|
||||||
f = open(file, "w")
|
f = open(file, "w")
|
||||||
f.write(chunk)
|
f.write(chunk)
|
||||||
f.close()
|
f.close()
|
||||||
|
try:
|
||||||
fileConfig(file)
|
fileConfig(file)
|
||||||
|
except (KeyboardInterrupt, SystemExit):
|
||||||
|
raise
|
||||||
|
except:
|
||||||
|
traceback.print_exc()
|
||||||
os.remove(file)
|
os.remove(file)
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
if type(e.args) != types.TupleType:
|
if type(e.args) != types.TupleType:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue