From 86220ba028371954b4fb2f4dfc1207c69399278f Mon Sep 17 00:00:00 2001 From: gholt Date: Tue, 26 Feb 2013 18:38:09 +0000 Subject: [PATCH] Force log entries to be one line Different versions of syslog-ng and probably other syslog services handle multi line log messages differently and sometimes quite poorly. This patch collapses multi line log messages into single lines before sending them on to syslog. It's just a copy of what was already in Python's logging.Formatter but altered to replace the newlines with #012. I used #012 since that's a convention we've already used elsewhere in Swift. Change-Id: I8d0509b7cf48e45c2cf6480b51c67eec5bc94fe2 --- swift/common/utils.py | 19 ++++++++++++++++++- test/unit/common/test_utils.py | 4 ++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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)