36
36
37
37
__author__ = "Vinay Sajip <[email protected] >"
38
38
__status__ = "alpha"
39
- __version__ = "0.4.7 "
40
- __date__ = "27 August 2002 "
39
+ __version__ = "0.4.8 "
40
+ __date__ = "16 February 2003 "
41
41
42
42
#---------------------------------------------------------------------------
43
43
# Miscellaneous module data
44
44
#---------------------------------------------------------------------------
45
45
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
+
46
53
#
47
54
#_srcfile is used when walking the stack to check when we've got the first
48
55
# caller stack frame.
56
+ #
49
57
if string .lower (__file__ [- 4 :]) in ['.pyc' , '.pyo' ]:
50
58
_srcfile = __file__ [:- 4 ] + '.py'
51
59
else :
70
78
#
71
79
raiseExceptions = 1
72
80
73
-
74
81
#---------------------------------------------------------------------------
75
82
# Level related stuff
76
83
#---------------------------------------------------------------------------
84
91
CRITICAL = 50
85
92
FATAL = CRITICAL
86
93
ERROR = 40
87
- WARN = 30
94
+ WARNING = 30
95
+ WARN = WARNING
88
96
INFO = 20
89
97
DEBUG = 10
90
98
NOTSET = 0
91
99
92
100
_levelNames = {
93
101
CRITICAL : 'CRITICAL' ,
94
102
ERROR : 'ERROR' ,
95
- WARN : 'WARN ' ,
103
+ WARNING : 'WARNING ' ,
96
104
INFO : 'INFO' ,
97
105
DEBUG : 'DEBUG' ,
98
106
NOTSET : 'NOTSET' ,
99
107
'CRITICAL' : CRITICAL ,
100
108
'ERROR' : ERROR ,
101
- 'WARN' : WARN ,
109
+ 'WARN' : WARNING ,
110
+ 'WARNING' : WARNING ,
102
111
'INFO' : INFO ,
103
112
'DEBUG' : DEBUG ,
104
113
'NOTSET' : NOTSET ,
@@ -108,7 +117,7 @@ def getLevelName(level):
108
117
"""
109
118
Return the textual representation of logging level 'level'.
110
119
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 ,
112
121
INFO, DEBUG) then you get the corresponding string. If you have
113
122
associated levels with names using addLevelName then the name you have
114
123
associated with 'level' is returned. Otherwise, the string
@@ -204,6 +213,7 @@ def __init__(self, name, level, pathname, lineno, msg, args, exc_info):
204
213
self .thread = thread .get_ident ()
205
214
else :
206
215
self .thread = None
216
+ self .process = os .getpid ()
207
217
208
218
def __str__ (self ):
209
219
return '<LogRecord: %s, %s, %s, %s, "%s">' % (self .name , self .levelno ,
@@ -216,7 +226,13 @@ def getMessage(self):
216
226
Return the message for this LogRecord after merging any user-supplied
217
227
arguments with the message.
218
228
"""
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
220
236
if self .args :
221
237
msg = msg % self .args
222
238
return msg
@@ -243,9 +259,9 @@ class Formatter:
243
259
244
260
%(name)s Name of the logger (logging channel)
245
261
%(levelno)s Numeric logging level for the message (DEBUG, INFO,
246
- WARN , ERROR, CRITICAL)
262
+ WARNING , ERROR, CRITICAL)
247
263
%(levelname)s Text logging level for the message ("DEBUG", "INFO",
248
- "WARN ", "ERROR", "CRITICAL")
264
+ "WARNING ", "ERROR", "CRITICAL")
249
265
%(pathname)s Full pathname of the source file where the logging
250
266
call was issued (if available)
251
267
%(filename)s Filename portion of pathname
@@ -260,6 +276,7 @@ class Formatter:
260
276
relative to the time the logging module was loaded
261
277
(typically at application startup time)
262
278
%(thread)d Thread ID (if available)
279
+ %(process)d Process ID (if available)
263
280
%(message)s The result of record.getMessage(), computed just as
264
281
the record is emitted
265
282
"""
@@ -558,14 +575,17 @@ def handle(self, record):
558
575
559
576
Emission depends on filters which may have been added to the handler.
560
577
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.
562
580
"""
563
- if self .filter (record ):
581
+ rv = self .filter (record )
582
+ if rv :
564
583
self .acquire ()
565
584
try :
566
585
self .emit (record )
567
586
finally :
568
587
self .release ()
588
+ return rv
569
589
570
590
def setFormatter (self , fmt ):
571
591
"""
@@ -591,17 +611,17 @@ def close(self):
591
611
"""
592
612
pass
593
613
594
- def handleError (self ):
614
+ def handleError (self , record ):
595
615
"""
596
616
Handle errors which occur during an emit() call.
597
617
598
618
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,
601
620
exceptions get silently ignored. This is what is mostly wanted
602
621
for a logging system - most users will not care about errors in
603
622
the logging system, they are more interested in application errors.
604
623
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.
605
625
"""
606
626
if raiseExceptions :
607
627
import traceback
@@ -645,10 +665,16 @@ def emit(self, record):
645
665
"""
646
666
try :
647
667
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" ))
649
675
self .flush ()
650
676
except :
651
- self .handleError ()
677
+ self .handleError (record )
652
678
653
679
class FileHandler (StreamHandler ):
654
680
"""
@@ -861,19 +887,21 @@ def info(self, msg, *args, **kwargs):
861
887
if INFO >= self .getEffectiveLevel ():
862
888
apply (self ._log , (INFO , msg , args ), kwargs )
863
889
864
- def warn (self , msg , * args , ** kwargs ):
890
+ def warning (self , msg , * args , ** kwargs ):
865
891
"""
866
- Log 'msg % args' with severity 'WARN '.
892
+ Log 'msg % args' with severity 'WARNING '.
867
893
868
894
To pass exception information, use the keyword argument exc_info with
869
895
a true value, e.g.
870
896
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)
872
898
"""
873
- if self .manager .disable >= WARN :
899
+ if self .manager .disable >= WARNING :
874
900
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
877
905
878
906
def error (self , msg , * args , ** kwargs ):
879
907
"""
@@ -982,7 +1010,7 @@ def removeHandler(self, hdlr):
982
1010
Remove the specified handler from this logger.
983
1011
"""
984
1012
if hdlr in self .handlers :
985
- hdlr .close ()
1013
+ # hdlr.close()
986
1014
self .handlers .remove (hdlr )
987
1015
988
1016
def callHandlers (self , record ):
@@ -1047,7 +1075,7 @@ def __init__(self, level):
1047
1075
1048
1076
_loggerClass = Logger
1049
1077
1050
- root = RootLogger (WARN )
1078
+ root = RootLogger (WARNING )
1051
1079
Logger .root = root
1052
1080
Logger .manager = Manager (Logger .root )
1053
1081
@@ -1119,13 +1147,15 @@ def exception(msg, *args):
1119
1147
"""
1120
1148
apply (error , (msg ,)+ args , {'exc_info' : 1 })
1121
1149
1122
- def warn (msg , * args , ** kwargs ):
1150
+ def warning (msg , * args , ** kwargs ):
1123
1151
"""
1124
- Log a message with severity 'WARN ' on the root logger.
1152
+ Log a message with severity 'WARNING ' on the root logger.
1125
1153
"""
1126
1154
if len (root .handlers ) == 0 :
1127
1155
basicConfig ()
1128
- apply (root .warn , (msg ,)+ args , kwargs )
1156
+ apply (root .warning , (msg ,)+ args , kwargs )
1157
+
1158
+ warn = warning
1129
1159
1130
1160
def info (msg , * args , ** kwargs ):
1131
1161
"""
0 commit comments