diff --git a/swift/common/utils.py b/swift/common/utils.py index 26f7c37ead..a4d70552bb 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -675,7 +675,24 @@ class SwiftLogFormatter(logging.Formatter): # Catch log messages that were not initiated by swift # (for example, the keystone auth middleware) record.server = record.name - msg = logging.Formatter.format(self, record) + + # Included from Python's logging.Formatter and then altered slightly to + # replace \n with #012 + record.message = record.getMessage() + if self._fmt.find('%(asctime)') >= 0: + record.asctime = self.formatTime(record, self.datefmt) + msg = (self._fmt % record.__dict__).replace('\n', '#012') + if record.exc_info: + # Cache the traceback text to avoid converting it multiple times + # (it's constant anyway) + if not record.exc_text: + record.exc_text = self.formatException( + record.exc_info).replace('\n', '#012') + if record.exc_text: + if msg[-3:] != '#012': + msg = msg + '#012' + msg = msg + record.exc_text + if (hasattr(record, 'txn_id') and record.txn_id and record.levelno != logging.INFO and record.txn_id not in msg): diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index 42e8c0ed44..2f291098c1 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -585,6 +585,10 @@ class TestUtils(unittest.TestCase): self.assertEquals(logger.txn_id, '12345') logger.warn('test 12345 test') self.assertEquals(strip_value(sio), 'test 12345 test\n') + # Test multi line collapsing + logger.error('my\nerror\nmessage') + log_msg = strip_value(sio) + self.assert_('my#012error#012message' in log_msg) # test client_ip self.assertFalse(logger.client_ip)