Kaydet (Commit) 6721a346 authored tarafından Vinay Sajip's avatar Vinay Sajip

Added optional encoding argument to File based handlers and improved error…

Added optional encoding argument to File based handlers and improved error handling for SysLogHandler
üst 2d6fe256
# 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 # Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, # documentation for any purpose and without fee is hereby granted,
...@@ -29,6 +29,11 @@ To use, simply 'import logging' and log away! ...@@ -29,6 +29,11 @@ To use, simply 'import logging' and log away!
import sys, logging, socket, types, os, string, cPickle, struct, time, glob import sys, logging, socket, types, os, string, cPickle, struct, time, glob
try:
import codecs
except ImportError:
codecs = None
# #
# Some constants... # Some constants...
# #
...@@ -45,11 +50,15 @@ class BaseRotatingHandler(logging.FileHandler): ...@@ -45,11 +50,15 @@ class BaseRotatingHandler(logging.FileHandler):
Not meant to be instantiated directly. Instead, use RotatingFileHandler Not meant to be instantiated directly. Instead, use RotatingFileHandler
or TimedRotatingFileHandler. or TimedRotatingFileHandler.
""" """
def __init__(self, filename, mode): def __init__(self, filename, mode, encoding=None):
""" """
Use the specified filename for streamed logging 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): def emit(self, record):
""" """
...@@ -70,7 +79,7 @@ class RotatingFileHandler(BaseRotatingHandler): ...@@ -70,7 +79,7 @@ class RotatingFileHandler(BaseRotatingHandler):
Handler for logging to a set of files, which switches from one file Handler for logging to a set of files, which switches from one file
to the next when the current file reaches a certain size. 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. Open the specified file and use it as the stream for logging.
...@@ -91,10 +100,9 @@ class RotatingFileHandler(BaseRotatingHandler): ...@@ -91,10 +100,9 @@ class RotatingFileHandler(BaseRotatingHandler):
If maxBytes is zero, rollover never occurs. If maxBytes is zero, rollover never occurs.
""" """
self.mode = mode
if maxBytes > 0: if maxBytes > 0:
self.mode = "a" # doesn't make sense otherwise! mode = 'a' # doesn't make sense otherwise!
BaseRotatingHandler.__init__(self, filename, self.mode) BaseRotatingHandler.__init__(self, filename, mode, encoding)
self.maxBytes = maxBytes self.maxBytes = maxBytes
self.backupCount = backupCount self.backupCount = backupCount
...@@ -118,7 +126,10 @@ class RotatingFileHandler(BaseRotatingHandler): ...@@ -118,7 +126,10 @@ class RotatingFileHandler(BaseRotatingHandler):
os.remove(dfn) os.remove(dfn)
os.rename(self.baseFilename, dfn) os.rename(self.baseFilename, dfn)
#print "%s -> %s" % (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): def shouldRollover(self, record):
""" """
...@@ -142,8 +153,8 @@ class TimedRotatingFileHandler(BaseRotatingHandler): ...@@ -142,8 +153,8 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
If backupCount is > 0, when rollover is done, no more than backupCount If backupCount is > 0, when rollover is done, no more than backupCount
files are kept - the oldest ones are deleted. files are kept - the oldest ones are deleted.
""" """
def __init__(self, filename, when='h', interval=1, backupCount=0): def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None):
BaseRotatingHandler.__init__(self, filename, 'a') BaseRotatingHandler.__init__(self, filename, 'a', encoding)
self.when = string.upper(when) self.when = string.upper(when)
self.backupCount = backupCount self.backupCount = backupCount
# Calculate the real rollover interval, which is just the number of # Calculate the real rollover interval, which is just the number of
...@@ -262,7 +273,10 @@ class TimedRotatingFileHandler(BaseRotatingHandler): ...@@ -262,7 +273,10 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
s.sort() s.sort()
os.remove(s[0]) os.remove(s[0])
#print "%s -> %s" % (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')
self.rolloverAt = int(time.time()) + self.interval self.rolloverAt = int(time.time()) + self.interval
class SocketHandler(logging.Handler): class SocketHandler(logging.Handler):
...@@ -555,14 +569,7 @@ class SysLogHandler(logging.Handler): ...@@ -555,14 +569,7 @@ class SysLogHandler(logging.Handler):
self.address = address self.address = address
self.facility = facility self.facility = facility
if type(address) == types.StringType: if type(address) == types.StringType:
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) self._connect_unixsocket(address)
# syslog may require either DGRAM or STREAM sockets
try:
self.socket.connect(address)
except socket.error:
self.socket.close()
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.socket.connect(address)
self.unixsocket = 1 self.unixsocket = 1
else: else:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
...@@ -570,6 +577,16 @@ class SysLogHandler(logging.Handler): ...@@ -570,6 +577,16 @@ class SysLogHandler(logging.Handler):
self.formatter = None self.formatter = None
def _connect_unixsocket(self, address):
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
# syslog may require either DGRAM or STREAM sockets
try:
self.socket.connect(address)
except socket.error:
self.socket.close()
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.socket.connect(address)
# curious: when talking to the unix-domain '/dev/log' socket, a # curious: when talking to the unix-domain '/dev/log' socket, a
# zero-terminator seems to be required. this string is placed # zero-terminator seems to be required. this string is placed
# into a class variable so that it can be overridden if # into a class variable so that it can be overridden if
...@@ -615,7 +632,11 @@ class SysLogHandler(logging.Handler): ...@@ -615,7 +632,11 @@ class SysLogHandler(logging.Handler):
msg) msg)
try: try:
if self.unixsocket: if self.unixsocket:
self.socket.send(msg) try:
self.socket.send(msg)
except socket.error:
self._connect_unixsocket(self.address)
self.socket.send(msg)
else: else:
self.socket.sendto(msg, self.address) self.socket.sendto(msg, self.address)
except: except:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment