Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b89e7c9

Browse files
committed
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)
1 parent a733bd9 commit b89e7c9

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

Lib/logging/__init__.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2004 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2005 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -26,7 +26,12 @@
2626
To use, simply 'import logging' and log away!
2727
"""
2828

29-
import sys, os, types, time, string, cStringIO
29+
import sys, os, types, time, string, cStringIO, traceback
30+
31+
try:
32+
import codecs
33+
except ImportError:
34+
codecs = None
3035

3136
try:
3237
import thread
@@ -37,7 +42,7 @@
3742
__author__ = "Vinay Sajip <[email protected]>"
3843
__status__ = "beta"
3944
__version__ = "0.4.9.6"
40-
__date__ = "03 February 2005"
45+
__date__ = "12 March 2005"
4146

4247
#---------------------------------------------------------------------------
4348
# Miscellaneous module data
@@ -92,6 +97,7 @@ def currentframe():
9297
# loggers are initialized with NOTSET so that they will log all messages, even
9398
# at user-defined levels.
9499
#
100+
95101
CRITICAL = 50
96102
FATAL = CRITICAL
97103
ERROR = 40
@@ -368,7 +374,6 @@ def formatException(self, ei):
368374
This default implementation just uses
369375
traceback.print_exception()
370376
"""
371-
import traceback
372377
sio = cStringIO.StringIO()
373378
traceback.print_exception(ei[0], ei[1], ei[2], None, sio)
374379
s = sio.getvalue()
@@ -674,7 +679,6 @@ def handleError(self, record):
674679
The record which was being processed is passed in to this method.
675680
"""
676681
if raiseExceptions:
677-
import traceback
678682
ei = sys.exc_info()
679683
traceback.print_exception(ei[0], ei[1], ei[2], None, sys.stderr)
680684
del ei
@@ -731,11 +735,17 @@ class FileHandler(StreamHandler):
731735
"""
732736
A handler class which writes formatted logging records to disk files.
733737
"""
734-
def __init__(self, filename, mode="a"):
738+
def __init__(self, filename, mode='a', encoding=None):
735739
"""
736740
Open the specified file and use it as the stream for logging.
737741
"""
738-
StreamHandler.__init__(self, open(filename, mode))
742+
if codecs is None:
743+
encoding = None
744+
if encoding is None:
745+
stream = open(filename, mode)
746+
else:
747+
stream = codecs.open(filename, mode, encoding)
748+
StreamHandler.__init__(self, stream)
739749
#keep the absolute path, otherwise derived classes which use this
740750
#may come a cropper when the current directory changes
741751
self.baseFilename = os.path.abspath(filename)
@@ -1164,7 +1174,7 @@ def basicConfig(**kwargs):
11641174
filename Specifies that a FileHandler be created, using the specified
11651175
filename, rather than a StreamHandler.
11661176
filemode Specifies the mode to open the file, if filename is specified
1167-
(if filemode is unspecified, it defaults to "a").
1177+
(if filemode is unspecified, it defaults to 'a').
11681178
format Use the specified format string for the handler.
11691179
datefmt Use the specified date/time format.
11701180
level Set the root logger level to the specified level.
@@ -1181,7 +1191,7 @@ def basicConfig(**kwargs):
11811191
if len(root.handlers) == 0:
11821192
filename = kwargs.get("filename")
11831193
if filename:
1184-
mode = kwargs.get("filemode", "a")
1194+
mode = kwargs.get("filemode", 'a')
11851195
hdlr = FileHandler(filename, mode)
11861196
else:
11871197
stream = kwargs.get("stream")

0 commit comments

Comments
 (0)