annotate Lib/logging/config.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 5fd39b84fc29
children 45ed0cf2c386 6d092a51e187
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43816
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
1 # Copyright 2001-2007 by Vinay Sajip. All Rights Reserved.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Permission to use, copy, modify, and distribute this software and its
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # documentation for any purpose and without fee is hereby granted,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # provided that the above copyright notice appear in all copies and that
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # both that copyright notice and this permission notice appear in
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # supporting documentation, and that the name of Vinay Sajip
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # not be used in advertising or publicity pertaining to distribution
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # of the software without specific, written prior permission.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 """
31597
05e1c27c0a74 Minor documentation changes
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31561
diff changeset
18 Configuration functions for the logging package for Python. The core package
05e1c27c0a74 Minor documentation changes
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31561
diff changeset
19 is based on PEP 282 and comments thereto in comp.lang.python, and influenced
05e1c27c0a74 Minor documentation changes
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31561
diff changeset
20 by Apache's log4j system.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 Should work under Python versions >= 1.5.2, except that source line
31561
6dd38e4586ff Copyright year change.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 28136
diff changeset
23 information is not available unless 'sys._getframe()' is.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48153
diff changeset
25 Copyright (C) 2001-2008 Vinay Sajip. All Rights Reserved.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 To use, simply 'import logging' and log away!
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
39785
dce17b5bd387 Fix missing import of the types module in logging.config.
Georg Brandl <georg@python.org>
parents: 39462
diff changeset
30 import sys, logging, logging.handlers, string, socket, struct, os, traceback, types
34613
a4d13974f6eb Handle errors in imports of thread, threading
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33555
diff changeset
31
a4d13974f6eb Handle errors in imports of thread, threading
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33555
diff changeset
32 try:
a4d13974f6eb Handle errors in imports of thread, threading
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33555
diff changeset
33 import thread
a4d13974f6eb Handle errors in imports of thread, threading
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33555
diff changeset
34 import threading
a4d13974f6eb Handle errors in imports of thread, threading
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33555
diff changeset
35 except ImportError:
a4d13974f6eb Handle errors in imports of thread, threading
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33555
diff changeset
36 thread = None
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
47241
ba1fa511b72d socketserver renaming reversal part 3: move the module into the right
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
38 from SocketServer import ThreadingTCPServer, StreamRequestHandler
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 DEFAULT_LOGGING_CONFIG_PORT = 9030
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
31561
6dd38e4586ff Copyright year change.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 28136
diff changeset
43 if sys.platform == "win32":
6dd38e4586ff Copyright year change.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 28136
diff changeset
44 RESET_ERROR = 10054 #WSAECONNRESET
6dd38e4586ff Copyright year change.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 28136
diff changeset
45 else:
6dd38e4586ff Copyright year change.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 28136
diff changeset
46 RESET_ERROR = 104 #ECONNRESET
6dd38e4586ff Copyright year change.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 28136
diff changeset
47
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 # The following code implements a socket listener for on-the-fly
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # reconfiguration of logging.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 # _listener holds the server object doing the listening
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 _listener = None
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
47774
b418dd39f798 Bug #3136: fileConfig()'s disabling of old loggers is now conditional via an optional disable_existing_loggers parameter, but the default value is such that the old behaviour is preserved.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47252
diff changeset
55 def fileConfig(fname, defaults=None, disable_existing_loggers=1):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 Read the logging configuration from a ConfigParser-format file.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 This can be called several times from an application, allowing an end user
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 the ability to select from various pre-canned configurations (if the
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 developer provides a mechanism to present the choices and load the chosen
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 configuration).
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 In versions of ConfigParser which have the readfp method [typically
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 shipped in 2.x versions of Python], you can pass in a file-like object
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 rather than a filename, in which case the file-like object will be read
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 using readfp.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 """
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47241
diff changeset
68 import ConfigParser
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47241
diff changeset
70 cp = ConfigParser.ConfigParser(defaults)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 if hasattr(cp, 'readfp') and hasattr(fname, 'readline'):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 cp.readfp(fname)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 else:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 cp.read(fname)
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
75
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
76 formatters = _create_formatters(cp)
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
77
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
78 # critical section
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 logging._acquireLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 try:
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
81 logging._handlers.clear()
39462
68bed5348454 Repair logging test spew caused by rev. 51206.
Georg Brandl <georg@python.org>
parents: 38966
diff changeset
82 del logging._handlerList[:]
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
83 # Handlers add themselves to logging._handlers
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
84 handlers = _install_handlers(cp, formatters)
47774
b418dd39f798 Bug #3136: fileConfig()'s disabling of old loggers is now conditional via an optional disable_existing_loggers parameter, but the default value is such that the old behaviour is preserved.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47252
diff changeset
85 _install_loggers(cp, handlers, disable_existing_loggers)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 finally:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 logging._releaseLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
89
36348
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
90 def _resolve(name):
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
91 """Resolve a dotted name to a global object."""
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
92 name = string.split(name, '.')
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
93 used = name.pop(0)
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
94 found = __import__(used)
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
95 for n in name:
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
96 used = used + '.' + n
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
97 try:
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
98 found = getattr(found, n)
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
99 except AttributeError:
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
100 __import__(used)
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
101 found = getattr(found, n)
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
102 return found
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
103
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48153
diff changeset
104 def _strip_spaces(alist):
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48153
diff changeset
105 return map(lambda x: string.strip(x), alist)
36348
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
106
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
107 def _create_formatters(cp):
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
108 """Create and return formatters"""
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
109 flist = cp.get("formatters", "keys")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
110 if not len(flist):
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
111 return {}
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
112 flist = string.split(flist, ",")
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48153
diff changeset
113 flist = _strip_spaces(flist)
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
114 formatters = {}
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
115 for form in flist:
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48153
diff changeset
116 sectname = "formatter_%s" % form
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
117 opts = cp.options(sectname)
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
118 if "format" in opts:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
119 fs = cp.get(sectname, "format", 1)
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
120 else:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
121 fs = None
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
122 if "datefmt" in opts:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
123 dfs = cp.get(sectname, "datefmt", 1)
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
124 else:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
125 dfs = None
36348
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
126 c = logging.Formatter
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
127 if "class" in opts:
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
128 class_name = cp.get(sectname, "class")
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
129 if class_name:
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
130 c = _resolve(class_name)
eb4a47b81942 Added the ability to specify a class attribute in Formatter configuration. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36309
diff changeset
131 f = c(fs, dfs)
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
132 formatters[form] = f
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
133 return formatters
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
134
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
135
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
136 def _install_handlers(cp, formatters):
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
137 """Install and return handlers"""
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
138 hlist = cp.get("handlers", "keys")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
139 if not len(hlist):
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
140 return {}
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
141 hlist = string.split(hlist, ",")
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48153
diff changeset
142 hlist = _strip_spaces(hlist)
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
143 handlers = {}
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
144 fixups = [] #for inter-handler references
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
145 for hand in hlist:
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48153
diff changeset
146 sectname = "handler_%s" % hand
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
147 klass = cp.get(sectname, "class")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
148 opts = cp.options(sectname)
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
149 if "formatter" in opts:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
150 fmt = cp.get(sectname, "formatter")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
151 else:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
152 fmt = ""
48009
0eae9b25a2ed Issue #3389: Allow resolving dotted names for handlers in logging configuration files. Thanks to Philip Jenvey for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47854
diff changeset
153 try:
0eae9b25a2ed Issue #3389: Allow resolving dotted names for handlers in logging configuration files. Thanks to Philip Jenvey for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47854
diff changeset
154 klass = eval(klass, vars(logging))
0eae9b25a2ed Issue #3389: Allow resolving dotted names for handlers in logging configuration files. Thanks to Philip Jenvey for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47854
diff changeset
155 except (AttributeError, NameError):
0eae9b25a2ed Issue #3389: Allow resolving dotted names for handlers in logging configuration files. Thanks to Philip Jenvey for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47854
diff changeset
156 klass = _resolve(klass)
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
157 args = cp.get(sectname, "args")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
158 args = eval(args, vars(logging))
48153
c77ac8bdd3fe Remove dict.has_key() and apply() usage from the logging package to silence
Brett Cannon <bcannon@gmail.com>
parents: 48009
diff changeset
159 h = klass(*args)
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
160 if "level" in opts:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
161 level = cp.get(sectname, "level")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
162 h.setLevel(logging._levelNames[level])
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
163 if len(fmt):
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
164 h.setFormatter(formatters[fmt])
47854
73cbcde75e05 Removed out-of-date comment in _install_handlers and
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47774
diff changeset
165 if issubclass(klass, logging.handlers.MemoryHandler):
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
166 if "target" in opts:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
167 target = cp.get(sectname,"target")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
168 else:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
169 target = ""
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
170 if len(target): #the target handler may not be loaded yet, so keep for later...
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
171 fixups.append((h, target))
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
172 handlers[hand] = h
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
173 #now all handlers are loaded, fixup inter-handler references...
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
174 for h, t in fixups:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
175 h.setTarget(handlers[t])
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
176 return handlers
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
177
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
178
47774
b418dd39f798 Bug #3136: fileConfig()'s disabling of old loggers is now conditional via an optional disable_existing_loggers parameter, but the default value is such that the old behaviour is preserved.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47252
diff changeset
179 def _install_loggers(cp, handlers, disable_existing_loggers):
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
180 """Create and install loggers"""
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
181
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
182 # configure the root first
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
183 llist = cp.get("loggers", "keys")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
184 llist = string.split(llist, ",")
40447
315c5a633b05 Patch by "cuppatea" (SF #1503765)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 39785
diff changeset
185 llist = map(lambda x: string.strip(x), llist)
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
186 llist.remove("root")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
187 sectname = "logger_root"
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
188 root = logging.root
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
189 log = root
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
190 opts = cp.options(sectname)
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
191 if "level" in opts:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
192 level = cp.get(sectname, "level")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
193 log.setLevel(logging._levelNames[level])
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
194 for h in root.handlers[:]:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
195 root.removeHandler(h)
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
196 hlist = cp.get(sectname, "handlers")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
197 if len(hlist):
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
198 hlist = string.split(hlist, ",")
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48153
diff changeset
199 hlist = _strip_spaces(hlist)
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
200 for hand in hlist:
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48153
diff changeset
201 log.addHandler(handlers[hand])
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
202
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
203 #and now the others...
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
204 #we don't want to lose the existing loggers,
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
205 #since other threads may have pointers to them.
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
206 #existing is set to contain all existing loggers,
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
207 #and as we go through the new configuration we
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
208 #remove any which are configured. At the end,
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
209 #what's left in existing is the set of loggers
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
210 #which were in the previous configuration but
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
211 #which are not in the new configuration.
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
212 existing = root.manager.loggerDict.keys()
43816
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
213 #The list needs to be sorted so that we can
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
214 #avoid disabling child loggers of explicitly
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
215 #named loggers. With a sorted list it is easier
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
216 #to find the child loggers.
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
217 existing.sort()
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
218 #We'll keep the list of existing loggers
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
219 #which are children of named loggers here...
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
220 child_loggers = []
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
221 #now set up the new ones...
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
222 for log in llist:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
223 sectname = "logger_%s" % log
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
224 qn = cp.get(sectname, "qualname")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
225 opts = cp.options(sectname)
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
226 if "propagate" in opts:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
227 propagate = cp.getint(sectname, "propagate")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
228 else:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
229 propagate = 1
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
230 logger = logging.getLogger(qn)
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
231 if qn in existing:
43816
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
232 i = existing.index(qn)
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
233 prefixed = qn + "."
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
234 pflen = len(prefixed)
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
235 num_existing = len(existing)
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
236 i = i + 1 # look at the entry after qn
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
237 while (i < num_existing) and (existing[i][:pflen] == prefixed):
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
238 child_loggers.append(existing[i])
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
239 i = i + 1
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
240 existing.remove(qn)
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
241 if "level" in opts:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
242 level = cp.get(sectname, "level")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
243 logger.setLevel(logging._levelNames[level])
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
244 for h in logger.handlers[:]:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
245 logger.removeHandler(h)
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
246 logger.propagate = propagate
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
247 logger.disabled = 0
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
248 hlist = cp.get(sectname, "handlers")
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
249 if len(hlist):
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
250 hlist = string.split(hlist, ",")
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48153
diff changeset
251 hlist = _strip_spaces(hlist)
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
252 for hand in hlist:
48980
5fd39b84fc29 Issue #3726: Allowed spaces in separators in logging configuration files.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48153
diff changeset
253 logger.addHandler(handlers[hand])
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
254
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
255 #Disable any old loggers. There's no point deleting
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
256 #them as other threads may continue to hold references
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
257 #and by disabling them, you stop them doing any logging.
43816
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
258 #However, don't disable children of named loggers, as that's
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
259 #probably not what was intended by the user.
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
260 for log in existing:
43816
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
261 logger = root.manager.loggerDict[log]
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
262 if log in child_loggers:
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
263 logger.level = logging.NOTSET
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
264 logger.handlers = []
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
265 logger.propagate = 1
47774
b418dd39f798 Bug #3136: fileConfig()'s disabling of old loggers is now conditional via an optional disable_existing_loggers parameter, but the default value is such that the old behaviour is preserved.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 47252
diff changeset
266 elif disable_existing_loggers:
43816
487f3737eaef Fixed a bug reported (in private email, by Robert Crida) in logging configuration whereby child loggers of a logger named in a configuration file, which are not themselves named in the configuration, are disabled when the configuration is applied.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40447
diff changeset
267 logger.disabled = 1
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
268
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
269
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 def listen(port=DEFAULT_LOGGING_CONFIG_PORT):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 Start up a socket server on the specified port, and listen for new
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 configurations.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 These will be sent as a file suitable for processing by fileConfig().
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 Returns a Thread object on which you can call start() to start the server,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 and which you can join() when appropriate. To stop the server, call
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 stopListening().
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 if not thread:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 raise NotImplementedError, "listen() needs threading to work"
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 class ConfigStreamHandler(StreamRequestHandler):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 Handler for a logging configuration request.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 It expects a completely new logging configuration and uses fileConfig
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 to install it.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 def handle(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 Handle a request.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293
35024
fcf010c370cb Documentation clarified re. config socket listener protocol
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34746
diff changeset
294 Each request is expected to be a 4-byte length, packed using
fcf010c370cb Documentation clarified re. config socket listener protocol
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34746
diff changeset
295 struct.pack(">L", n), followed by the config file.
fcf010c370cb Documentation clarified re. config socket listener protocol
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34746
diff changeset
296 Uses fileConfig() to do the grunt work.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 import tempfile
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 try:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 conn = self.connection
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 chunk = conn.recv(4)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 if len(chunk) == 4:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 slen = struct.unpack(">L", chunk)[0]
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 chunk = self.connection.recv(slen)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 while len(chunk) < slen:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 chunk = chunk + conn.recv(slen - len(chunk))
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 #Apply new configuration. We'd like to be able to
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 #create a StringIO and pass that in, but unfortunately
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 #1.5.2 ConfigParser does not support reading file
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 #objects, only actual files. So we create a temporary
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 #file and remove it later.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 file = tempfile.mktemp(".ini")
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 f = open(file, "w")
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 f.write(chunk)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 f.close()
36309
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
316 try:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
317 fileConfig(file)
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
318 except (KeyboardInterrupt, SystemExit):
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
319 raise
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
320 except:
4dba61e49037 Refactoring for fileConfig. Contributed by Shane Hathaway.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35024
diff changeset
321 traceback.print_exc()
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 os.remove(file)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 except socket.error, e:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 if type(e.args) != types.TupleType:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 raise
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 else:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 errcode = e.args[0]
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 if errcode != RESET_ERROR:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 raise
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 class ConfigSocketReceiver(ThreadingTCPServer):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 A simple TCP socket-based logging config receiver.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
335
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 allow_reuse_address = 1
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
337
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 def __init__(self, host='localhost', port=DEFAULT_LOGGING_CONFIG_PORT,
26258
b19443bdf447 SF Patch #638825
Neal Norwitz <nnorwitz@gmail.com>
parents: 26218
diff changeset
339 handler=None):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 ThreadingTCPServer.__init__(self, (host, port), handler)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 logging._acquireLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 self.abort = 0
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 logging._releaseLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 self.timeout = 1
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 def serve_until_stopped(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 import select
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 abort = 0
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 while not abort:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 rd, wr, ex = select.select([self.socket.fileno()],
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 [], [],
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 self.timeout)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 if rd:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 self.handle_request()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 logging._acquireLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 abort = self.abort
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 logging._releaseLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
358
26258
b19443bdf447 SF Patch #638825
Neal Norwitz <nnorwitz@gmail.com>
parents: 26218
diff changeset
359 def serve(rcvr, hdlr, port):
b19443bdf447 SF Patch #638825
Neal Norwitz <nnorwitz@gmail.com>
parents: 26218
diff changeset
360 server = rcvr(port=port, handler=hdlr)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 global _listener
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 logging._acquireLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 _listener = server
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 logging._releaseLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 server.serve_until_stopped()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
366
26258
b19443bdf447 SF Patch #638825
Neal Norwitz <nnorwitz@gmail.com>
parents: 26218
diff changeset
367 return threading.Thread(target=serve,
b19443bdf447 SF Patch #638825
Neal Norwitz <nnorwitz@gmail.com>
parents: 26218
diff changeset
368 args=(ConfigSocketReceiver,
b19443bdf447 SF Patch #638825
Neal Norwitz <nnorwitz@gmail.com>
parents: 26218
diff changeset
369 ConfigStreamHandler, port))
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 def stopListening():
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 Stop the listening server which was created with a call to listen().
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 """
26258
b19443bdf447 SF Patch #638825
Neal Norwitz <nnorwitz@gmail.com>
parents: 26218
diff changeset
375 global _listener
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 if _listener:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 logging._acquireLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 _listener.abort = 1
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 _listener = None
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 logging._releaseLock()