summaryrefslogtreecommitdiffstats
path: root/Lib/logging
diff options
context:
space:
mode:
authorŁukasz Langa <lukasz@langa.pl>2010-11-10 18:57:39 (GMT)
committerŁukasz Langa <lukasz@langa.pl>2010-11-10 18:57:39 (GMT)
commit26d513cf2ff31ba7c4f8862d5d28cb4cfa71de67 (patch)
tree058c9a6c41ea34dfd04472c5328463e063bbdcd9 /Lib/logging
parent47f637ce17022464307b093cee6f367e2dc7f024 (diff)
downloadcpython-26d513cf2ff31ba7c4f8862d5d28cb4cfa71de67.zip
cpython-26d513cf2ff31ba7c4f8862d5d28cb4cfa71de67.tar.gz
cpython-26d513cf2ff31ba7c4f8862d5d28cb4cfa71de67.tar.bz2
Issue #5412: extend configparser to support mapping access
Diffstat (limited to 'Lib/logging')
-rw-r--r--Lib/logging/config.py70
1 files changed, 25 insertions, 45 deletions
diff --git a/Lib/logging/config.py b/Lib/logging/config.py
index 12c1c13..5afdf9f 100644
--- a/Lib/logging/config.py
+++ b/Lib/logging/config.py
@@ -103,7 +103,7 @@ def _encoded(s):
def _create_formatters(cp):
"""Create and return formatters"""
- flist = cp.get("formatters", "keys")
+ flist = cp["formatters"]["keys"]
if not len(flist):
return {}
flist = flist.split(",")
@@ -111,20 +111,12 @@ def _create_formatters(cp):
formatters = {}
for form in flist:
sectname = "formatter_%s" % form
- opts = cp.options(sectname)
- if "format" in opts:
- fs = cp.get(sectname, "format", 1)
- else:
- fs = None
- if "datefmt" in opts:
- dfs = cp.get(sectname, "datefmt", 1)
- else:
- dfs = None
+ fs = cp.get(sectname, "format", raw=True, fallback=None)
+ dfs = cp.get(sectname, "datefmt", raw=True, fallback=None)
c = logging.Formatter
- if "class" in opts:
- class_name = cp.get(sectname, "class")
- if class_name:
- c = _resolve(class_name)
+ class_name = cp[sectname].get("class")
+ if class_name:
+ c = _resolve(class_name)
f = c(fs, dfs)
formatters[form] = f
return formatters
@@ -132,7 +124,7 @@ def _create_formatters(cp):
def _install_handlers(cp, formatters):
"""Install and return handlers"""
- hlist = cp.get("handlers", "keys")
+ hlist = cp["handlers"]["keys"]
if not len(hlist):
return {}
hlist = hlist.split(",")
@@ -140,30 +132,23 @@ def _install_handlers(cp, formatters):
handlers = {}
fixups = [] #for inter-handler references
for hand in hlist:
- sectname = "handler_%s" % hand
- klass = cp.get(sectname, "class")
- opts = cp.options(sectname)
- if "formatter" in opts:
- fmt = cp.get(sectname, "formatter")
- else:
- fmt = ""
+ section = cp["handler_%s" % hand]
+ klass = section["class"]
+ fmt = section.get("formatter", "")
try:
klass = eval(klass, vars(logging))
except (AttributeError, NameError):
klass = _resolve(klass)
- args = cp.get(sectname, "args")
+ args = section["args"]
args = eval(args, vars(logging))
h = klass(*args)
- if "level" in opts:
- level = cp.get(sectname, "level")
+ if "level" in section:
+ level = section["level"]
h.setLevel(logging._levelNames[level])
if len(fmt):
h.setFormatter(formatters[fmt])
if issubclass(klass, logging.handlers.MemoryHandler):
- if "target" in opts:
- target = cp.get(sectname,"target")
- else:
- target = ""
+ target = section.get("target", "")
if len(target): #the target handler may not be loaded yet, so keep for later...
fixups.append((h, target))
handlers[hand] = h
@@ -197,20 +182,19 @@ def _install_loggers(cp, handlers, disable_existing):
"""Create and install loggers"""
# configure the root first
- llist = cp.get("loggers", "keys")
+ llist = cp["loggers"]["keys"]
llist = llist.split(",")
llist = list(map(lambda x: x.strip(), llist))
llist.remove("root")
- sectname = "logger_root"
+ section = cp["logger_root"]
root = logging.root
log = root
- opts = cp.options(sectname)
- if "level" in opts:
- level = cp.get(sectname, "level")
+ if "level" in section:
+ level = section["level"]
log.setLevel(logging._levelNames[level])
for h in root.handlers[:]:
root.removeHandler(h)
- hlist = cp.get(sectname, "handlers")
+ hlist = section["handlers"]
if len(hlist):
hlist = hlist.split(",")
hlist = _strip_spaces(hlist)
@@ -237,13 +221,9 @@ def _install_loggers(cp, handlers, disable_existing):
child_loggers = []
#now set up the new ones...
for log in llist:
- sectname = "logger_%s" % log
- qn = cp.get(sectname, "qualname")
- opts = cp.options(sectname)
- if "propagate" in opts:
- propagate = cp.getint(sectname, "propagate")
- else:
- propagate = 1
+ section = cp["logger_%s" % log]
+ qn = section["qualname"]
+ propagate = section.getint("propagate", fallback=1)
logger = logging.getLogger(qn)
if qn in existing:
i = existing.index(qn)
@@ -255,14 +235,14 @@ def _install_loggers(cp, handlers, disable_existing):
child_loggers.append(existing[i])
i = i + 1
existing.remove(qn)
- if "level" in opts:
- level = cp.get(sectname, "level")
+ if "level" in section:
+ level = section["level"]
logger.setLevel(logging._levelNames[level])
for h in logger.handlers[:]:
logger.removeHandler(h)
logger.propagate = propagate
logger.disabled = 0
- hlist = cp.get(sectname, "handlers")
+ hlist = section["handlers"]
if len(hlist):
hlist = hlist.split(",")
hlist = _strip_spaces(hlist)