Fixed python2.6 compat for RFCSysLogHandler
(From the original bug report): Currently used pattern in RFCSysLogHandler will fail for Python 2.6.x (using super(RFCSysLogHandler, self)). In order to fix the broken Python 2.6.x compatibility, old style explicit superclass method calls should be used instead. Uses e.g. logging.handlers.SysLogHandler.__init__(self,...) rather than super(RFCSysLogHandler, self).__init__(...) Change-Id: Ia81e00e4a7763a887703d0ef83e475f314eab28d Closes-Bug: #1306559
This commit is contained in:
parent
915c5983fc
commit
3ba00c25e1
@ -499,10 +499,16 @@ def _find_facility_from_conf():
|
||||
class RFCSysLogHandler(logging.handlers.SysLogHandler):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.binary_name = _get_binary_name()
|
||||
super(RFCSysLogHandler, self).__init__(*args, **kwargs)
|
||||
# Do not use super() unless type(logging.handlers.SysLogHandler)
|
||||
# is 'type' (Python 2.7).
|
||||
# Use old style calls, if the type is 'classobj' (Python 2.6)
|
||||
logging.handlers.SysLogHandler.__init__(self, *args, **kwargs)
|
||||
|
||||
def format(self, record):
|
||||
msg = super(RFCSysLogHandler, self).format(record)
|
||||
# Do not use super() unless type(logging.handlers.SysLogHandler)
|
||||
# is 'type' (Python 2.7).
|
||||
# Use old style calls, if the type is 'classobj' (Python 2.6)
|
||||
msg = logging.handlers.SysLogHandler.format(self, record)
|
||||
msg = self.binary_name + ' ' + msg
|
||||
return msg
|
||||
|
||||
|
47
muranoapi/tests/test_log.py
Normal file
47
muranoapi/tests/test_log.py
Normal file
@ -0,0 +1,47 @@
|
||||
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
from muranoapi.openstack.common import log
|
||||
|
||||
|
||||
class SysLogHandlersTestCase(unittest.TestCase):
|
||||
"""Test for standard and RFC compliant Syslog handlers."""
|
||||
def setUp(self):
|
||||
self.facility = logging.handlers.SysLogHandler.LOG_USER
|
||||
self.rfclogger = log.RFCSysLogHandler(address='/dev/log',
|
||||
facility=self.facility)
|
||||
self.rfclogger.binary_name = 'Foo_application'
|
||||
self.logger = logging.handlers.SysLogHandler(address='/dev/log',
|
||||
facility=self.facility)
|
||||
self.logger.binary_name = 'Foo_application'
|
||||
|
||||
def test_rfc_format(self):
|
||||
"""Ensure syslog msg contains APP-NAME for RFC wrapped handler"""
|
||||
logrecord = logging.LogRecord('name', 'WARN', '/tmp', 1,
|
||||
'Message', None, None)
|
||||
expected = logging.LogRecord('name', 'WARN', '/tmp', 1,
|
||||
'Foo_application Message', None, None)
|
||||
self.assertEqual(self.rfclogger.format(logrecord),
|
||||
expected.getMessage())
|
||||
|
||||
def test_standard_format(self):
|
||||
"""Ensure syslog msg isn't modified for standard handler"""
|
||||
logrecord = logging.LogRecord('name', 'WARN', '/tmp', 1,
|
||||
'Message', None, None)
|
||||
expected = logrecord
|
||||
self.assertEqual(self.logger.format(logrecord),
|
||||
expected.getMessage())
|
Loading…
x
Reference in New Issue
Block a user