[Python-checkins] python/dist/src/Lib/logging handlers.py, 1.20, 1.21

vsajip at users.sourceforge.net vsajip at users.sourceforge.net
Sun Mar 13 10:56:39 CET 2005


Update of /cvsroot/python/python/dist/src/Lib/logging
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30422
Modified Files:
	handlers.py 
Log Message:
Added optional encoding argument to file handlers.
Index: handlers.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/logging/handlers.py,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- handlers.py	13 Jan 2005 08:23:56 -0000	1.20
+++ handlers.py	13 Mar 2005 09:56:36 -0000	1.21
@@ -1,4 +1,4 @@
-# Copyright 2001-2004 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2005 by Vinay Sajip. All Rights Reserved.
 #
 # Permission to use, copy, modify, and distribute this software and its
 # documentation for any purpose and without fee is hereby granted,
@@ -29,6 +29,11 @@
 
 import sys, logging, socket, types, os, string, cPickle, struct, time, glob
 
+try:
+ import codecs
+except ImportError:
+ codecs = None
+
 #
 # Some constants...
 #
@@ -45,11 +50,15 @@
 Not meant to be instantiated directly. Instead, use RotatingFileHandler
 or TimedRotatingFileHandler.
 """
- def __init__(self, filename, mode):
+ def __init__(self, filename, mode, encoding=None):
 """
 Use the specified filename for streamed logging
 """
- logging.FileHandler.__init__(self, filename, mode)
+ if codecs is None:
+ encoding = None
+ logging.FileHandler.__init__(self, filename, mode, encoding)
+ self.mode = mode
+ self.encoding = encoding
 
 def emit(self, record):
 """
@@ -70,7 +79,7 @@
 Handler for logging to a set of files, which switches from one file
 to the next when the current file reaches a certain size.
 """
- def __init__(self, filename, mode="a", maxBytes=0, backupCount=0):
+ def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None):
 """
 Open the specified file and use it as the stream for logging.
 
@@ -91,10 +100,9 @@
 
 If maxBytes is zero, rollover never occurs.
 """
- self.mode = mode
 if maxBytes > 0:
- self.mode = "a" # doesn't make sense otherwise!
- BaseRotatingHandler.__init__(self, filename, self.mode)
+ mode = 'a' # doesn't make sense otherwise!
+ BaseRotatingHandler.__init__(self, filename, mode, encoding)
 self.maxBytes = maxBytes
 self.backupCount = backupCount
 
@@ -118,7 +126,10 @@
 os.remove(dfn)
 os.rename(self.baseFilename, dfn)
 #print "%s -> %s" % (self.baseFilename, dfn)
- self.stream = open(self.baseFilename, "w")
+ if self.encoding:
+ self.stream = codecs.open(self.baseFilename, 'w', self.encoding)
+ else:
+ self.stream = open(self.baseFilename, 'w')
 
 def shouldRollover(self, record):
 """
@@ -142,8 +153,8 @@
 If backupCount is > 0, when rollover is done, no more than backupCount
 files are kept - the oldest ones are deleted.
 """
- def __init__(self, filename, when='h', interval=1, backupCount=0):
- BaseRotatingHandler.__init__(self, filename, 'a')
+ def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None):
+ BaseRotatingHandler.__init__(self, filename, 'a', encoding)
 self.when = string.upper(when)
 self.backupCount = backupCount
 # Calculate the real rollover interval, which is just the number of
@@ -262,7 +273,10 @@
 s.sort()
 os.remove(s[0])
 #print "%s -> %s" % (self.baseFilename, dfn)
- self.stream = open(self.baseFilename, "w")
+ if self.encoding:
+ self.stream = codecs.open(self.baseFilename, 'w', self.encoding)
+ else:
+ self.stream = open(self.baseFilename, 'w')
 self.rolloverAt = int(time.time()) + self.interval
 
 class SocketHandler(logging.Handler):


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /