1 # Copyright 2001-2005 by Vinay Sajip. All Rights Reserved.
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose and without fee is hereby granted,
5 # provided that the above copyright notice appear in all copies and that
6 # both that copyright notice and this permission notice appear in
7 # supporting documentation, and that the name of Vinay Sajip
8 # not be used in advertising or publicity pertaining to distribution
9 # of the software without specific, written prior permission.
10 # VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
11 # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
12 # VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
13 # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
14 # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 Configuration functions for the logging package for Python. The core package
19 is based on PEP 282 and comments thereto in comp.lang.python, and influenced
20 by Apache's log4j system.
22 Should work under Python versions >= 1.5.2, except that source line
23 information is not available unless 'sys._getframe()' is.
25 Copyright (C) 2001-2004 Vinay Sajip. All Rights Reserved.
27 To use, simply 'import logging' and log away!
30 import sys
, logging
, logging
.handlers
, string
, socket
, struct
, os
, traceback
, types
38 from SocketServer
import ThreadingTCPServer
, StreamRequestHandler
41 DEFAULT_LOGGING_CONFIG_PORT
= 9030
43 if sys
.platform
== "win32":
44 RESET_ERROR
= 10054 #WSAECONNRESET
46 RESET_ERROR
= 104 #ECONNRESET
49 # The following code implements a socket listener for on-the-fly
50 # reconfiguration of logging.
52 # _listener holds the server object doing the listening
55 def fileConfig(fname
, defaults
=None):
57 Read the logging configuration from a ConfigParser-format file.
59 This can be called several times from an application, allowing an end user
60 the ability to select from various pre-canned configurations (if the
61 developer provides a mechanism to present the choices and load the chosen
63 In versions of ConfigParser which have the readfp method [typically
64 shipped in 2.x versions of Python], you can pass in a file-like object
65 rather than a filename, in which case the file-like object will be read
70 cp
= ConfigParser
.ConfigParser(defaults
)
71 if hasattr(cp
, 'readfp') and hasattr(fname
, 'readline'):
76 formatters
= _create_formatters(cp
)
79 logging
._acquireLock
()
81 logging
._handlers
.clear()
82 del logging
._handlerList
[:]
83 # Handlers add themselves to logging._handlers
84 handlers
= _install_handlers(cp
, formatters
)
85 _install_loggers(cp
, handlers
)
87 logging
._releaseLock
()
91 """Resolve a dotted name to a global object."""
92 name
= string
.split(name
, '.')
94 found
= __import__(used
)
98 found
= getattr(found
, n
)
99 except AttributeError:
101 found
= getattr(found
, n
)
105 def _create_formatters(cp
):
106 """Create and return formatters"""
107 flist
= cp
.get("formatters", "keys")
110 flist
= string
.split(flist
, ",")
113 sectname
= "formatter_%s" % string
.strip(form
)
114 opts
= cp
.options(sectname
)
116 fs
= cp
.get(sectname
, "format", 1)
119 if "datefmt" in opts
:
120 dfs
= cp
.get(sectname
, "datefmt", 1)
123 c
= logging
.Formatter
125 class_name
= cp
.get(sectname
, "class")
127 c
= _resolve(class_name
)
133 def _install_handlers(cp
, formatters
):
134 """Install and return handlers"""
135 hlist
= cp
.get("handlers", "keys")
138 hlist
= string
.split(hlist
, ",")
140 fixups
= [] #for inter-handler references
142 sectname
= "handler_%s" % string
.strip(hand
)
143 klass
= cp
.get(sectname
, "class")
144 opts
= cp
.options(sectname
)
145 if "formatter" in opts
:
146 fmt
= cp
.get(sectname
, "formatter")
149 klass
= eval(klass
, vars(logging
))
150 args
= cp
.get(sectname
, "args")
151 args
= eval(args
, vars(logging
))
152 h
= apply(klass
, args
)
154 level
= cp
.get(sectname
, "level")
155 h
.setLevel(logging
._levelNames
[level
])
157 h
.setFormatter(formatters
[fmt
])
158 #temporary hack for FileHandler and MemoryHandler.
159 if klass
== logging
.handlers
.MemoryHandler
:
161 target
= cp
.get(sectname
,"target")
164 if len(target
): #the target handler may not be loaded yet, so keep for later...
165 fixups
.append((h
, target
))
167 #now all handlers are loaded, fixup inter-handler references...
169 h
.setTarget(handlers
[t
])
173 def _install_loggers(cp
, handlers
):
174 """Create and install loggers"""
176 # configure the root first
177 llist
= cp
.get("loggers", "keys")
178 llist
= string
.split(llist
, ",")
179 llist
= map(lambda x
: string
.strip(x
), llist
)
181 sectname
= "logger_root"
184 opts
= cp
.options(sectname
)
186 level
= cp
.get(sectname
, "level")
187 log
.setLevel(logging
._levelNames
[level
])
188 for h
in root
.handlers
[:]:
189 root
.removeHandler(h
)
190 hlist
= cp
.get(sectname
, "handlers")
192 hlist
= string
.split(hlist
, ",")
194 log
.addHandler(handlers
[string
.strip(hand
)])
196 #and now the others...
197 #we don't want to lose the existing loggers,
198 #since other threads may have pointers to them.
199 #existing is set to contain all existing loggers,
200 #and as we go through the new configuration we
201 #remove any which are configured. At the end,
202 #what's left in existing is the set of loggers
203 #which were in the previous configuration but
204 #which are not in the new configuration.
205 existing
= root
.manager
.loggerDict
.keys()
206 #now set up the new ones...
208 sectname
= "logger_%s" % log
209 qn
= cp
.get(sectname
, "qualname")
210 opts
= cp
.options(sectname
)
211 if "propagate" in opts
:
212 propagate
= cp
.getint(sectname
, "propagate")
215 logger
= logging
.getLogger(qn
)
219 level
= cp
.get(sectname
, "level")
220 logger
.setLevel(logging
._levelNames
[level
])
221 for h
in logger
.handlers
[:]:
222 logger
.removeHandler(h
)
223 logger
.propagate
= propagate
225 hlist
= cp
.get(sectname
, "handlers")
227 hlist
= string
.split(hlist
, ",")
229 logger
.addHandler(handlers
[string
.strip(hand
)])
231 #Disable any old loggers. There's no point deleting
232 #them as other threads may continue to hold references
233 #and by disabling them, you stop them doing any logging.
235 root
.manager
.loggerDict
[log
].disabled
= 1
238 def listen(port
=DEFAULT_LOGGING_CONFIG_PORT
):
240 Start up a socket server on the specified port, and listen for new
243 These will be sent as a file suitable for processing by fileConfig().
244 Returns a Thread object on which you can call start() to start the server,
245 and which you can join() when appropriate. To stop the server, call
249 raise NotImplementedError, "listen() needs threading to work"
251 class ConfigStreamHandler(StreamRequestHandler
):
253 Handler for a logging configuration request.
255 It expects a completely new logging configuration and uses fileConfig
262 Each request is expected to be a 4-byte length, packed using
263 struct.pack(">L", n), followed by the config file.
264 Uses fileConfig() to do the grunt work.
268 conn
= self
.connection
271 slen
= struct
.unpack(">L", chunk
)[0]
272 chunk
= self
.connection
.recv(slen
)
273 while len(chunk
) < slen
:
274 chunk
= chunk
+ conn
.recv(slen
- len(chunk
))
275 #Apply new configuration. We'd like to be able to
276 #create a StringIO and pass that in, but unfortunately
277 #1.5.2 ConfigParser does not support reading file
278 #objects, only actual files. So we create a temporary
279 #file and remove it later.
280 file = tempfile
.mktemp(".ini")
286 except (KeyboardInterrupt, SystemExit):
289 traceback
.print_exc()
291 except socket
.error
, e
:
292 if type(e
.args
) != types
.TupleType
:
296 if errcode
!= RESET_ERROR
:
299 class ConfigSocketReceiver(ThreadingTCPServer
):
301 A simple TCP socket-based logging config receiver.
304 allow_reuse_address
= 1
306 def __init__(self
, host
='localhost', port
=DEFAULT_LOGGING_CONFIG_PORT
,
308 ThreadingTCPServer
.__init
__(self
, (host
, port
), handler
)
309 logging
._acquireLock
()
311 logging
._releaseLock
()
314 def serve_until_stopped(self
):
318 rd
, wr
, ex
= select
.select([self
.socket
.fileno()],
322 self
.handle_request()
323 logging
._acquireLock
()
325 logging
._releaseLock
()
327 def serve(rcvr
, hdlr
, port
):
328 server
= rcvr(port
=port
, handler
=hdlr
)
330 logging
._acquireLock
()
332 logging
._releaseLock
()
333 server
.serve_until_stopped()
335 return threading
.Thread(target
=serve
,
336 args
=(ConfigSocketReceiver
,
337 ConfigStreamHandler
, port
))
341 Stop the listening server which was created with a call to listen().
345 logging
._acquireLock
()
348 logging
._releaseLock
()