Skip to content

Commit 6fa635d

Browse files
committed
SF patch #687683, Patches to logging (updates from Vinay)
Mostly rename WARN -> WARNING Other misc tweaks Update tests (not in original patch)
1 parent d6a3f93 commit 6fa635d

File tree

5 files changed

+98
-63
lines changed

5 files changed

+98
-63
lines changed

Doc/lib/liblogging.tex

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,9 @@ \subsection{Logger Objects}
250250
\begin{methoddesc}{setLevel}{lvl}
251251
Sets the threshold for this logger to \var{lvl}. Logging messages
252252
which are less severe than \var{lvl} will be ignored. When a logger is
253-
created, the level is set to \constant{ALL} (which causes all messages
254-
to be processed).
253+
created, the level is set to \constant{NOTSET} (which causes all messages
254+
to be processed in the root logger, or delegation to the parent in non-root
255+
loggers).
255256
\end{methoddesc}
256257

257258
\begin{methoddesc}{isEnabledFor}{lvl}
@@ -263,9 +264,9 @@ \subsection{Logger Objects}
263264

264265
\begin{methoddesc}{getEffectiveLevel}{}
265266
Indicates the effective level for this logger. If a value other than
266-
\constant{ALL} has been set using \method{setLevel()}, it is returned.
267+
\constant{NOTSET} has been set using \method{setLevel()}, it is returned.
267268
Otherwise, the hierarchy is traversed towards the root until a value
268-
other than \constant{ALL} is found, and that value is returned.
269+
other than \constant{NOTSET} is found,and that value is returned.
269270
\end{methoddesc}
270271

271272
\begin{methoddesc}{debug}{msg\optional{, *args\optional{, **kwargs}}}
@@ -355,7 +356,7 @@ \subsection{Handler Objects}
355356
base for more useful subclasses. However, the \method{__init__()}
356357
method in subclasses needs to call \method{Handler.__init__()}.
357358

358-
\begin{methoddesc}{__init__}{level=\constant{ALL}}
359+
\begin{methoddesc}{__init__}{level=\constant{NOTSET}}
359360
Initializes the \class{Handler} instance by setting its level, setting
360361
the list of filters to the empty list and creating a lock (using
361362
\method{getLock()}) for serializing access to an I/O mechanism.
@@ -377,7 +378,7 @@ \subsection{Handler Objects}
377378
\begin{methoddesc}{setLevel}{lvl}
378379
Sets the threshold for this handler to \var{lvl}. Logging messages which are
379380
less severe than \var{lvl} will be ignored. When a handler is created, the
380-
level is set to \constant{ALL} (which causes all messages to be processed).
381+
level is set to \constant{NOTSET} (which causes all messages to be processed).
381382
\end{methoddesc}
382383

383384
\begin{methoddesc}{setFormatter}{form}
@@ -487,7 +488,7 @@ \subsubsection{RotatingFileHandler}
487488
The \class{RotatingFileHandler} class supports rotation of disk log files.
488489

489490
\begin{classdesc}{RotatingFileHandler}{filename\optional{, mode, maxBytes,
490-
backupCount}}
491+
backupCount}}
491492
Returns a new instance of the \class{RotatingFileHandler} class. The
492493
specified file is opened and used as the stream for logging. If
493494
\var{mode} is not specified, \code{'a'} is used. By default, the
@@ -736,7 +737,7 @@ \subsubsection{MemoryHandler}
736737
\end{methoddesc}
737738

738739
\begin{classdesc}{MemoryHandler}{capacity\optional{, flushLevel
739-
\optional{, target}}}
740+
\optional{, target}}}
740741
Returns a new instance of the \class{MemoryHandler} class. The
741742
instance is initialized with a buffer size of \var{capacity}. If
742743
\var{flushLevel} is not specified, \constant{ERROR} is used. If no
@@ -813,10 +814,10 @@ \subsection{Formatter Objects}
813814
relative to the time the logging module was loaded
814815
(typically at application startup time)
815816
\%(thread)d Thread ID (if available)
817+
\%(process)d Process ID (if available)
816818
\%(message)s The result of msg \% args, computed just as the
817819
record is emitted
818820

819-
820821
\begin{classdesc}{Formatter}{\optional{fmt\optional{, datefmt}}}
821822
Returns a new instance of the \class{Formatter} class. The
822823
instance is initialized with a format string for the message as a whole,
@@ -889,7 +890,7 @@ \subsection{LogRecord Objects}
889890
facilitate extension.
890891

891892
\begin{classdesc}{LogRecord}{name, lvl, pathname, lineno, msg, args,
892-
exc_info}
893+
exc_info}
893894
Returns an instance of \class{LogRecord} initialized with interesting
894895
information. The \var{name} is the logger name; \var{lvl} is the
895896
numeric level; \var{pathname} is the absolute pathname of the source

Lib/logging/__init__.py

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,24 @@
3636

3737
__author__ = "Vinay Sajip <[email protected]>"
3838
__status__ = "alpha"
39-
__version__ = "0.4.7"
40-
__date__ = "27 August 2002"
39+
__version__ = "0.4.8"
40+
__date__ = "16 February 2003"
4141

4242
#---------------------------------------------------------------------------
4343
# Miscellaneous module data
4444
#---------------------------------------------------------------------------
4545

46+
#
47+
# _verinfo is used for when behaviour needs to be adjusted to the version
48+
# of Python
49+
#
50+
51+
_verinfo = getattr(sys, "version_info", None)
52+
4653
#
4754
#_srcfile is used when walking the stack to check when we've got the first
4855
# caller stack frame.
56+
#
4957
if string.lower(__file__[-4:]) in ['.pyc', '.pyo']:
5058
_srcfile = __file__[:-4] + '.py'
5159
else:
@@ -70,7 +78,6 @@
7078
#
7179
raiseExceptions = 1
7280

73-
7481
#---------------------------------------------------------------------------
7582
# Level related stuff
7683
#---------------------------------------------------------------------------
@@ -84,21 +91,23 @@
8491
CRITICAL = 50
8592
FATAL = CRITICAL
8693
ERROR = 40
87-
WARN = 30
94+
WARNING = 30
95+
WARN = WARNING
8896
INFO = 20
8997
DEBUG = 10
9098
NOTSET = 0
9199

92100
_levelNames = {
93101
CRITICAL : 'CRITICAL',
94102
ERROR : 'ERROR',
95-
WARN : 'WARN',
103+
WARNING : 'WARNING',
96104
INFO : 'INFO',
97105
DEBUG : 'DEBUG',
98106
NOTSET : 'NOTSET',
99107
'CRITICAL' : CRITICAL,
100108
'ERROR' : ERROR,
101-
'WARN' : WARN,
109+
'WARN' : WARNING,
110+
'WARNING' : WARNING,
102111
'INFO' : INFO,
103112
'DEBUG' : DEBUG,
104113
'NOTSET' : NOTSET,
@@ -108,7 +117,7 @@ def getLevelName(level):
108117
"""
109118
Return the textual representation of logging level 'level'.
110119
111-
If the level is one of the predefined levels (CRITICAL, ERROR, WARN,
120+
If the level is one of the predefined levels (CRITICAL, ERROR, WARNING,
112121
INFO, DEBUG) then you get the corresponding string. If you have
113122
associated levels with names using addLevelName then the name you have
114123
associated with 'level' is returned. Otherwise, the string
@@ -204,6 +213,7 @@ def __init__(self, name, level, pathname, lineno, msg, args, exc_info):
204213
self.thread = thread.get_ident()
205214
else:
206215
self.thread = None
216+
self.process = os.getpid()
207217

208218
def __str__(self):
209219
return '<LogRecord: %s, %s, %s, %s, "%s">'%(self.name, self.levelno,
@@ -216,7 +226,13 @@ def getMessage(self):
216226
Return the message for this LogRecord after merging any user-supplied
217227
arguments with the message.
218228
"""
219-
msg = str(self.msg)
229+
if not hasattr(types, "UnicodeType"): #if no unicode support...
230+
msg = str(self.msg)
231+
else:
232+
try:
233+
msg = str(self.msg)
234+
except UnicodeError:
235+
msg = self.msg #Defer encoding till later
220236
if self.args:
221237
msg = msg % self.args
222238
return msg
@@ -243,9 +259,9 @@ class Formatter:
243259
244260
%(name)s Name of the logger (logging channel)
245261
%(levelno)s Numeric logging level for the message (DEBUG, INFO,
246-
WARN, ERROR, CRITICAL)
262+
WARNING, ERROR, CRITICAL)
247263
%(levelname)s Text logging level for the message ("DEBUG", "INFO",
248-
"WARN", "ERROR", "CRITICAL")
264+
"WARNING", "ERROR", "CRITICAL")
249265
%(pathname)s Full pathname of the source file where the logging
250266
call was issued (if available)
251267
%(filename)s Filename portion of pathname
@@ -260,6 +276,7 @@ class Formatter:
260276
relative to the time the logging module was loaded
261277
(typically at application startup time)
262278
%(thread)d Thread ID (if available)
279+
%(process)d Process ID (if available)
263280
%(message)s The result of record.getMessage(), computed just as
264281
the record is emitted
265282
"""
@@ -558,14 +575,17 @@ def handle(self, record):
558575
559576
Emission depends on filters which may have been added to the handler.
560577
Wrap the actual emission of the record with acquisition/release of
561-
the I/O thread lock.
578+
the I/O thread lock. Returns whether the filter passed the record for
579+
emission.
562580
"""
563-
if self.filter(record):
581+
rv = self.filter(record)
582+
if rv:
564583
self.acquire()
565584
try:
566585
self.emit(record)
567586
finally:
568587
self.release()
588+
return rv
569589

570590
def setFormatter(self, fmt):
571591
"""
@@ -591,17 +611,17 @@ def close(self):
591611
"""
592612
pass
593613

594-
def handleError(self):
614+
def handleError(self, record):
595615
"""
596616
Handle errors which occur during an emit() call.
597617
598618
This method should be called from handlers when an exception is
599-
encountered during an emit() call. By default it does nothing,
600-
because by default raiseExceptions is false, which means that
619+
encountered during an emit() call. If raiseExceptions is false,
601620
exceptions get silently ignored. This is what is mostly wanted
602621
for a logging system - most users will not care about errors in
603622
the logging system, they are more interested in application errors.
604623
You could, however, replace this with a custom handler if you wish.
624+
The record which was being processed is passed in to this method.
605625
"""
606626
if raiseExceptions:
607627
import traceback
@@ -645,10 +665,16 @@ def emit(self, record):
645665
"""
646666
try:
647667
msg = self.format(record)
648-
self.stream.write("%s\n" % msg)
668+
if not hasattr(types, "UnicodeType"): #if no unicode support...
669+
self.stream.write("%s\n" % msg)
670+
else:
671+
try:
672+
self.stream.write("%s\n" % msg)
673+
except UnicodeError:
674+
self.stream.write("%s\n" % msg.encode("UTF-8"))
649675
self.flush()
650676
except:
651-
self.handleError()
677+
self.handleError(record)
652678

653679
class FileHandler(StreamHandler):
654680
"""
@@ -861,19 +887,21 @@ def info(self, msg, *args, **kwargs):
861887
if INFO >= self.getEffectiveLevel():
862888
apply(self._log, (INFO, msg, args), kwargs)
863889

864-
def warn(self, msg, *args, **kwargs):
890+
def warning(self, msg, *args, **kwargs):
865891
"""
866-
Log 'msg % args' with severity 'WARN'.
892+
Log 'msg % args' with severity 'WARNING'.
867893
868894
To pass exception information, use the keyword argument exc_info with
869895
a true value, e.g.
870896
871-
logger.warn("Houston, we have a %s", "bit of a problem", exc_info=1)
897+
logger.warning("Houston, we have a %s", "bit of a problem", exc_info=1)
872898
"""
873-
if self.manager.disable >= WARN:
899+
if self.manager.disable >= WARNING:
874900
return
875-
if self.isEnabledFor(WARN):
876-
apply(self._log, (WARN, msg, args), kwargs)
901+
if self.isEnabledFor(WARNING):
902+
apply(self._log, (WARNING, msg, args), kwargs)
903+
904+
warn = warning
877905

878906
def error(self, msg, *args, **kwargs):
879907
"""
@@ -982,7 +1010,7 @@ def removeHandler(self, hdlr):
9821010
Remove the specified handler from this logger.
9831011
"""
9841012
if hdlr in self.handlers:
985-
hdlr.close()
1013+
#hdlr.close()
9861014
self.handlers.remove(hdlr)
9871015

9881016
def callHandlers(self, record):
@@ -1047,7 +1075,7 @@ def __init__(self, level):
10471075

10481076
_loggerClass = Logger
10491077

1050-
root = RootLogger(WARN)
1078+
root = RootLogger(WARNING)
10511079
Logger.root = root
10521080
Logger.manager = Manager(Logger.root)
10531081

@@ -1119,13 +1147,15 @@ def exception(msg, *args):
11191147
"""
11201148
apply(error, (msg,)+args, {'exc_info': 1})
11211149

1122-
def warn(msg, *args, **kwargs):
1150+
def warning(msg, *args, **kwargs):
11231151
"""
1124-
Log a message with severity 'WARN' on the root logger.
1152+
Log a message with severity 'WARNING' on the root logger.
11251153
"""
11261154
if len(root.handlers) == 0:
11271155
basicConfig()
1128-
apply(root.warn, (msg,)+args, kwargs)
1156+
apply(root.warning, (msg,)+args, kwargs)
1157+
1158+
warn = warning
11291159

11301160
def info(msg, *args, **kwargs):
11311161
"""

0 commit comments

Comments
 (0)