annotate Lib/logging/__init__.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 8ba6d8fc9740
children 8c2635afbfe1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
75209
1316d2af2331 logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 74537
diff changeset
1 # Copyright 2001-2012 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 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 Logging package for Python. Based on PEP 282 and comments thereto in
75210
b5cb819404aa Updated comments.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75209
diff changeset
19 comp.lang.python.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
75210
b5cb819404aa Updated comments.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75209
diff changeset
21 Copyright (C) 2001-2012 Vinay Sajip. All Rights Reserved.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 To use, simply 'import logging' and log away!
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
57445
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
26 import sys, os, time, cStringIO, traceback, warnings, weakref
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
27
45370
71139b692439 Add __all__ to logging module.
Raymond Hettinger <python@rcn.com>
parents: 44907
diff changeset
28 __all__ = ['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR',
53504
293ada1dbfda Issue #5854: Updated __all__ to include some missing names and remove some names which should not be exported.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53311
diff changeset
29 'FATAL', 'FileHandler', 'Filter', 'Formatter', 'Handler', 'INFO',
53503
973d24f44b07 Issue #5854: Updated __all__ to include some missing names and remove some names which should not be exported.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53310
diff changeset
30 'LogRecord', 'Logger', 'LoggerAdapter', 'NOTSET', 'NullHandler',
53504
293ada1dbfda Issue #5854: Updated __all__ to include some missing names and remove some names which should not be exported.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53311
diff changeset
31 'StreamHandler', 'WARN', 'WARNING', 'addLevelName', 'basicConfig',
53503
973d24f44b07 Issue #5854: Updated __all__ to include some missing names and remove some names which should not be exported.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53310
diff changeset
32 'captureWarnings', 'critical', 'debug', 'disable', 'error',
53504
293ada1dbfda Issue #5854: Updated __all__ to include some missing names and remove some names which should not be exported.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53311
diff changeset
33 'exception', 'fatal', 'getLevelName', 'getLogger', 'getLoggerClass',
293ada1dbfda Issue #5854: Updated __all__ to include some missing names and remove some names which should not be exported.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53311
diff changeset
34 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'warn', 'warning']
45370
71139b692439 Add __all__ to logging module.
Raymond Hettinger <python@rcn.com>
parents: 44907
diff changeset
35
34744
3d97eb3b7cec Added optional encoding argument to file handlers. Made traceback import unconditional, to avoid lock contention problems which occur when logging from custom importers (SF patch #1158052)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34612
diff changeset
36 try:
3d97eb3b7cec Added optional encoding argument to file handlers. Made traceback import unconditional, to avoid lock contention problems which occur when logging from custom importers (SF patch #1158052)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34612
diff changeset
37 import codecs
3d97eb3b7cec Added optional encoding argument to file handlers. Made traceback import unconditional, to avoid lock contention problems which occur when logging from custom importers (SF patch #1158052)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34612
diff changeset
38 except ImportError:
3d97eb3b7cec Added optional encoding argument to file handlers. Made traceback import unconditional, to avoid lock contention problems which occur when logging from custom importers (SF patch #1158052)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34612
diff changeset
39 codecs = None
26183
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 try:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 import thread
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 import threading
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 except ImportError:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 thread = None
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 __author__ = "Vinay Sajip <vinay_sajip@red-dove.com>"
38214
4cb9dc0dc3f9 Changed status from "beta" to "production"; since logging has been part of the stdlib since 2.3, it should be safe to make this assertion ;-)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 37610
diff changeset
48 __status__ = "production"
58779
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
49 __version__ = "0.5.1.2"
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
50 __date__ = "07 February 2010"
26183
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 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 # Miscellaneous module data
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 #---------------------------------------------------------------------------
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
55 try:
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
56 unicode
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
57 _unicode = True
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
58 except NameError:
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
59 _unicode = False
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 #
34612
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
62 # _srcfile is used when walking the stack to check when we've got the first
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 # caller stack frame.
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
64 #
35512
9516ba54b9df Changed _srcfile determination to support py2exe.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34858
diff changeset
65 if hasattr(sys, 'frozen'): #support for py2exe
9516ba54b9df Changed _srcfile determination to support py2exe.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34858
diff changeset
66 _srcfile = "logging%s__init__%s" % (os.sep, __file__[-4:])
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
67 elif __file__[-4:].lower() in ['.pyc', '.pyo']:
26184
3e59d674b8c3 Get rid of #! lines, references to usage as __main__, README.txt, and
Guido van Rossum <guido@python.org>
parents: 26183
diff changeset
68 _srcfile = __file__[:-4] + '.py'
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 else:
26184
3e59d674b8c3 Get rid of #! lines, references to usage as __main__, README.txt, and
Guido van Rossum <guido@python.org>
parents: 26183
diff changeset
70 _srcfile = __file__
3e59d674b8c3 Get rid of #! lines, references to usage as __main__, README.txt, and
Guido van Rossum <guido@python.org>
parents: 26183
diff changeset
71 _srcfile = os.path.normcase(_srcfile)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
34612
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
73 # next bit filched from 1.5.2's inspect.py
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
74 def currentframe():
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
75 """Return the frame object for the caller's stack frame."""
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
76 try:
35749
17828a00a454 Don't use a string exception since it's deprecated
Neal Norwitz <nnorwitz@gmail.com>
parents: 35721
diff changeset
77 raise Exception
34612
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
78 except:
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
79 return sys.exc_info()[2].tb_frame.f_back
34612
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
80
40860
d795a55e9be4 Minor fix for currentframe (SF #1652788).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40571
diff changeset
81 if hasattr(sys, '_getframe'): currentframe = lambda: sys._getframe(3)
34612
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
82 # done filching
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
83
27349
ddb52f5673db Use lightweight introspection instead of the inspect module hammer.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26257
diff changeset
84 # _srcfile is only used in conjunction with sys._getframe().
ddb52f5673db Use lightweight introspection instead of the inspect module hammer.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26257
diff changeset
85 # To provide compatibility with older versions of Python, set _srcfile
ddb52f5673db Use lightweight introspection instead of the inspect module hammer.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26257
diff changeset
86 # to None if _getframe() is not available; this value will prevent
ddb52f5673db Use lightweight introspection instead of the inspect module hammer.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26257
diff changeset
87 # findCaller() from being called.
34612
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
88 #if not hasattr(sys, "_getframe"):
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
89 # _srcfile = None
27349
ddb52f5673db Use lightweight introspection instead of the inspect module hammer.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26257
diff changeset
90
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 #_startTime is used as the base when calculating the relative time of events
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 _startTime = time.time()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 #raiseExceptions is used to see if exceptions during handling should be
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 #propagated
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 raiseExceptions = 1
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
36974
29e6e7a107de Added logThreads and logProcesses to allow conditional omission of logging this information
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36469
diff changeset
102 #
29e6e7a107de Added logThreads and logProcesses to allow conditional omission of logging this information
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36469
diff changeset
103 # If you don't want threading information in the log, set this to zero
29e6e7a107de Added logThreads and logProcesses to allow conditional omission of logging this information
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36469
diff changeset
104 #
29e6e7a107de Added logThreads and logProcesses to allow conditional omission of logging this information
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36469
diff changeset
105 logThreads = 1
29e6e7a107de Added logThreads and logProcesses to allow conditional omission of logging this information
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36469
diff changeset
106
29e6e7a107de Added logThreads and logProcesses to allow conditional omission of logging this information
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36469
diff changeset
107 #
52148
632aa568474c Merged revisions 68737 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51693
diff changeset
108 # If you don't want multiprocessing information in the log, set this to zero
632aa568474c Merged revisions 68737 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51693
diff changeset
109 #
632aa568474c Merged revisions 68737 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51693
diff changeset
110 logMultiprocessing = 1
632aa568474c Merged revisions 68737 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51693
diff changeset
111
632aa568474c Merged revisions 68737 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51693
diff changeset
112 #
36974
29e6e7a107de Added logThreads and logProcesses to allow conditional omission of logging this information
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36469
diff changeset
113 # If you don't want process information in the log, set this to zero
29e6e7a107de Added logThreads and logProcesses to allow conditional omission of logging this information
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36469
diff changeset
114 #
29e6e7a107de Added logThreads and logProcesses to allow conditional omission of logging this information
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36469
diff changeset
115 logProcesses = 1
29e6e7a107de Added logThreads and logProcesses to allow conditional omission of logging this information
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36469
diff changeset
116
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 # Level related stuff
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 # Default levels and level names, these can be replaced with any positive set
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 # of values having corresponding names. There is a pseudo-level, NOTSET, which
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 # is only really there as a lower limit for user-defined levels. Handlers and
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 # loggers are initialized with NOTSET so that they will log all messages, even
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 # at user-defined levels.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 #
34744
3d97eb3b7cec Added optional encoding argument to file handlers. Made traceback import unconditional, to avoid lock contention problems which occur when logging from custom importers (SF patch #1158052)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34612
diff changeset
127
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 CRITICAL = 50
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 FATAL = CRITICAL
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 ERROR = 40
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
131 WARNING = 30
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
132 WARN = WARNING
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 INFO = 20
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 DEBUG = 10
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 NOTSET = 0
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 _levelNames = {
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 CRITICAL : 'CRITICAL',
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 ERROR : 'ERROR',
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
140 WARNING : 'WARNING',
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 INFO : 'INFO',
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 DEBUG : 'DEBUG',
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 NOTSET : 'NOTSET',
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 'CRITICAL' : CRITICAL,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 'ERROR' : ERROR,
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
146 'WARN' : WARNING,
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
147 'WARNING' : WARNING,
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 'INFO' : INFO,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 'DEBUG' : DEBUG,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 'NOTSET' : NOTSET,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 }
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 def getLevelName(level):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 Return the textual representation of logging level 'level'.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
157 If the level is one of the predefined levels (CRITICAL, ERROR, WARNING,
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 INFO, DEBUG) then you get the corresponding string. If you have
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 associated levels with names using addLevelName then the name you have
32479
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
160 associated with 'level' is returned.
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
161
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
162 If a numeric value corresponding to one of the defined levels is passed
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
163 in, the corresponding string representation is returned.
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
164
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
165 Otherwise, the string "Level %s" % level is returned.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 return _levelNames.get(level, ("Level %s" % level))
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 def addLevelName(level, levelName):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 Associate 'levelName' with 'level'.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 This is used when converting levels to text during message formatting.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 _acquireLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 try: #unlikely to cause an exception, but you never know...
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 _levelNames[level] = levelName
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 _levelNames[levelName] = level
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 finally:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 _releaseLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
55241
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
182 def _checkLevel(level):
78801
8ba6d8fc9740 Closes #15710: accept long in _checkLevel.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 78424
diff changeset
183 if isinstance(level, (int, long)):
55241
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
184 rv = level
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
185 elif str(level) == level:
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
186 if level not in _levelNames:
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
187 raise ValueError("Unknown level: %r" % level)
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
188 rv = _levelNames[level]
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
189 else:
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
190 raise TypeError("Level not an integer or a valid string: %r" % level)
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
191 return rv
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
192
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 # Thread-related stuff
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 #_lock is used to serialize access to shared data structures in this module.
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
199 #This needs to be an RLock because fileConfig() creates and configures
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
200 #Handlers, and so might arbitrary user threads. Since Handler code updates the
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
201 #shared dictionary _handlers, it needs to acquire the lock. But if configuring,
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 #the lock would already have been acquired - so we need an RLock.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 #The same argument applies to Loggers and Manager.loggerDict.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 #
57478
527b7c075849 Issue #7403: logging: Fixed possible race condition in lock creation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 45782
diff changeset
205 if thread:
527b7c075849 Issue #7403: logging: Fixed possible race condition in lock creation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 45782
diff changeset
206 _lock = threading.RLock()
527b7c075849 Issue #7403: logging: Fixed possible race condition in lock creation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 45782
diff changeset
207 else:
527b7c075849 Issue #7403: logging: Fixed possible race condition in lock creation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 45782
diff changeset
208 _lock = None
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 def _acquireLock():
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 Acquire the module-level lock for serializing access to shared data.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 This should be released with _releaseLock().
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 if _lock:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 _lock.acquire()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 def _releaseLock():
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 Release the module-level lock acquired by calling _acquireLock().
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 if _lock:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 _lock.release()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 # The logging record
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
230 class LogRecord(object):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 A LogRecord instance represents an event being logged.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 LogRecord instances are created every time something is logged. They
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 contain all the information pertinent to the event being logged. The
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 main information passed in is in msg and args, which are combined
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 using str(msg) % args to create the message field of the record. The
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 record also includes information such as when the record was created,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 the source line where the logging call was made, and any exception
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 information to be logged.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 """
36469
93efa59b3297 Added function name to LogRecord.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36468
diff changeset
242 def __init__(self, name, level, pathname, lineno,
40571
8da6dda679f8 Backported changes from trunk:
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 39439
diff changeset
243 msg, args, exc_info, func=None):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 Initialize a logging record with interesting information.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 ct = time.time()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 self.name = name
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 self.msg = msg
33807
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
250 #
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
251 # The following statement allows passing of a dictionary as a sole
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
252 # argument, so that you can do something like
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
253 # logging.debug("a %(a)d b %(b)s", {'a':1, 'b':2})
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
254 # Suggested by Stefan Behnel.
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
255 # Note that without the test for args[0], we get a problem because
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
256 # during formatting, we test to see if the arg is present using
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
257 # 'if self.args:'. If the event being logged is e.g. 'Value is %d'
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
258 # and if the passed arg fails 'if self.args:' then no formatting
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
259 # is done. For example, logger.warn('Value is %d', 0) would log
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
260 # 'Value is %d' instead of 'Value is 0'.
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
261 # For the use case of passing a dictionary, this should not be a
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
262 # problem.
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
263 if args and len(args) == 1 and isinstance(args[0], dict) and args[0]:
33807
47b384519775 Changed handling of args in LogRecord.__init__.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33668
diff changeset
264 args = args[0]
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 self.args = args
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 self.levelname = getLevelName(level)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 self.levelno = level
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 self.pathname = pathname
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 try:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 self.filename = os.path.basename(pathname)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 self.module = os.path.splitext(self.filename)[0]
40575
1ed3ed9af348 Bare except clause removed from LogRecord.__init__. Now, only ValueError, TypeError and AttributeError are trapped.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40275
diff changeset
272 except (TypeError, ValueError, AttributeError):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 self.filename = pathname
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 self.module = "Unknown module"
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 self.exc_info = exc_info
31563
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
276 self.exc_text = None # used to cache the traceback text
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 self.lineno = lineno
36469
93efa59b3297 Added function name to LogRecord.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36468
diff changeset
278 self.funcName = func
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 self.created = ct
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 self.msecs = (ct - long(ct)) * 1000
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 self.relativeCreated = (self.created - _startTime) * 1000
36974
29e6e7a107de Added logThreads and logProcesses to allow conditional omission of logging this information
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36469
diff changeset
282 if logThreads and thread:
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 self.thread = thread.get_ident()
48289
024b2b984866 change a few uses of the threading APIs
Benjamin Peterson <benjamin@python.org>
parents: 48153
diff changeset
284 self.threadName = threading.current_thread().name
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 else:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 self.thread = None
34858
2ea3d6915204 Added threadName and now using re-entrant lock
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34744
diff changeset
287 self.threadName = None
56281
a8d0e764e34b Issue #7014: logging: Improved IronPython 2.6 compatibility.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53946
diff changeset
288 if not logMultiprocessing:
a8d0e764e34b Issue #7014: logging: Improved IronPython 2.6 compatibility.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53946
diff changeset
289 self.processName = None
59896
5b3a601ed205 Issue #8200: logging: Handle errors when multiprocessing is not fully loaded when logging occurs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59666
diff changeset
290 else:
56533
9044f64b10ae Issue #7120: logging: Removed import of multiprocessing which is causing crash in GAE.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56365
diff changeset
291 self.processName = 'MainProcess'
59896
5b3a601ed205 Issue #8200: logging: Handle errors when multiprocessing is not fully loaded when logging occurs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59666
diff changeset
292 mp = sys.modules.get('multiprocessing')
5b3a601ed205 Issue #8200: logging: Handle errors when multiprocessing is not fully loaded when logging occurs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59666
diff changeset
293 if mp is not None:
5b3a601ed205 Issue #8200: logging: Handle errors when multiprocessing is not fully loaded when logging occurs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59666
diff changeset
294 # Errors may occur if multiprocessing has not finished loading
5b3a601ed205 Issue #8200: logging: Handle errors when multiprocessing is not fully loaded when logging occurs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59666
diff changeset
295 # yet - e.g. if a custom import hook causes third-party code
5b3a601ed205 Issue #8200: logging: Handle errors when multiprocessing is not fully loaded when logging occurs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59666
diff changeset
296 # to run when multiprocessing calls import. See issue 8200
5b3a601ed205 Issue #8200: logging: Handle errors when multiprocessing is not fully loaded when logging occurs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59666
diff changeset
297 # for an example
5b3a601ed205 Issue #8200: logging: Handle errors when multiprocessing is not fully loaded when logging occurs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59666
diff changeset
298 try:
5b3a601ed205 Issue #8200: logging: Handle errors when multiprocessing is not fully loaded when logging occurs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59666
diff changeset
299 self.processName = mp.current_process().name
5b3a601ed205 Issue #8200: logging: Handle errors when multiprocessing is not fully loaded when logging occurs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59666
diff changeset
300 except StandardError:
5b3a601ed205 Issue #8200: logging: Handle errors when multiprocessing is not fully loaded when logging occurs.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59666
diff changeset
301 pass
36974
29e6e7a107de Added logThreads and logProcesses to allow conditional omission of logging this information
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36469
diff changeset
302 if logProcesses and hasattr(os, 'getpid'):
28019
32a0c2f422f1 getpid doesn't exist on MacOS9.
Jack Jansen <jack.jansen@cwi.nl>
parents: 27932
diff changeset
303 self.process = os.getpid()
32a0c2f422f1 getpid doesn't exist on MacOS9.
Jack Jansen <jack.jansen@cwi.nl>
parents: 27932
diff changeset
304 else:
32a0c2f422f1 getpid doesn't exist on MacOS9.
Jack Jansen <jack.jansen@cwi.nl>
parents: 27932
diff changeset
305 self.process = None
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 def __str__(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 return '<LogRecord: %s, %s, %s, %s, "%s">'%(self.name, self.levelno,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 self.pathname, self.lineno, self.msg)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 def getMessage(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 Return the message for this LogRecord.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 Return the message for this LogRecord after merging any user-supplied
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 arguments with the message.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 """
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
318 if not _unicode: #if no unicode support...
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
319 msg = str(self.msg)
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
320 else:
35703
76635ab7b707 Fixed bug where the logging message was wrongly being demoted from Unicode to string (SF #1314107)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35582
diff changeset
321 msg = self.msg
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
322 if not isinstance(msg, basestring):
35703
76635ab7b707 Fixed bug where the logging message was wrongly being demoted from Unicode to string (SF #1314107)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35582
diff changeset
323 try:
76635ab7b707 Fixed bug where the logging message was wrongly being demoted from Unicode to string (SF #1314107)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35582
diff changeset
324 msg = str(self.msg)
76635ab7b707 Fixed bug where the logging message was wrongly being demoted from Unicode to string (SF #1314107)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35582
diff changeset
325 except UnicodeError:
76635ab7b707 Fixed bug where the logging message was wrongly being demoted from Unicode to string (SF #1314107)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35582
diff changeset
326 msg = self.msg #Defer encoding till later
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 if self.args:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 msg = msg % self.args
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 return msg
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
29624
a464020f15d6 SF patch #761519: Fixes for bugs 760703 and 757821
Raymond Hettinger <python@rcn.com>
parents: 28739
diff changeset
331 def makeLogRecord(dict):
a464020f15d6 SF patch #761519: Fixes for bugs 760703 and 757821
Raymond Hettinger <python@rcn.com>
parents: 28739
diff changeset
332 """
a464020f15d6 SF patch #761519: Fixes for bugs 760703 and 757821
Raymond Hettinger <python@rcn.com>
parents: 28739
diff changeset
333 Make a LogRecord whose attributes are defined by the specified dictionary,
a464020f15d6 SF patch #761519: Fixes for bugs 760703 and 757821
Raymond Hettinger <python@rcn.com>
parents: 28739
diff changeset
334 This function is useful for converting a logging event received over
a464020f15d6 SF patch #761519: Fixes for bugs 760703 and 757821
Raymond Hettinger <python@rcn.com>
parents: 28739
diff changeset
335 a socket connection (which is sent as a dictionary) into a LogRecord
a464020f15d6 SF patch #761519: Fixes for bugs 760703 and 757821
Raymond Hettinger <python@rcn.com>
parents: 28739
diff changeset
336 instance.
a464020f15d6 SF patch #761519: Fixes for bugs 760703 and 757821
Raymond Hettinger <python@rcn.com>
parents: 28739
diff changeset
337 """
36469
93efa59b3297 Added function name to LogRecord.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36468
diff changeset
338 rv = LogRecord(None, None, "", 0, "", (), None, None)
29624
a464020f15d6 SF patch #761519: Fixes for bugs 760703 and 757821
Raymond Hettinger <python@rcn.com>
parents: 28739
diff changeset
339 rv.__dict__.update(dict)
a464020f15d6 SF patch #761519: Fixes for bugs 760703 and 757821
Raymond Hettinger <python@rcn.com>
parents: 28739
diff changeset
340 return rv
a464020f15d6 SF patch #761519: Fixes for bugs 760703 and 757821
Raymond Hettinger <python@rcn.com>
parents: 28739
diff changeset
341
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 # Formatter classes and functions
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
346 class Formatter(object):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 Formatter instances are used to convert a LogRecord to text.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 Formatters need to know how a LogRecord is constructed. They are
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 responsible for converting a LogRecord to (usually) a string which can
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 be interpreted by either a human or an external system. The base Formatter
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 allows a formatting string to be specified. If none is supplied, the
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 default value of "%s(message)\\n" is used.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
355
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 The Formatter can be initialized with a format string which makes use of
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 knowledge of the LogRecord attributes - e.g. the default value mentioned
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 above makes use of the fact that the user's message and arguments are pre-
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 formatted into a LogRecord's message attribute. Currently, the useful
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 attributes in a LogRecord are described by:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 %(name)s Name of the logger (logging channel)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 %(levelno)s Numeric logging level for the message (DEBUG, INFO,
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
364 WARNING, ERROR, CRITICAL)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 %(levelname)s Text logging level for the message ("DEBUG", "INFO",
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
366 "WARNING", "ERROR", "CRITICAL")
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 %(pathname)s Full pathname of the source file where the logging
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 call was issued (if available)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 %(filename)s Filename portion of pathname
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 %(module)s Module (name portion of filename)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 %(lineno)d Source line number where the logging call was issued
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 (if available)
36469
93efa59b3297 Added function name to LogRecord.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36468
diff changeset
373 %(funcName)s Function name
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 %(created)f Time when the LogRecord was created (time.time()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 return value)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 %(asctime)s Textual time when the LogRecord was created
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 %(msecs)d Millisecond portion of the creation time
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 %(relativeCreated)d Time in milliseconds when the LogRecord was created,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 relative to the time the logging module was loaded
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 (typically at application startup time)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 %(thread)d Thread ID (if available)
34858
2ea3d6915204 Added threadName and now using re-entrant lock
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34744
diff changeset
382 %(threadName)s Thread name (if available)
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
383 %(process)d Process ID (if available)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 %(message)s The result of record.getMessage(), computed just as
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 the record is emitted
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 converter = time.localtime
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 def __init__(self, fmt=None, datefmt=None):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 Initialize the formatter with specified format strings.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
393
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 Initialize the formatter either with the specified format string, or a
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 default as described above. Allow for specialized date formatting with
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 the optional datefmt argument (if omitted, you get the ISO8601 format).
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 if fmt:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 self._fmt = fmt
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 else:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 self._fmt = "%(message)s"
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 self.datefmt = datefmt
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
403
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 def formatTime(self, record, datefmt=None):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 Return the creation time of the specified LogRecord as formatted text.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
407
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 This method should be called from format() by a formatter which
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 wants to make use of a formatted time. This method can be overridden
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 in formatters to provide for any specific requirement, but the
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 basic behaviour is as follows: if datefmt (a string) is specified,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 it is used with time.strftime() to format the creation time of the
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 record. Otherwise, the ISO8601 format is used. The resulting
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 string is returned. This function uses a user-configurable function
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 to convert the creation time to a tuple. By default, time.localtime()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 is used; to change this for a particular formatter instance, set the
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 'converter' attribute to a function with the same signature as
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 time.localtime() or time.gmtime(). To change it for all formatters,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 for example if you want all logging times to be shown in GMT,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 set the 'converter' attribute in the Formatter class.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 ct = self.converter(record.created)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 if datefmt:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 s = time.strftime(datefmt, ct)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 else:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 t = time.strftime("%Y-%m-%d %H:%M:%S", ct)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 s = "%s,%03d" % (t, record.msecs)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 return s
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 def formatException(self, ei):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 Format and return the specified exception information as a string.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 This default implementation just uses
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 traceback.print_exception()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 sio = cStringIO.StringIO()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 traceback.print_exception(ei[0], ei[1], ei[2], None, sio)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 s = sio.getvalue()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 sio.close()
42115
66c8347a64dd Bug #1737864: allow empty message in logging format routines.
Georg Brandl <georg@python.org>
parents: 40860
diff changeset
441 if s[-1:] == "\n":
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 s = s[:-1]
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 return s
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444
59377
cc70f68e9c11 Factored out time usage determination into a method, to facilitate alternative formatting implementations in the future.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58805
diff changeset
445 def usesTime(self):
cc70f68e9c11 Factored out time usage determination into a method, to facilitate alternative formatting implementations in the future.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58805
diff changeset
446 """
cc70f68e9c11 Factored out time usage determination into a method, to facilitate alternative formatting implementations in the future.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58805
diff changeset
447 Check if the format uses the creation time of the record.
cc70f68e9c11 Factored out time usage determination into a method, to facilitate alternative formatting implementations in the future.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58805
diff changeset
448 """
cc70f68e9c11 Factored out time usage determination into a method, to facilitate alternative formatting implementations in the future.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58805
diff changeset
449 return self._fmt.find("%(asctime)") >= 0
cc70f68e9c11 Factored out time usage determination into a method, to facilitate alternative formatting implementations in the future.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58805
diff changeset
450
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 def format(self, record):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 Format the specified record as text.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 The record's attribute dictionary is used as the operand to a
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 string formatting operation which yields the returned string.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 Before formatting the dictionary, a couple of preparatory steps
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 are carried out. The message attribute of the record is computed
59377
cc70f68e9c11 Factored out time usage determination into a method, to facilitate alternative formatting implementations in the future.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58805
diff changeset
459 using LogRecord.getMessage(). If the formatting string uses the
cc70f68e9c11 Factored out time usage determination into a method, to facilitate alternative formatting implementations in the future.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58805
diff changeset
460 time (as determined by a call to usesTime(), formatTime() is
cc70f68e9c11 Factored out time usage determination into a method, to facilitate alternative formatting implementations in the future.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58805
diff changeset
461 called to format the event time. If there is exception information,
cc70f68e9c11 Factored out time usage determination into a method, to facilitate alternative formatting implementations in the future.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58805
diff changeset
462 it is formatted using formatException() and appended to the message.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 record.message = record.getMessage()
59377
cc70f68e9c11 Factored out time usage determination into a method, to facilitate alternative formatting implementations in the future.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58805
diff changeset
465 if self.usesTime():
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 record.asctime = self.formatTime(record, self.datefmt)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 s = self._fmt % record.__dict__
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 if record.exc_info:
31563
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
469 # Cache the traceback text to avoid converting it multiple times
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
470 # (it's constant anyway)
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
471 if not record.exc_text:
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
472 record.exc_text = self.formatException(record.exc_info)
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
473 if record.exc_text:
42115
66c8347a64dd Bug #1737864: allow empty message in logging format routines.
Georg Brandl <georg@python.org>
parents: 40860
diff changeset
474 if s[-1:] != "\n":
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 s = s + "\n"
62268
fe6be0426e0d Issue #8924: logging: Improved error handling for Unicode in exception text.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 61173
diff changeset
476 try:
fe6be0426e0d Issue #8924: logging: Improved error handling for Unicode in exception text.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 61173
diff changeset
477 s = s + record.exc_text
fe6be0426e0d Issue #8924: logging: Improved error handling for Unicode in exception text.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 61173
diff changeset
478 except UnicodeError:
fe6be0426e0d Issue #8924: logging: Improved error handling for Unicode in exception text.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 61173
diff changeset
479 # Sometimes filenames have non-ASCII chars, which can lead
fe6be0426e0d Issue #8924: logging: Improved error handling for Unicode in exception text.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 61173
diff changeset
480 # to errors when s is Unicode and record.exc_text is str
73108
4bb1dc4e2cec Closes #13232: Handle multiple encodings in exception logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68420
diff changeset
481 # See issue 8924.
4bb1dc4e2cec Closes #13232: Handle multiple encodings in exception logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68420
diff changeset
482 # We also use replace for when there are multiple
73125
a7191ec811c6 Corrected typo in comment.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 73108
diff changeset
483 # encodings, e.g. UTF-8 for the filesystem and latin-1
73108
4bb1dc4e2cec Closes #13232: Handle multiple encodings in exception logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68420
diff changeset
484 # for a script. See issue 13232.
4bb1dc4e2cec Closes #13232: Handle multiple encodings in exception logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68420
diff changeset
485 s = s + record.exc_text.decode(sys.getfilesystemencoding(),
4bb1dc4e2cec Closes #13232: Handle multiple encodings in exception logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68420
diff changeset
486 'replace')
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 return s
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
488
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 # The default formatter to use when no other is specified
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 _defaultFormatter = Formatter()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
493
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
494 class BufferingFormatter(object):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 A formatter suitable for formatting a number of records.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 def __init__(self, linefmt=None):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 Optionally specify a formatter which will be used to format each
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 individual record.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 if linefmt:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 self.linefmt = linefmt
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 else:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 self.linefmt = _defaultFormatter
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
507
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 def formatHeader(self, records):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 Return the header string for the specified records.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 return ""
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
513
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 def formatFooter(self, records):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 Return the footer string for the specified records.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 return ""
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
519
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 def format(self, records):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 Format the specified records and return the result as a string.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 rv = ""
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 if len(records) > 0:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 rv = rv + self.formatHeader(records)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 for record in records:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 rv = rv + self.linefmt.format(record)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 rv = rv + self.formatFooter(records)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 return rv
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
531
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 # Filter classes and functions
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
535
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
536 class Filter(object):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 Filter instances are used to perform arbitrary filtering of LogRecords.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
539
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 Loggers and Handlers can optionally use Filter instances to filter
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 records as desired. The base filter class only allows events which are
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 below a certain point in the logger hierarchy. For example, a filter
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 initialized with "A.B" will allow events logged by loggers "A.B",
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 "A.B.C", "A.B.C.D", "A.B.D" etc. but not "A.BB", "B.A.B" etc. If
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 initialized with the empty string, all events are passed.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 def __init__(self, name=''):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 Initialize a filter.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
550
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 Initialize with the name of the logger which, together with its
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 children, will have its events allowed through the filter. If no
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 name is specified, allow every event.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 self.name = name
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 self.nlen = len(name)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
557
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 def filter(self, record):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 Determine if the specified record is to be logged.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
561
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 Is the specified record to be logged? Returns 0 for no, nonzero for
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 yes. If deemed appropriate, the record may be modified in-place.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 if self.nlen == 0:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 return 1
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 elif self.name == record.name:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 return 1
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
569 elif record.name.find(self.name, 0, self.nlen) != 0:
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 return 0
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 return (record.name[self.nlen] == ".")
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
572
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
573 class Filterer(object):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 A base class for loggers and handlers which allows them to share
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 common code.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 def __init__(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 Initialize the list of filters to be an empty list.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 self.filters = []
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
583
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 def addFilter(self, filter):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 Add the specified filter to this handler.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 if not (filter in self.filters):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 self.filters.append(filter)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
590
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 def removeFilter(self, filter):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 Remove the specified filter from this handler.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 if filter in self.filters:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 self.filters.remove(filter)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
597
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 def filter(self, record):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 Determine if a record is loggable by consulting all the filters.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
601
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 The default is to allow the record to be logged; any filter can veto
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 this and the record is then dropped. Returns a zero value if a record
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 is to be dropped, else non-zero.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 rv = 1
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 for f in self.filters:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 if not f.filter(record):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 rv = 0
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 break
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 return rv
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
612
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 # Handler classes and functions
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
616
57446
1d157e2ce949 logging: made _handlers a WeakValueDictionary.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57445
diff changeset
617 _handlers = weakref.WeakValueDictionary() #map of handler names to handlers
35526
336b664a2210 Added _handlerList to allow shutdown to flush and close handlers in reverse order of creation (see SF# 1282539)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35512
diff changeset
618 _handlerList = [] # added to allow handlers to be removed in reverse of order initialized
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
619
57445
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
620 def _removeHandlerRef(wr):
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
621 """
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
622 Remove a handler reference from the internal cleanup list.
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
623 """
64246
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
624 # This function can be called during module teardown, when globals are
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
625 # set to None. If _acquireLock is None, assume this is the case and do
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
626 # nothing.
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
627 if _acquireLock is not None:
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
628 _acquireLock()
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
629 try:
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
630 if wr in _handlerList:
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
631 _handlerList.remove(wr)
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
632 finally:
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
633 _releaseLock()
57445
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
634
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
635 def _addHandlerRef(handler):
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
636 """
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
637 Add a handler to the internal cleanup list using a weak reference.
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
638 """
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
639 _acquireLock()
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
640 try:
57447
85cd93525568 logging: Issue 6615: Changed handler prepend to append.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57446
diff changeset
641 _handlerList.append(weakref.ref(handler, _removeHandlerRef))
57445
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
642 finally:
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
643 _releaseLock()
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
644
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 class Handler(Filterer):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
647 Handler instances dispatch logging events to specific destinations.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
648
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
649 The base handler class. Acts as a placeholder which defines the Handler
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
650 interface. Handlers can optionally use Formatter instances to format
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
651 records as desired. By default, no formatter is specified; in this case,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 the 'raw' message as determined by record.message is logged.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 def __init__(self, level=NOTSET):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 Initializes the instance - basically setting the formatter to None
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 and the filter list to empty.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 Filterer.__init__(self)
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
660 self._name = None
55241
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
661 self.level = _checkLevel(level)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 self.formatter = None
57445
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
663 # Add the handler to the global _handlerList (for cleanup on shutdown)
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
664 _addHandlerRef(self)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 self.createLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
666
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
667 def get_name(self):
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
668 return self._name
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
669
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
670 def set_name(self, name):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 _acquireLock()
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
672 try:
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
673 if self._name in _handlers:
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
674 del _handlers[self._name]
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
675 self._name = name
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
676 if name:
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
677 _handlers[name] = self
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
678 finally:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 _releaseLock()
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
680
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
681 name = property(get_name, set_name)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
682
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 def createLock(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
684 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
685 Acquire a thread lock for serializing access to the underlying I/O.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
686 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
687 if thread:
34858
2ea3d6915204 Added threadName and now using re-entrant lock
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34744
diff changeset
688 self.lock = threading.RLock()
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 else:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 self.lock = None
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
691
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 def acquire(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 Acquire the I/O thread lock.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
695 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 if self.lock:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 self.lock.acquire()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
698
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
699 def release(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
701 Release the I/O thread lock.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
702 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 if self.lock:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 self.lock.release()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
705
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 def setLevel(self, level):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 Set the logging level of this handler.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
709 """
55241
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
710 self.level = _checkLevel(level)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
711
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 def format(self, record):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 Format the specified record.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
715
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
716 If a formatter is set, use it. Otherwise, use the default formatter
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 for the module.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 if self.formatter:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 fmt = self.formatter
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 else:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
722 fmt = _defaultFormatter
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 return fmt.format(record)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
724
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 def emit(self, record):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 Do whatever it takes to actually log the specified logging record.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
728
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
729 This version is intended to be implemented by subclasses and so
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 raises a NotImplementedError.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
731 """
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
732 raise NotImplementedError('emit must be implemented '
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
733 'by Handler subclasses')
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
734
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
735 def handle(self, record):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 Conditionally emit the specified logging record.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
738
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 Emission depends on filters which may have been added to the handler.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
740 Wrap the actual emission of the record with acquisition/release of
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
741 the I/O thread lock. Returns whether the filter passed the record for
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
742 emission.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
743 """
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
744 rv = self.filter(record)
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
745 if rv:
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
746 self.acquire()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
747 try:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 self.emit(record)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
749 finally:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
750 self.release()
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
751 return rv
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
752
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 def setFormatter(self, fmt):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 Set the formatter for this handler.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
757 self.formatter = fmt
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
758
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
759 def flush(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
760 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
761 Ensure all logging output has been flushed.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
762
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
763 This version does nothing and is intended to be implemented by
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
764 subclasses.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
766 pass
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
767
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 def close(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
769 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
770 Tidy up any resources used by the handler.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
771
57445
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
772 This version removes the handler from an internal map of handlers,
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
773 _handlers, which is used for handler lookup by name. Subclasses
31563
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
774 should ensure that this gets called from overridden close()
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
775 methods.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 """
31563
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
777 #get the module data lock, as we're updating a shared structure.
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
778 _acquireLock()
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
779 try: #unlikely to raise an exception, but you never know...
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
780 if self._name and self._name in _handlers:
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
781 del _handlers[self._name]
31563
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
782 finally:
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
783 _releaseLock()
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
784
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
785 def handleError(self, record):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
787 Handle errors which occur during an emit() call.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
788
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
789 This method should be called from handlers when an exception is
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
790 encountered during an emit() call. If raiseExceptions is false,
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
791 exceptions get silently ignored. This is what is mostly wanted
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
792 for a logging system - most users will not care about errors in
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
793 the logging system, they are more interested in application errors.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 You could, however, replace this with a custom handler if you wish.
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
795 The record which was being processed is passed in to this method.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
796 """
74537
b60c789c4ccd Closes #13807: Now checks for sys.stderr being there before writing to it.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 73418
diff changeset
797 if raiseExceptions and sys.stderr: # see issue 13807
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 ei = sys.exc_info()
53946
eb6eb49dbae9 Issue #5971: StreamHandler.handleError now swallows IOErrors which occur when trying to print a traceback.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53504
diff changeset
799 try:
58805
87cd893cb8ba Issue #7869: logging: improved format-time diagnostics and removed some 1.5.2 support code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58779
diff changeset
800 traceback.print_exception(ei[0], ei[1], ei[2],
87cd893cb8ba Issue #7869: logging: improved format-time diagnostics and removed some 1.5.2 support code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58779
diff changeset
801 None, sys.stderr)
87cd893cb8ba Issue #7869: logging: improved format-time diagnostics and removed some 1.5.2 support code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58779
diff changeset
802 sys.stderr.write('Logged from file %s, line %s\n' % (
87cd893cb8ba Issue #7869: logging: improved format-time diagnostics and removed some 1.5.2 support code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58779
diff changeset
803 record.filename, record.lineno))
53946
eb6eb49dbae9 Issue #5971: StreamHandler.handleError now swallows IOErrors which occur when trying to print a traceback.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53504
diff changeset
804 except IOError:
eb6eb49dbae9 Issue #5971: StreamHandler.handleError now swallows IOErrors which occur when trying to print a traceback.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53504
diff changeset
805 pass # see issue 5971
eb6eb49dbae9 Issue #5971: StreamHandler.handleError now swallows IOErrors which occur when trying to print a traceback.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53504
diff changeset
806 finally:
eb6eb49dbae9 Issue #5971: StreamHandler.handleError now swallows IOErrors which occur when trying to print a traceback.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53504
diff changeset
807 del ei
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
808
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 class StreamHandler(Handler):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
811 A handler class which writes logging records, appropriately formatted,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
812 to a stream. Note that this class does not close the stream, as
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
813 sys.stdout or sys.stderr may be used.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 """
48923
576441527d4f logging: fixed lack of use of encoding attribute specified on a stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48289
diff changeset
815
56234
7391436d8a74 Tidied up name of parameter in StreamHandler
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 55241
diff changeset
816 def __init__(self, stream=None):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
817 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
818 Initialize the handler.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
819
56234
7391436d8a74 Tidied up name of parameter in StreamHandler
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 55241
diff changeset
820 If stream is not specified, sys.stderr is used.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
821 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
822 Handler.__init__(self)
56234
7391436d8a74 Tidied up name of parameter in StreamHandler
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 55241
diff changeset
823 if stream is None:
7391436d8a74 Tidied up name of parameter in StreamHandler
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 55241
diff changeset
824 stream = sys.stderr
7391436d8a74 Tidied up name of parameter in StreamHandler
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 55241
diff changeset
825 self.stream = stream
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
826
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
827 def flush(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
828 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
829 Flushes the stream.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
830 """
75220
2ab3a97d544c Fix added for recent changes in non-threading environments.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75210
diff changeset
831 self.acquire()
2ab3a97d544c Fix added for recent changes in non-threading environments.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75210
diff changeset
832 try:
75209
1316d2af2331 logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 74537
diff changeset
833 if self.stream and hasattr(self.stream, "flush"):
1316d2af2331 logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 74537
diff changeset
834 self.stream.flush()
75220
2ab3a97d544c Fix added for recent changes in non-threading environments.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75210
diff changeset
835 finally:
2ab3a97d544c Fix added for recent changes in non-threading environments.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75210
diff changeset
836 self.release()
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
837
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
838 def emit(self, record):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
839 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
840 Emit a record.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
841
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
842 If a formatter is specified, it is used to format the record.
48923
576441527d4f logging: fixed lack of use of encoding attribute specified on a stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48289
diff changeset
843 The record is then written to the stream with a trailing newline. If
576441527d4f logging: fixed lack of use of encoding attribute specified on a stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48289
diff changeset
844 exception information is present, it is formatted using
576441527d4f logging: fixed lack of use of encoding attribute specified on a stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48289
diff changeset
845 traceback.print_exception and appended to the stream. If the stream
53179
73bfcb398886 Issue #5768: Change to Unicode output logic and test case for same.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 51739
diff changeset
846 has an 'encoding' attribute, it is used to determine how to do the
48923
576441527d4f logging: fixed lack of use of encoding attribute specified on a stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 48289
diff changeset
847 output to the stream.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
848 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 try:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
850 msg = self.format(record)
51475
c17f9281a856 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 50951
diff changeset
851 stream = self.stream
33535
ecd654daaec9 Added getLoggerClass()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32955
diff changeset
852 fs = "%s\n"
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
853 if not _unicode: #if no unicode support...
51475
c17f9281a856 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 50951
diff changeset
854 stream.write(fs % msg)
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
855 else:
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
856 try:
53180
cf190ff2a72c Issue #5768: Change to Unicode output logic and test case for same.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 52148
diff changeset
857 if (isinstance(msg, unicode) and
cf190ff2a72c Issue #5768: Change to Unicode output logic and test case for same.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 52148
diff changeset
858 getattr(stream, 'encoding', None)):
57650
ae9396260835 Issue #7470: logging: fix bug in Unicode encoding fallback.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57474
diff changeset
859 ufs = fs.decode(stream.encoding)
53311
78bfd4481763 Issue #5170: Fixed regression caused when fixing #5768.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53180
diff changeset
860 try:
57650
ae9396260835 Issue #7470: logging: fix bug in Unicode encoding fallback.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57474
diff changeset
861 stream.write(ufs % msg)
53311
78bfd4481763 Issue #5170: Fixed regression caused when fixing #5768.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53180
diff changeset
862 except UnicodeEncodeError:
78bfd4481763 Issue #5170: Fixed regression caused when fixing #5768.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53180
diff changeset
863 #Printing to terminals sometimes fails. For example,
78bfd4481763 Issue #5170: Fixed regression caused when fixing #5768.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53180
diff changeset
864 #with an encoding of 'cp1251', the above write will
78bfd4481763 Issue #5170: Fixed regression caused when fixing #5768.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53180
diff changeset
865 #work if written to a stream opened or wrapped by
78bfd4481763 Issue #5170: Fixed regression caused when fixing #5768.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53180
diff changeset
866 #the codecs module, but fail when writing to a
78bfd4481763 Issue #5170: Fixed regression caused when fixing #5768.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53180
diff changeset
867 #terminal even when the codepage is set to cp1251.
78bfd4481763 Issue #5170: Fixed regression caused when fixing #5768.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53180
diff changeset
868 #An extra encoding step seems to be needed.
57650
ae9396260835 Issue #7470: logging: fix bug in Unicode encoding fallback.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57474
diff changeset
869 stream.write((ufs % msg).encode(stream.encoding))
53180
cf190ff2a72c Issue #5768: Change to Unicode output logic and test case for same.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 52148
diff changeset
870 else:
51475
c17f9281a856 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 50951
diff changeset
871 stream.write(fs % msg)
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
872 except UnicodeError:
51475
c17f9281a856 Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 50951
diff changeset
873 stream.write(fs % msg.encode("UTF-8"))
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
874 self.flush()
35816
a000f322dd05 Exception handling now raises KeyboardInterrupt and SystemExit rather than passing to handleError
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35787
diff changeset
875 except (KeyboardInterrupt, SystemExit):
a000f322dd05 Exception handling now raises KeyboardInterrupt and SystemExit rather than passing to handleError
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35787
diff changeset
876 raise
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
877 except:
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
878 self.handleError(record)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
879
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
880 class FileHandler(StreamHandler):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
881 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
882 A handler class which writes formatted logging records to disk files.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
883 """
44907
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
884 def __init__(self, filename, mode='a', encoding=None, delay=0):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
885 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
886 Open the specified file and use it as the stream for logging.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
887 """
40577
92dd2bb94ca9 Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding".
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40575
diff changeset
888 #keep the absolute path, otherwise derived classes which use this
92dd2bb94ca9 Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding".
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40575
diff changeset
889 #may come a cropper when the current directory changes
34744
3d97eb3b7cec Added optional encoding argument to file handlers. Made traceback import unconditional, to avoid lock contention problems which occur when logging from custom importers (SF patch #1158052)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34612
diff changeset
890 if codecs is None:
3d97eb3b7cec Added optional encoding argument to file handlers. Made traceback import unconditional, to avoid lock contention problems which occur when logging from custom importers (SF patch #1158052)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34612
diff changeset
891 encoding = None
32546
d42749db9339 FileHandler now stores the basename as an absolute path (fix for SF #982049)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32528
diff changeset
892 self.baseFilename = os.path.abspath(filename)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
893 self.mode = mode
40577
92dd2bb94ca9 Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding".
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40575
diff changeset
894 self.encoding = encoding
44907
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
895 if delay:
50949
7c30ab0134a1 Issue 5013: Fixed bug in FileHandler when delay was set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 50257
diff changeset
896 #We don't open the stream, but we still need to call the
7c30ab0134a1 Issue 5013: Fixed bug in FileHandler when delay was set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 50257
diff changeset
897 #Handler constructor to set level, formatter, lock etc.
7c30ab0134a1 Issue 5013: Fixed bug in FileHandler when delay was set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 50257
diff changeset
898 Handler.__init__(self)
44907
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
899 self.stream = None
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
900 else:
50949
7c30ab0134a1 Issue 5013: Fixed bug in FileHandler when delay was set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 50257
diff changeset
901 StreamHandler.__init__(self, self._open())
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
902
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
903 def close(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
904 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
905 Closes the stream.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
906 """
75220
2ab3a97d544c Fix added for recent changes in non-threading environments.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75210
diff changeset
907 self.acquire()
2ab3a97d544c Fix added for recent changes in non-threading environments.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75210
diff changeset
908 try:
75209
1316d2af2331 logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 74537
diff changeset
909 if self.stream:
1316d2af2331 logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 74537
diff changeset
910 self.flush()
1316d2af2331 logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 74537
diff changeset
911 if hasattr(self.stream, "close"):
1316d2af2331 logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 74537
diff changeset
912 self.stream.close()
1316d2af2331 logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 74537
diff changeset
913 StreamHandler.close(self)
1316d2af2331 logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 74537
diff changeset
914 self.stream = None
75220
2ab3a97d544c Fix added for recent changes in non-threading environments.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75210
diff changeset
915 finally:
2ab3a97d544c Fix added for recent changes in non-threading environments.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75210
diff changeset
916 self.release()
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
917
40577
92dd2bb94ca9 Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding".
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40575
diff changeset
918 def _open(self):
40579
61d634a0bd6a Added a docstring
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40577
diff changeset
919 """
61d634a0bd6a Added a docstring
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40577
diff changeset
920 Open the current base file with the (original) mode and encoding.
61d634a0bd6a Added a docstring
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40577
diff changeset
921 Return the resulting stream.
61d634a0bd6a Added a docstring
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40577
diff changeset
922 """
40577
92dd2bb94ca9 Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding".
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40575
diff changeset
923 if self.encoding is None:
92dd2bb94ca9 Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding".
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40575
diff changeset
924 stream = open(self.baseFilename, self.mode)
92dd2bb94ca9 Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding".
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40575
diff changeset
925 else:
92dd2bb94ca9 Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding".
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40575
diff changeset
926 stream = codecs.open(self.baseFilename, self.mode, self.encoding)
92dd2bb94ca9 Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding".
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40575
diff changeset
927 return stream
92dd2bb94ca9 Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding".
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 40575
diff changeset
928
44907
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
929 def emit(self, record):
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
930 """
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
931 Emit a record.
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
932
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
933 If the stream was not opened because 'delay' was specified in the
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
934 constructor, open it before calling the superclass's emit.
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
935 """
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
936 if self.stream is None:
50949
7c30ab0134a1 Issue 5013: Fixed bug in FileHandler when delay was set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 50257
diff changeset
937 self.stream = self._open()
44907
f3a9ad448cca Added optional delay argument to FileHandler and subclasses.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 44745
diff changeset
938 StreamHandler.emit(self, record)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
939
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
940 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 # Manager classes and functions
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
942 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
943
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
944 class PlaceHolder(object):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
945 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
946 PlaceHolder instances are used in the Manager logger hierarchy to take
31597
05e1c27c0a74 Minor documentation changes
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31575
diff changeset
947 the place of nodes for which no loggers have been defined. This class is
05e1c27c0a74 Minor documentation changes
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31575
diff changeset
948 intended for internal use only and not as part of the public API.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
949 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
950 def __init__(self, alogger):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
951 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
952 Initialize with the specified logger being a child of this placeholder.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
953 """
35721
cda87357185f Optimised Placeholders handling of child loggers by using a dict rather than a list (much slower in the pathological case of hundreds of child Loggers to a Placeholder - problem reported by Ryan Blazecka).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35703
diff changeset
954 #self.loggers = [alogger]
cda87357185f Optimised Placeholders handling of child loggers by using a dict rather than a list (much slower in the pathological case of hundreds of child Loggers to a Placeholder - problem reported by Ryan Blazecka).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35703
diff changeset
955 self.loggerMap = { alogger : None }
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
956
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
957 def append(self, alogger):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
958 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
959 Add the specified logger as a child of this placeholder.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
960 """
35721
cda87357185f Optimised Placeholders handling of child loggers by using a dict rather than a list (much slower in the pathological case of hundreds of child Loggers to a Placeholder - problem reported by Ryan Blazecka).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35703
diff changeset
961 #if alogger not in self.loggers:
50257
e4675113d10e Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49009
diff changeset
962 if alogger not in self.loggerMap:
35721
cda87357185f Optimised Placeholders handling of child loggers by using a dict rather than a list (much slower in the pathological case of hundreds of child Loggers to a Placeholder - problem reported by Ryan Blazecka).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35703
diff changeset
963 #self.loggers.append(alogger)
cda87357185f Optimised Placeholders handling of child loggers by using a dict rather than a list (much slower in the pathological case of hundreds of child Loggers to a Placeholder - problem reported by Ryan Blazecka).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35703
diff changeset
964 self.loggerMap[alogger] = None
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
965
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
966 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
967 # Determine which class to use when instantiating loggers.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
968 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
969 _loggerClass = None
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
970
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 def setLoggerClass(klass):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 Set the class to be used when instantiating a logger. The class should
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
974 define __init__() such that only a name argument is required, and the
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
975 __init__() should call Logger.__init__()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
976 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
977 if klass != Logger:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
978 if not issubclass(klass, Logger):
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
979 raise TypeError("logger not derived from logging.Logger: "
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
980 + klass.__name__)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 global _loggerClass
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
982 _loggerClass = klass
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
983
33535
ecd654daaec9 Added getLoggerClass()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32955
diff changeset
984 def getLoggerClass():
ecd654daaec9 Added getLoggerClass()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32955
diff changeset
985 """
ecd654daaec9 Added getLoggerClass()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32955
diff changeset
986 Return the class to be used when instantiating a logger.
ecd654daaec9 Added getLoggerClass()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32955
diff changeset
987 """
ecd654daaec9 Added getLoggerClass()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32955
diff changeset
988
ecd654daaec9 Added getLoggerClass()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32955
diff changeset
989 return _loggerClass
ecd654daaec9 Added getLoggerClass()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32955
diff changeset
990
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
991 class Manager(object):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
992 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
993 There is [under normal circumstances] just one Manager instance, which
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
994 holds the hierarchy of loggers.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
995 """
26257
f23c35f81033 SF Patch #638825 logging module
Neal Norwitz <nnorwitz@gmail.com>
parents: 26184
diff changeset
996 def __init__(self, rootnode):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
997 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
998 Initialize the manager with the root node of the logger hierarchy.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
999 """
26257
f23c35f81033 SF Patch #638825 logging module
Neal Norwitz <nnorwitz@gmail.com>
parents: 26184
diff changeset
1000 self.root = rootnode
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1001 self.disable = 0
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1002 self.emittedNoHandlerWarning = 0
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1003 self.loggerDict = {}
58779
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1004 self.loggerClass = None
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1005
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1006 def getLogger(self, name):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1007 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1008 Get a logger with the specified name (channel name), creating it
33535
ecd654daaec9 Added getLoggerClass()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32955
diff changeset
1009 if it doesn't yet exist. This name is a dot-separated hierarchical
ecd654daaec9 Added getLoggerClass()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32955
diff changeset
1010 name, such as "a", "a.b", "a.b.c" or similar.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1011
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1012 If a PlaceHolder existed for the specified name [i.e. the logger
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1013 didn't exist but a child of it did], replace it with the created
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1014 logger and fix up the parent/child references which pointed to the
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1015 placeholder to now point to the logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1016 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1017 rv = None
73415
5f3b7528b144 Closes #13661: Check added for type of logger name.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 73414
diff changeset
1018 if not isinstance(name, basestring):
73418
60dd1568bbd1 Closes #13361: Raise correct exception type.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 73415
diff changeset
1019 raise TypeError('A logger name must be string or Unicode')
73414
8726ad774cf0 Closes #13356. Thanks to Florent Xicluna for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 73125
diff changeset
1020 if isinstance(name, unicode):
8726ad774cf0 Closes #13356. Thanks to Florent Xicluna for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 73125
diff changeset
1021 name = name.encode('utf-8')
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1022 _acquireLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023 try:
48153
c77ac8bdd3fe Remove dict.has_key() and apply() usage from the logging package to silence
Brett Cannon <bcannon@gmail.com>
parents: 47745
diff changeset
1024 if name in self.loggerDict:
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025 rv = self.loggerDict[name]
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1026 if isinstance(rv, PlaceHolder):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1027 ph = rv
58779
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1028 rv = (self.loggerClass or _loggerClass)(name)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1029 rv.manager = self
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1030 self.loggerDict[name] = rv
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1031 self._fixupChildren(ph, rv)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1032 self._fixupParents(rv)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033 else:
58779
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1034 rv = (self.loggerClass or _loggerClass)(name)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1035 rv.manager = self
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036 self.loggerDict[name] = rv
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1037 self._fixupParents(rv)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1038 finally:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039 _releaseLock()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1040 return rv
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1041
58779
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1042 def setLoggerClass(self, klass):
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1043 """
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1044 Set the class to be used when instantiating a logger with this Manager.
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1045 """
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1046 if klass != Logger:
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1047 if not issubclass(klass, Logger):
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1048 raise TypeError("logger not derived from logging.Logger: "
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1049 + klass.__name__)
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1050 self.loggerClass = klass
6b82a2323501 Issue #7868: logging: added loggerClass attribute to Manager.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58063
diff changeset
1051
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1052 def _fixupParents(self, alogger):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054 Ensure that there are either loggers or placeholders all the way
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1055 from the specified logger to the root of the logger hierarchy.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1056 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1057 name = alogger.name
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
1058 i = name.rfind(".")
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1059 rv = None
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1060 while (i > 0) and not rv:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1061 substr = name[:i]
48153
c77ac8bdd3fe Remove dict.has_key() and apply() usage from the logging package to silence
Brett Cannon <bcannon@gmail.com>
parents: 47745
diff changeset
1062 if substr not in self.loggerDict:
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1063 self.loggerDict[substr] = PlaceHolder(alogger)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1064 else:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1065 obj = self.loggerDict[substr]
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1066 if isinstance(obj, Logger):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1067 rv = obj
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1068 else:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1069 assert isinstance(obj, PlaceHolder)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1070 obj.append(alogger)
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
1071 i = name.rfind(".", 0, i - 1)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1072 if not rv:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1073 rv = self.root
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1074 alogger.parent = rv
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1075
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1076 def _fixupChildren(self, ph, alogger):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1077 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1078 Ensure that children of the placeholder ph are connected to the
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1079 specified logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1080 """
40571
8da6dda679f8 Backported changes from trunk:
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 39439
diff changeset
1081 name = alogger.name
8da6dda679f8 Backported changes from trunk:
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 39439
diff changeset
1082 namelen = len(name)
35721
cda87357185f Optimised Placeholders handling of child loggers by using a dict rather than a list (much slower in the pathological case of hundreds of child Loggers to a Placeholder - problem reported by Ryan Blazecka).
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35703
diff changeset
1083 for c in ph.loggerMap.keys():
40571
8da6dda679f8 Backported changes from trunk:
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 39439
diff changeset
1084 #The if means ... if not c.parent.name.startswith(nm)
8da6dda679f8 Backported changes from trunk:
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 39439
diff changeset
1085 if c.parent.name[:namelen] != name:
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1086 alogger.parent = c.parent
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1087 c.parent = alogger
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1088
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1089 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1090 # Logger classes and functions
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1091 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1092
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1093 class Logger(Filterer):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1094 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1095 Instances of the Logger class represent a single logging channel. A
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1096 "logging channel" indicates an area of an application. Exactly how an
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1097 "area" is defined is up to the application developer. Since an
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1098 application can have any number of areas, logging channels are identified
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1099 by a unique string. Application areas can be nested (e.g. an area
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 of "input processing" might include sub-areas "read CSV files", "read
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1101 XLS files" and "read Gnumeric files"). To cater for this natural nesting,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1102 channel names are organized into a namespace hierarchy where levels are
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103 separated by periods, much like the Java or Python package namespace. So
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1104 in the instance given above, channel names might be "input" for the upper
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1105 level, and "input.csv", "input.xls" and "input.gnu" for the sub-levels.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1106 There is no arbitrary limit to the depth of nesting.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1107 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1108 def __init__(self, name, level=NOTSET):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1109 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1110 Initialize the logger with a name and an optional level.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1111 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1112 Filterer.__init__(self)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1113 self.name = name
55241
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
1114 self.level = _checkLevel(level)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1115 self.parent = None
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1116 self.propagate = 1
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1117 self.handlers = []
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1118 self.disabled = 0
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1119
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1120 def setLevel(self, level):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1121 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1122 Set the logging level of this logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1123 """
55241
32d48662aa5f Issue #6314: logging: Extra checks on the "level" argument in more places.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 54805
diff changeset
1124 self.level = _checkLevel(level)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1125
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1126 def debug(self, msg, *args, **kwargs):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1127 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1128 Log 'msg % args' with severity 'DEBUG'.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1129
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1130 To pass exception information, use the keyword argument exc_info with
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1131 a true value, e.g.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1132
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1133 logger.debug("Houston, we have a %s", "thorny problem", exc_info=1)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1134 """
42751
2073ea3005a7 Patch #1779550: remove redundant code in logging.
Georg Brandl <georg@python.org>
parents: 42114
diff changeset
1135 if self.isEnabledFor(DEBUG):
48153
c77ac8bdd3fe Remove dict.has_key() and apply() usage from the logging package to silence
Brett Cannon <bcannon@gmail.com>
parents: 47745
diff changeset
1136 self._log(DEBUG, msg, args, **kwargs)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1137
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1138 def info(self, msg, *args, **kwargs):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1139 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1140 Log 'msg % args' with severity 'INFO'.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1141
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1142 To pass exception information, use the keyword argument exc_info with
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1143 a true value, e.g.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1144
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1145 logger.info("Houston, we have a %s", "interesting problem", exc_info=1)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1146 """
42751
2073ea3005a7 Patch #1779550: remove redundant code in logging.
Georg Brandl <georg@python.org>
parents: 42114
diff changeset
1147 if self.isEnabledFor(INFO):
48153
c77ac8bdd3fe Remove dict.has_key() and apply() usage from the logging package to silence
Brett Cannon <bcannon@gmail.com>
parents: 47745
diff changeset
1148 self._log(INFO, msg, args, **kwargs)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1149
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
1150 def warning(self, msg, *args, **kwargs):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1151 """
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
1152 Log 'msg % args' with severity 'WARNING'.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1153
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1154 To pass exception information, use the keyword argument exc_info with
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1155 a true value, e.g.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1156
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
1157 logger.warning("Houston, we have a %s", "bit of a problem", exc_info=1)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1158 """
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
1159 if self.isEnabledFor(WARNING):
48153
c77ac8bdd3fe Remove dict.has_key() and apply() usage from the logging package to silence
Brett Cannon <bcannon@gmail.com>
parents: 47745
diff changeset
1160 self._log(WARNING, msg, args, **kwargs)
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
1161
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
1162 warn = warning
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1163
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1164 def error(self, msg, *args, **kwargs):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1165 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1166 Log 'msg % args' with severity 'ERROR'.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1167
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1168 To pass exception information, use the keyword argument exc_info with
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169 a true value, e.g.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1170
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1171 logger.error("Houston, we have a %s", "major problem", exc_info=1)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1172 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1173 if self.isEnabledFor(ERROR):
48153
c77ac8bdd3fe Remove dict.has_key() and apply() usage from the logging package to silence
Brett Cannon <bcannon@gmail.com>
parents: 47745
diff changeset
1174 self._log(ERROR, msg, args, **kwargs)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1175
78424
322da186cced Issue #15541: Correct anomaly in logging.exception. Thanks to Ned Batchelder for the report.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75220
diff changeset
1176 def exception(self, msg, *args, **kwargs):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1177 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1178 Convenience method for logging an ERROR with exception information.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1179 """
78424
322da186cced Issue #15541: Correct anomaly in logging.exception. Thanks to Ned Batchelder for the report.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75220
diff changeset
1180 kwargs['exc_info'] = 1
322da186cced Issue #15541: Correct anomaly in logging.exception. Thanks to Ned Batchelder for the report.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75220
diff changeset
1181 self.error(msg, *args, **kwargs)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1182
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1183 def critical(self, msg, *args, **kwargs):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1184 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1185 Log 'msg % args' with severity 'CRITICAL'.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1186
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1187 To pass exception information, use the keyword argument exc_info with
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1188 a true value, e.g.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1189
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1190 logger.critical("Houston, we have a %s", "major disaster", exc_info=1)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1191 """
42751
2073ea3005a7 Patch #1779550: remove redundant code in logging.
Georg Brandl <georg@python.org>
parents: 42114
diff changeset
1192 if self.isEnabledFor(CRITICAL):
48153
c77ac8bdd3fe Remove dict.has_key() and apply() usage from the logging package to silence
Brett Cannon <bcannon@gmail.com>
parents: 47745
diff changeset
1193 self._log(CRITICAL, msg, args, **kwargs)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1194
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1195 fatal = critical
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1196
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1197 def log(self, level, msg, *args, **kwargs):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1198 """
32955
e2fea22e0c51 Updated Logger.log() docstring to clarify that lvl parameter should be an integer.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32856
diff changeset
1199 Log 'msg % args' with the integer severity 'level'.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1200
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1201 To pass exception information, use the keyword argument exc_info with
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1202 a true value, e.g.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1203
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1204 logger.log(level, "We have a %s", "mysterious problem", exc_info=1)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1205 """
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
1206 if not isinstance(level, int):
32479
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1207 if raiseExceptions:
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
1208 raise TypeError("level must be an integer")
32479
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1209 else:
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1210 return
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1211 if self.isEnabledFor(level):
48153
c77ac8bdd3fe Remove dict.has_key() and apply() usage from the logging package to silence
Brett Cannon <bcannon@gmail.com>
parents: 47745
diff changeset
1212 self._log(level, msg, args, **kwargs)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1213
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1214 def findCaller(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1215 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1216 Find the stack frame of the caller so that we can note the source
34612
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
1217 file name, line number and function name.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1218 """
56281
a8d0e764e34b Issue #7014: logging: Improved IronPython 2.6 compatibility.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53946
diff changeset
1219 f = currentframe()
a8d0e764e34b Issue #7014: logging: Improved IronPython 2.6 compatibility.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53946
diff changeset
1220 #On some versions of IronPython, currentframe() returns None if
a8d0e764e34b Issue #7014: logging: Improved IronPython 2.6 compatibility.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53946
diff changeset
1221 #IronPython isn't run with -X:Frames.
a8d0e764e34b Issue #7014: logging: Improved IronPython 2.6 compatibility.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53946
diff changeset
1222 if f is not None:
a8d0e764e34b Issue #7014: logging: Improved IronPython 2.6 compatibility.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 53946
diff changeset
1223 f = f.f_back
36999
179084899d96 Catch situations where currentframe() returns None. See SF patch #1447410, this is a different implementation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36974
diff changeset
1224 rv = "(unknown file)", 0, "(unknown function)"
179084899d96 Catch situations where currentframe() returns None. See SF patch #1447410, this is a different implementation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36974
diff changeset
1225 while hasattr(f, "f_code"):
27349
ddb52f5673db Use lightweight introspection instead of the inspect module hammer.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26257
diff changeset
1226 co = f.f_code
ddb52f5673db Use lightweight introspection instead of the inspect module hammer.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26257
diff changeset
1227 filename = os.path.normcase(co.co_filename)
ddb52f5673db Use lightweight introspection instead of the inspect module hammer.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26257
diff changeset
1228 if filename == _srcfile:
ddb52f5673db Use lightweight introspection instead of the inspect module hammer.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26257
diff changeset
1229 f = f.f_back
ddb52f5673db Use lightweight introspection instead of the inspect module hammer.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26257
diff changeset
1230 continue
65213
5ede15e12458 Issue #9948: logging: fixed problem of losing filename case information.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64947
diff changeset
1231 rv = (co.co_filename, f.f_lineno, co.co_name)
36999
179084899d96 Catch situations where currentframe() returns None. See SF patch #1447410, this is a different implementation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36974
diff changeset
1232 break
179084899d96 Catch situations where currentframe() returns None. See SF patch #1447410, this is a different implementation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36974
diff changeset
1233 return rv
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1234
36469
93efa59b3297 Added function name to LogRecord.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36468
diff changeset
1235 def makeRecord(self, name, level, fn, lno, msg, args, exc_info, func=None, extra=None):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1236 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1237 A factory method which can be overridden in subclasses to create
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1238 specialized LogRecords.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1239 """
36469
93efa59b3297 Added function name to LogRecord.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36468
diff changeset
1240 rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
44745
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1241 if extra is not None:
36468
3cf8a9ed446b Propagate exceptions from shutdown() if raiseExceptions is not set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36456
diff changeset
1242 for key in extra:
3cf8a9ed446b Propagate exceptions from shutdown() if raiseExceptions is not set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36456
diff changeset
1243 if (key in ["message", "asctime"]) or (key in rv.__dict__):
3cf8a9ed446b Propagate exceptions from shutdown() if raiseExceptions is not set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36456
diff changeset
1244 raise KeyError("Attempt to overwrite %r in LogRecord" % key)
3cf8a9ed446b Propagate exceptions from shutdown() if raiseExceptions is not set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36456
diff changeset
1245 rv.__dict__[key] = extra[key]
3cf8a9ed446b Propagate exceptions from shutdown() if raiseExceptions is not set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36456
diff changeset
1246 return rv
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1247
36468
3cf8a9ed446b Propagate exceptions from shutdown() if raiseExceptions is not set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36456
diff changeset
1248 def _log(self, level, msg, args, exc_info=None, extra=None):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1249 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1250 Low-level logging routine which creates a LogRecord and then calls
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1251 all the handlers of this logger to handle the record.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1252 """
27349
ddb52f5673db Use lightweight introspection instead of the inspect module hammer.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26257
diff changeset
1253 if _srcfile:
51693
85407d047782 #5287: Add exception handling around findCaller() call to help out IronPython.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 51475
diff changeset
1254 #IronPython doesn't track Python frames, so findCaller throws an
56280
f26b802c003e Issue #7014: logging: Improved IronPython 2.6 compatibility.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56234
diff changeset
1255 #exception on some versions of IronPython. We trap it here so that
f26b802c003e Issue #7014: logging: Improved IronPython 2.6 compatibility.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56234
diff changeset
1256 #IronPython can use logging.
51693
85407d047782 #5287: Add exception handling around findCaller() call to help out IronPython.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 51475
diff changeset
1257 try:
85407d047782 #5287: Add exception handling around findCaller() call to help out IronPython.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 51475
diff changeset
1258 fn, lno, func = self.findCaller()
85407d047782 #5287: Add exception handling around findCaller() call to help out IronPython.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 51475
diff changeset
1259 except ValueError:
85407d047782 #5287: Add exception handling around findCaller() call to help out IronPython.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 51475
diff changeset
1260 fn, lno, func = "(unknown file)", 0, "(unknown function)"
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1261 else:
34612
07cf1a1640da Improved frame handling for 1.5.2, and now return func from findCaller (not actually used yet)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33815
diff changeset
1262 fn, lno, func = "(unknown file)", 0, "(unknown function)"
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1263 if exc_info:
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
1264 if not isinstance(exc_info, tuple):
31563
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
1265 exc_info = sys.exc_info()
36469
93efa59b3297 Added function name to LogRecord.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36468
diff changeset
1266 record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1267 self.handle(record)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1268
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1269 def handle(self, record):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1270 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1271 Call the handlers for the specified record.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1272
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1273 This method is used for unpickled records received from a socket, as
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1274 well as those created locally. Logger-level filtering is applied.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1275 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1276 if (not self.disabled) and self.filter(record):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1277 self.callHandlers(record)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1278
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1279 def addHandler(self, hdlr):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1280 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1281 Add the specified handler to this logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1282 """
64941
265f7017772b Issue #9945: logging: Fixed locking bugs in addHandler/removeHandler.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64643
diff changeset
1283 _acquireLock()
265f7017772b Issue #9945: logging: Fixed locking bugs in addHandler/removeHandler.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64643
diff changeset
1284 try:
265f7017772b Issue #9945: logging: Fixed locking bugs in addHandler/removeHandler.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64643
diff changeset
1285 if not (hdlr in self.handlers):
265f7017772b Issue #9945: logging: Fixed locking bugs in addHandler/removeHandler.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64643
diff changeset
1286 self.handlers.append(hdlr)
265f7017772b Issue #9945: logging: Fixed locking bugs in addHandler/removeHandler.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64643
diff changeset
1287 finally:
265f7017772b Issue #9945: logging: Fixed locking bugs in addHandler/removeHandler.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64643
diff changeset
1288 _releaseLock()
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1289
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1290 def removeHandler(self, hdlr):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1291 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1292 Remove the specified handler from this logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1293 """
64941
265f7017772b Issue #9945: logging: Fixed locking bugs in addHandler/removeHandler.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64643
diff changeset
1294 _acquireLock()
265f7017772b Issue #9945: logging: Fixed locking bugs in addHandler/removeHandler.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64643
diff changeset
1295 try:
265f7017772b Issue #9945: logging: Fixed locking bugs in addHandler/removeHandler.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64643
diff changeset
1296 if hdlr in self.handlers:
35582
52c65d73fb74 Added lock acquisition around handler removal from logger
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35526
diff changeset
1297 self.handlers.remove(hdlr)
64941
265f7017772b Issue #9945: logging: Fixed locking bugs in addHandler/removeHandler.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64643
diff changeset
1298 finally:
265f7017772b Issue #9945: logging: Fixed locking bugs in addHandler/removeHandler.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64643
diff changeset
1299 _releaseLock()
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1300
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1301 def callHandlers(self, record):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1302 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1303 Pass a record to all relevant handlers.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1304
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1305 Loop through all handlers for this logger and its parents in the
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1306 logger hierarchy. If no handler was found, output a one-off error
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1307 message to sys.stderr. Stop searching up the hierarchy whenever a
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1308 logger with the "propagate" attribute set to zero is found - that
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1309 will be the last logger whose handlers are called.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1310 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1311 c = self
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1312 found = 0
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1313 while c:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1314 for hdlr in c.handlers:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1315 found = found + 1
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1316 if record.levelno >= hdlr.level:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1317 hdlr.handle(record)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1318 if not c.propagate:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1319 c = None #break out
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1320 else:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1321 c = c.parent
35787
1661e73c07ab One-off "No handlers..." error message only raised if raiseExceptions is set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 35749
diff changeset
1322 if (found == 0) and raiseExceptions and not self.manager.emittedNoHandlerWarning:
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1323 sys.stderr.write("No handlers could be found for logger"
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1324 " \"%s\"\n" % self.name)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1325 self.manager.emittedNoHandlerWarning = 1
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1326
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1327 def getEffectiveLevel(self):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1328 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1329 Get the effective level for this logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1330
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1331 Loop through this logger and its parents in the logger hierarchy,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1332 looking for a non-zero logging level. Return the first one found.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1333 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1334 logger = self
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1335 while logger:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1336 if logger.level:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1337 return logger.level
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1338 logger = logger.parent
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1339 return NOTSET
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1340
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1341 def isEnabledFor(self, level):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1343 Is this logger enabled for level 'level'?
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1344 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1345 if self.manager.disable >= level:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1346 return 0
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1347 return level >= self.getEffectiveLevel()
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1348
59910
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1349 def getChild(self, suffix):
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1350 """
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1351 Get a logger which is a descendant to this one.
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1352
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1353 This is a convenience method, such that
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1354
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1355 logging.getLogger('abc').getChild('def.ghi')
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1356
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1357 is the same as
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1358
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1359 logging.getLogger('abc.def.ghi')
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1360
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1361 It's useful, for example, when the parent logger is named using
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1362 __name__ rather than a literal string.
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1363 """
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1364 if self.root is not self:
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1365 suffix = '.'.join((self.name, suffix))
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1366 return self.manager.getLogger(suffix)
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1367
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1368 class RootLogger(Logger):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1369 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1370 A root logger is not that different to any other logger, except that
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1371 it must have a logging level and there is only one instance of it in
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1372 the hierarchy.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1373 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1374 def __init__(self, level):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1375 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1376 Initialize the logger with the name "root".
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1377 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1378 Logger.__init__(self, "root", level)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1379
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1380 _loggerClass = Logger
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1381
57437
17e590be4c5a Made logging classes new-style and added name property to handlers.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56547
diff changeset
1382 class LoggerAdapter(object):
44745
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1383 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1384 An adapter for loggers which makes it easier to specify contextual
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1385 information in logging output.
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1386 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1387
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1388 def __init__(self, logger, extra):
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1389 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1390 Initialize the adapter with a logger and a dict-like object which
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1391 provides contextual information. This constructor signature allows
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1392 easy stacking of LoggerAdapters, if so desired.
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1393
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1394 You can effectively pass keyword arguments as shown in the
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1395 following example:
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1396
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1397 adapter = LoggerAdapter(someLogger, dict(p1=v1, p2="v2"))
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1398 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1399 self.logger = logger
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1400 self.extra = extra
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1401
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1402 def process(self, msg, kwargs):
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1403 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1404 Process the logging message and keyword arguments passed in to
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1405 a logging call to insert contextual information. You can either
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1406 manipulate the message itself, the keyword args or both. Return
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1407 the message and kwargs modified (or not) to suit your needs.
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1408
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1409 Normally, you'll only need to override this one method in a
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1410 LoggerAdapter subclass for your specific needs.
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1411 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1412 kwargs["extra"] = self.extra
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1413 return msg, kwargs
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1414
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1415 def debug(self, msg, *args, **kwargs):
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1416 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1417 Delegate a debug call to the underlying logger, after adding
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1418 contextual information from this adapter instance.
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1419 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1420 msg, kwargs = self.process(msg, kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1421 self.logger.debug(msg, *args, **kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1422
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1423 def info(self, msg, *args, **kwargs):
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1424 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1425 Delegate an info call to the underlying logger, after adding
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1426 contextual information from this adapter instance.
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1427 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1428 msg, kwargs = self.process(msg, kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1429 self.logger.info(msg, *args, **kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1430
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1431 def warning(self, msg, *args, **kwargs):
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1432 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1433 Delegate a warning call to the underlying logger, after adding
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1434 contextual information from this adapter instance.
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1435 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1436 msg, kwargs = self.process(msg, kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1437 self.logger.warning(msg, *args, **kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1438
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1439 def error(self, msg, *args, **kwargs):
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1440 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1441 Delegate an error call to the underlying logger, after adding
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1442 contextual information from this adapter instance.
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1443 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1444 msg, kwargs = self.process(msg, kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1445 self.logger.error(msg, *args, **kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1446
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1447 def exception(self, msg, *args, **kwargs):
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1448 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1449 Delegate an exception call to the underlying logger, after adding
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1450 contextual information from this adapter instance.
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1451 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1452 msg, kwargs = self.process(msg, kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1453 kwargs["exc_info"] = 1
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1454 self.logger.error(msg, *args, **kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1455
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1456 def critical(self, msg, *args, **kwargs):
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1457 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1458 Delegate a critical call to the underlying logger, after adding
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1459 contextual information from this adapter instance.
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1460 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1461 msg, kwargs = self.process(msg, kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1462 self.logger.critical(msg, *args, **kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1463
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1464 def log(self, level, msg, *args, **kwargs):
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1465 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1466 Delegate a log call to the underlying logger, after adding
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1467 contextual information from this adapter instance.
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1468 """
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1469 msg, kwargs = self.process(msg, kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1470 self.logger.log(level, msg, *args, **kwargs)
dbf0856776dd Added LoggerAdapter class, changed copyright dates, made check for extra parameter passed to logging methods explicitly against None rather than a truth value.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 43445
diff changeset
1471
59910
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1472 def isEnabledFor(self, level):
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1473 """
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1474 See if the underlying logger is enabled for the specified level.
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1475 """
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1476 return self.logger.isEnabledFor(level)
806a8cafc997 logging: Added getChild utility method to Logger and added isEnabledFor method to LoggerAdapter.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59896
diff changeset
1477
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
1478 root = RootLogger(WARNING)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1479 Logger.root = root
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1480 Logger.manager = Manager(Logger.root)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1481
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1482 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1483 # Configuration classes and functions
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1484 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1485
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1486 BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s"
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1487
32479
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1488 def basicConfig(**kwargs):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1489 """
32479
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1490 Do basic configuration for the logging system.
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1491
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1492 This function does nothing if the root logger already has handlers
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1493 configured. It is a convenience method intended for use by simple scripts
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1494 to do one-shot configuration of the logging package.
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1495
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1496 The default behaviour is to create a StreamHandler which writes to
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1497 sys.stderr, set a formatter using the BASIC_FORMAT format string, and
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1498 add the handler to the root logger.
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1499
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1500 A number of optional keyword arguments may be specified, which can alter
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1501 the default behaviour.
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1502
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1503 filename Specifies that a FileHandler be created, using the specified
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1504 filename, rather than a StreamHandler.
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1505 filemode Specifies the mode to open the file, if filename is specified
34744
3d97eb3b7cec Added optional encoding argument to file handlers. Made traceback import unconditional, to avoid lock contention problems which occur when logging from custom importers (SF patch #1158052)
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 34612
diff changeset
1506 (if filemode is unspecified, it defaults to 'a').
32479
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1507 format Use the specified format string for the handler.
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1508 datefmt Use the specified date/time format.
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1509 level Set the root logger level to the specified level.
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1510 stream Use the specified stream to initialize the StreamHandler. Note
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1511 that this argument is incompatible with 'filename' - if both
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1512 are present, 'stream' is ignored.
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1513
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1514 Note that you could specify a stream created using open(filename, mode)
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1515 rather than passing the filename and mode in. However, it should be
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1516 remembered that StreamHandler does not close its stream (since it may be
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1517 using sys.stdout or sys.stderr), whereas FileHandler closes its stream
fe80f772bfa1 Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 31597
diff changeset
1518 when the handler is closed.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1519 """
64643
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1520 # Add thread safety in case someone mistakenly calls
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1521 # basicConfig() from multiple threads
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1522 _acquireLock()
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1523 try:
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1524 if len(root.handlers) == 0:
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1525 filename = kwargs.get("filename")
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1526 if filename:
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1527 mode = kwargs.get("filemode", 'a')
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1528 hdlr = FileHandler(filename, mode)
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1529 else:
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1530 stream = kwargs.get("stream")
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1531 hdlr = StreamHandler(stream)
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1532 fs = kwargs.get("format", BASIC_FORMAT)
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1533 dfs = kwargs.get("datefmt", None)
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1534 fmt = Formatter(fs, dfs)
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1535 hdlr.setFormatter(fmt)
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1536 root.addHandler(hdlr)
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1537 level = kwargs.get("level")
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1538 if level is not None:
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1539 root.setLevel(level)
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1540 finally:
cb91307f7072 logging: Added threading interlock in basicConfig().
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64246
diff changeset
1541 _releaseLock()
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1542
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1543 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1544 # Utility functions at module level.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1545 # Basically delegate everything to the root logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1546 #---------------------------------------------------------------------------
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1547
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1548 def getLogger(name=None):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1549 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1550 Return a logger with the specified name, creating it if necessary.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1551
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1552 If no name is specified, return the root logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1553 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1554 if name:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1555 return Logger.manager.getLogger(name)
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1556 else:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1557 return root
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1558
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1559 #def getRootLogger():
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1560 # """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1561 # Return the root logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1562 #
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1563 # Note that getLogger('') now does the same thing, so this function is
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1564 # deprecated and may disappear in the future.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1565 # """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1566 # return root
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1567
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1568 def critical(msg, *args, **kwargs):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1569 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1570 Log a message with severity 'CRITICAL' on the root logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1571 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1572 if len(root.handlers) == 0:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1573 basicConfig()
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
1574 root.critical(msg, *args, **kwargs)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1575
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1576 fatal = critical
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1577
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1578 def error(msg, *args, **kwargs):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1579 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1580 Log a message with severity 'ERROR' on the root logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1581 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1582 if len(root.handlers) == 0:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1583 basicConfig()
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
1584 root.error(msg, *args, **kwargs)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1585
78424
322da186cced Issue #15541: Correct anomaly in logging.exception. Thanks to Ned Batchelder for the report.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75220
diff changeset
1586 def exception(msg, *args, **kwargs):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1587 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1588 Log a message with severity 'ERROR' on the root logger,
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1589 with exception information.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1590 """
78424
322da186cced Issue #15541: Correct anomaly in logging.exception. Thanks to Ned Batchelder for the report.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75220
diff changeset
1591 kwargs['exc_info'] = 1
322da186cced Issue #15541: Correct anomaly in logging.exception. Thanks to Ned Batchelder for the report.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 75220
diff changeset
1592 error(msg, *args, **kwargs)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1593
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
1594 def warning(msg, *args, **kwargs):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1595 """
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
1596 Log a message with severity 'WARNING' on the root logger.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1597 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1598 if len(root.handlers) == 0:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1599 basicConfig()
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
1600 root.warning(msg, *args, **kwargs)
27932
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
1601
ac683ac6759a SF patch #687683, Patches to logging (updates from Vinay)
Neal Norwitz <nnorwitz@gmail.com>
parents: 27349
diff changeset
1602 warn = warning
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1603
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1604 def info(msg, *args, **kwargs):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1605 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1606 Log a message with severity 'INFO' on the root logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1607 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1608 if len(root.handlers) == 0:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1609 basicConfig()
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
1610 root.info(msg, *args, **kwargs)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1611
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1612 def debug(msg, *args, **kwargs):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1613 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1614 Log a message with severity 'DEBUG' on the root logger.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1615 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1616 if len(root.handlers) == 0:
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1617 basicConfig()
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
1618 root.debug(msg, *args, **kwargs)
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1619
33556
fd934778f4b4 Added log() function
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33535
diff changeset
1620 def log(level, msg, *args, **kwargs):
fd934778f4b4 Added log() function
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33535
diff changeset
1621 """
fd934778f4b4 Added log() function
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33535
diff changeset
1622 Log 'msg % args' with the integer severity 'level' on the root logger.
fd934778f4b4 Added log() function
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33535
diff changeset
1623 """
fd934778f4b4 Added log() function
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33535
diff changeset
1624 if len(root.handlers) == 0:
fd934778f4b4 Added log() function
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33535
diff changeset
1625 basicConfig()
56441
6d092a51e187 Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 56280
diff changeset
1626 root.log(level, msg, *args, **kwargs)
33556
fd934778f4b4 Added log() function
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 33535
diff changeset
1627
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1628 def disable(level):
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1629 """
59666
8a4693d517fe Issue #8162: logging: Clarified docstring and documentation for disable function.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 59377
diff changeset
1630 Disable all logging calls of severity 'level' and below.
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1631 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1632 root.manager.disable = level
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1633
39439
1087df00a6e8 logging's atexit hook now runs even if the rest of the module has
Georg Brandl <georg@python.org>
parents: 38214
diff changeset
1634 def shutdown(handlerList=_handlerList):
26183
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1635 """
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1636 Perform any cleanup actions in the logging system (e.g. flushing
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1637 buffers).
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1638
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1639 Should be called at application exit.
f72b1f8684a2 Adding Vinay Sajip's logging package.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1640 """
57445
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
1641 for wr in reversed(handlerList[:]):
32856
06a63c35df9e Ignore exceptions which occur when closing files in shutdown()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32546
diff changeset
1642 #errors might occur, for example, if files are locked
36468
3cf8a9ed446b Propagate exceptions from shutdown() if raiseExceptions is not set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36456
diff changeset
1643 #we just ignore them if raiseExceptions is not set
32856
06a63c35df9e Ignore exceptions which occur when closing files in shutdown()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32546
diff changeset
1644 try:
57445
5ea226d32f6d Issue #6615: logging: Used weak references in internal handler list. Thanks to flox (Florent Xicluna) for the patch.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 57437
diff changeset
1645 h = wr()
64246
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
1646 if h:
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
1647 try:
68334
c8b651b3a493 Issue #11444: Merge fix from 2.6.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68256 68333
diff changeset
1648 h.acquire()
64246
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
1649 h.flush()
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
1650 h.close()
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
1651 except (IOError, ValueError):
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
1652 # Ignore errors which might be caused
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
1653 # because handlers have been closed but
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
1654 # references to them are still around at
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
1655 # application exit.
42c70f17c2fe Issue #9501: Fixed logging regressions in cleanup code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 62267
diff changeset
1656 pass
68334
c8b651b3a493 Issue #11444: Merge fix from 2.6.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68256 68333
diff changeset
1657 finally:
c8b651b3a493 Issue #11444: Merge fix from 2.6.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68256 68333
diff changeset
1658 h.release()
32856
06a63c35df9e Ignore exceptions which occur when closing files in shutdown()
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 32546
diff changeset
1659 except:
36468
3cf8a9ed446b Propagate exceptions from shutdown() if raiseExceptions is not set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36456
diff changeset
1660 if raiseExceptions:
3cf8a9ed446b Propagate exceptions from shutdown() if raiseExceptions is not set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36456
diff changeset
1661 raise
3cf8a9ed446b Propagate exceptions from shutdown() if raiseExceptions is not set.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 36456
diff changeset
1662 #else, swallow
31563
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
1663
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
1664 #Let's try and shutdown automatically on application exit...
58805
87cd893cb8ba Issue #7869: logging: improved format-time diagnostics and removed some 1.5.2 support code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58779
diff changeset
1665 import atexit
87cd893cb8ba Issue #7869: logging: improved format-time diagnostics and removed some 1.5.2 support code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 58779
diff changeset
1666 atexit.register(shutdown)
49955
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1667
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1668 # Null handler
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1669
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1670 class NullHandler(Handler):
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1671 """
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1672 This handler does nothing. It's intended to be used to avoid the
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1673 "No handlers could be found for logger XXX" one-off warning. This is
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1674 important for library code, which may contain code to log events. If a user
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1675 of the library does not configure logging, the one-off warning might be
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1676 produced; to avoid this, the library developer simply needs to instantiate
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1677 a NullHandler and add it to the top-level logger of the library module or
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1678 package.
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1679 """
64947
ac8aa225c417 logging: NullHandler optimisation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64941
diff changeset
1680 def handle(self, record):
ac8aa225c417 logging: NullHandler optimisation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64941
diff changeset
1681 pass
ac8aa225c417 logging: NullHandler optimisation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64941
diff changeset
1682
49955
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1683 def emit(self, record):
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1684 pass
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1685
64947
ac8aa225c417 logging: NullHandler optimisation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64941
diff changeset
1686 def createLock(self):
ac8aa225c417 logging: NullHandler optimisation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64941
diff changeset
1687 self.lock = None
ac8aa225c417 logging: NullHandler optimisation.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 64941
diff changeset
1688
49955
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1689 # Warnings integration
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1690
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1691 _warnings_showwarning = None
31563
475bf4e7920e Copyright year & version number/version date changes.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 29624
diff changeset
1692
49955
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1693 def _showwarning(message, category, filename, lineno, file=None, line=None):
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1694 """
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1695 Implementation of showwarnings which redirects to logging, which will first
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1696 check to see if the file parameter is None. If a file is specified, it will
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1697 delegate to the original warnings implementation of showwarning. Otherwise,
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1698 it will call warnings.formatwarning and will log the resulting string to a
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1699 warnings logger named "py.warnings" with level logging.WARNING.
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1700 """
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1701 if file is not None:
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1702 if _warnings_showwarning is not None:
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1703 _warnings_showwarning(message, category, filename, lineno, file, line)
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1704 else:
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1705 s = warnings.formatwarning(message, category, filename, lineno, line)
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1706 logger = getLogger("py.warnings")
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1707 if not logger.handlers:
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1708 logger.addHandler(NullHandler())
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1709 logger.warning("%s", s)
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1710
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1711 def captureWarnings(capture):
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1712 """
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1713 If capture is true, redirect all warnings to the logging package.
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1714 If capture is False, ensure that warnings are not redirected to logging
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1715 but to their original destinations.
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1716 """
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1717 global _warnings_showwarning
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1718 if capture:
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1719 if _warnings_showwarning is None:
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1720 _warnings_showwarning = warnings.showwarning
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1721 warnings.showwarning = _showwarning
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1722 else:
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1723 if _warnings_showwarning is not None:
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1724 warnings.showwarning = _warnings_showwarning
878d40468c4e Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 49009
diff changeset
1725 _warnings_showwarning = None