Commit e50d232a authored by Jerry's avatar Jerry
Browse files

Fixed rotate file handler issue.



git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac176@1966 e5f2f494-b856-4b98-b285-d166d9295462
parent 8cd3eaf9
......@@ -380,7 +380,7 @@ class XfroutServer:
if self._log:
self._log.update_config(self._config_data.get('log_file'), self._config_data.get('severity'),
self._config_data.get('max_bytes'), self._config_data.get('versions'))
self._config_data.get('versions'), self._config_data.get('max_bytes'))
if self._unix_socket_server:
self._unix_socket_server.update_config_data(self._config_data)
......
......@@ -50,13 +50,13 @@ class RotatingFileHandler(logging.handlers.RotatingFileHandler):
if (self.stream) and (not os.path.exists(dfn)): #Is log file exist?
self.stream.close()
self.stream = self._open()
super(RotatingFileHandler, self).shouldRollover(record)
return super(RotatingFileHandler, self).shouldRollover(record)
def update_config(self, file_name, backup_count, max_bytes):
"""
Update RotatingFileHandler configuration.
If the file path is not exists, we will use the old configuration.
If the file path is not exist, we will use the old log file.
input:
log file name
max backup count
......@@ -65,8 +65,8 @@ class RotatingFileHandler(logging.handlers.RotatingFileHandler):
dir = os.path.split(file_name)
if(os.path.exists(dir[0])):
self.baseFilename = file_name
self.maxBytes = max_bytes
self.backupCount = backup_count
self.maxBytes = max_bytes
self.backupCount = backup_count
class SLHandler(logging.Handler):
"""
......@@ -122,8 +122,11 @@ class ModuleLogger(logging.getLoggerClass()):
log_to_console = True):
"""
Initializes the logger with some specific parameters
If log_to_console is Ture, stream handler will be used;
else syslog handler will be used.
"""
logging.Logger.__init__(self, log_name)
super(ModuleLogger, self).__init__(log_name)
# Set up a specific logger with our desired output level
logLevel = LEVELS.get(severity, logging.NOTSET)
......@@ -135,7 +138,7 @@ class ModuleLogger(logging.getLoggerClass()):
self.syslog_handler = None
self.add_null_handler()
self.add_rotate_handler(log_file, max_bytes, backup_count)
self.add_rotate_handler(log_file, backup_count, max_bytes)
if log_to_console:
self.add_stream_handler()
else:
......@@ -148,18 +151,20 @@ class ModuleLogger(logging.getLoggerClass()):
self.null_handler = logging.NullHandler()
self.addHandler(self.null_handler)
def add_rotate_handler(self, log_file, max_bytes, backup_count):
def add_rotate_handler(self, log_file, backup_count, max_bytes):
"""
Add a rotate file handler.
input:
log_file : the location of log file.Handler would't be created is log_file is empty
log_file : the location of log file. Handler wouldn't be created
if log_file is not specified
max_bytes : limit log growth
backup_count : max backup count
"""
if(log_file != 0 and log_file != ''):
try:
self.rotating_handler = RotatingFileHandler(filename = log_file, maxBytes = max_bytes,
self.rotating_handler = RotatingFileHandler(filename = log_file,
maxBytes = max_bytes,
backupCount = backup_count)
except IOError:
self.rotating_handler = None
......@@ -186,29 +191,31 @@ class ModuleLogger(logging.getLoggerClass()):
"""
self.syslog_handler = SLHandler('BIND10', facility=syslog.LOG_USER)
self.syslog_handler.setFormatter(formatter)
#set syslog handler level info
#set syslog handler severity level INFO
self.syslog_handler.setLevel(logging.INFO)
self.addHandler(self.syslog_handler)
def update_rotate_handler(self, log_file, max_bytes, backup_count):
def update_rotate_handler(self, log_file, backup_count, max_bytes):
"""
If the rotate handler has been added to the logger, update its configuration,
else add it to the logger.
If the rotate file handler has been added to the logger, update its
configuration, or add it to the logger.
If log file is empty, the handler will be removed.
"""
if (self.rotating_handler in self.handlers):
if(log_file != 0 and log_file != ''):
self.rotating_handler.update_config(log_file, max_bytes, backup_count)
self.rotating_handler.update_config(log_file, backup_count, max_bytes)
else:
"""
If log file is empty, the handler will be removed.
"""
self.rotating_handler.flush()
self.rotating_handler.close()
self.removeHandler(self.rotating_handler)
else:
self.add_rotate_handler(log_file, max_bytes, backup_count)
self.add_rotate_handler(log_file, backup_count, max_bytes)
def update_config(self, file_name, level, max_bytes, backup_count):
def update_config(self, file_name, level, backup_count, max_bytes):
"""
Update logger's configuration.
......
......@@ -5,7 +5,8 @@ import os
class TestRotateFileHandler(unittest.TestCase):
def setUp(self):
self.handler = RotatingFileHandler('/var/log/rotate_file_handler.log', 1024, 5)
self.handler = RotatingFileHandler(filename = '/var/log/rotate_file_handler.log',
maxBytes = 1024, backupCount = 5)
def test_shouldRollover(self):
if(os.path.exists('/var/log/rotate_file_handler.log')):
......@@ -15,10 +16,15 @@ class TestRotateFileHandler(unittest.TestCase):
self.assertTrue(os.path.exists('/var/log/rotate_file_handler.log'))
def test_update_config(self):
self.handler.update_config('/var/log/rotate_file_handler2.log', 512, 3)
self.handler.update_config('/var/log/rotate_file_handler2.log', 3, 512)
self.assertEqual(self.handler.baseFilename, '/var/log/rotate_file_handler2.log')
self.assertEqual(self.handler.maxBytes, 3)
self.assertEqual(self.handler.backupCount, 512)
self.assertEqual(self.handler.maxBytes, 512)
self.assertEqual(self.handler.backupCount, 3)
self.handler.update_config('/var/ZZZXXX/rotate_file_handler2.log', 4, 1024)
self.assertEqual(self.handler.baseFilename, '/var/log/rotate_file_handler2.log')
self.assertEqual(self.handler.maxBytes, 1024)
self.assertEqual(self.handler.backupCount, 4)
class TestLogging(unittest.TestCase):
......@@ -74,11 +80,11 @@ class TestLogging(unittest.TestCase):
if(self.syslog_logger.rotating_handler in self.syslog_logger.handlers):
self.syslog_logger.removeHandler(self.syslog_logger.rotating_handler)
self.syslog_logger.add_rotate_handler('', 1024, 5)
self.syslog_logger.add_rotate_handler('', 5, 1024)
ret = self.syslog_logger.rotating_handler in self.syslog_logger.handlers
self.assertFalse(ret)
self.syslog_logger.add_rotate_handler('/var/log/RotateFile.log', 1024, 5)
self.syslog_logger.add_rotate_handler('/var/log/RotateFile.log', 5, 1024)
ret = self.syslog_logger.rotating_handler in self.syslog_logger.handlers
self.assertTrue(ret)
......@@ -109,7 +115,7 @@ class TestLogging(unittest.TestCase):
self.file_stream_logger.update_rotate_handler('/var/log/RotateFile', 4, 1024)
ret = self.file_stream_logger.rotating_handler in self.file_stream_logger.handlers
self.assertFalse(ret)
self.assertTrue(ret)
def test_update_config(self):
self.file_stream_logger.update_config('/var/log/RotateFile','error', 4, 1024)
......
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