22
22
Should work under Python versions >= 1.5.2, except that source line
23
23
information is not available unless 'sys._getframe()' is.
25
Copyright (C) 2001-2007 Vinay Sajip. All Rights Reserved.
25
Copyright (C) 2001-2008 Vinay Sajip. All Rights Reserved.
27
27
To use, simply 'import logging' and log away!
52
52
# _listener holds the server object doing the listening
55
def fileConfig(fname, defaults=None):
55
def fileConfig(fname, defaults=None, disable_existing_loggers=1):
57
57
Read the logging configuration from a ConfigParser-format file.
82
82
del logging._handlerList[:]
83
83
# Handlers add themselves to logging._handlers
84
84
handlers = _install_handlers(cp, formatters)
85
_install_loggers(cp, handlers)
85
_install_loggers(cp, handlers, disable_existing_loggers)
87
87
logging._releaseLock()
101
101
found = getattr(found, n)
104
def _strip_spaces(alist):
105
return map(lambda x: string.strip(x), alist)
105
107
def _create_formatters(cp):
106
108
"""Create and return formatters"""
108
110
if not len(flist):
110
112
flist = string.split(flist, ",")
113
flist = _strip_spaces(flist)
112
115
for form in flist:
113
sectname = "formatter_%s" % string.strip(form)
116
sectname = "formatter_%s" % form
114
117
opts = cp.options(sectname)
115
118
if "format" in opts:
116
119
fs = cp.get(sectname, "format", 1)
136
139
if not len(hlist):
138
141
hlist = string.split(hlist, ",")
142
hlist = _strip_spaces(hlist)
140
144
fixups = [] #for inter-handler references
141
145
for hand in hlist:
142
sectname = "handler_%s" % string.strip(hand)
146
sectname = "handler_%s" % hand
143
147
klass = cp.get(sectname, "class")
144
148
opts = cp.options(sectname)
145
149
if "formatter" in opts:
146
150
fmt = cp.get(sectname, "formatter")
149
klass = eval(klass, vars(logging))
154
klass = eval(klass, vars(logging))
155
except (AttributeError, NameError):
156
klass = _resolve(klass)
150
157
args = cp.get(sectname, "args")
151
158
args = eval(args, vars(logging))
152
h = apply(klass, args)
153
160
if "level" in opts:
154
161
level = cp.get(sectname, "level")
155
162
h.setLevel(logging._levelNames[level])
157
164
h.setFormatter(formatters[fmt])
158
#temporary hack for FileHandler and MemoryHandler.
159
if klass == logging.handlers.MemoryHandler:
165
if issubclass(klass, logging.handlers.MemoryHandler):
160
166
if "target" in opts:
161
167
target = cp.get(sectname,"target")
173
def _install_loggers(cp, handlers):
179
def _install_loggers(cp, handlers, disable_existing_loggers):
174
180
"""Create and install loggers"""
176
182
# configure the root first
190
196
hlist = cp.get(sectname, "handlers")
192
198
hlist = string.split(hlist, ",")
199
hlist = _strip_spaces(hlist)
193
200
for hand in hlist:
194
log.addHandler(handlers[string.strip(hand)])
201
log.addHandler(handlers[hand])
196
203
#and now the others...
197
204
#we don't want to lose the existing loggers,
241
248
hlist = cp.get(sectname, "handlers")
243
250
hlist = string.split(hlist, ",")
251
hlist = _strip_spaces(hlist)
244
252
for hand in hlist:
245
logger.addHandler(handlers[string.strip(hand)])
253
logger.addHandler(handlers[hand])
247
255
#Disable any old loggers. There's no point deleting
248
256
#them as other threads may continue to hold references