summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorVinay Sajip <vinay_sajip@yahoo.co.uk>2010-10-25 13:57:39 (GMT)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>2010-10-25 13:57:39 (GMT)
commita39c571061658967918276224a4992d1b421ae3f (patch)
treec567b97c3f4d3128dc1c4a00cfb0807513d00e42 /Lib/test
parent7e9065cf8c9d2465af002bfc13687d72e9a9dcdd (diff)
downloadcpython-a39c571061658967918276224a4992d1b421ae3f.zip
cpython-a39c571061658967918276224a4992d1b421ae3f.tar.gz
cpython-a39c571061658967918276224a4992d1b421ae3f.tar.bz2
logging: Added style option to Formatter to allow %, {} or himBHformatting.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_logging.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index a738d7a..9aa6af3 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -1863,6 +1863,53 @@ class QueueHandlerTest(BaseTest):
self.assertEqual(data.name, self.que_logger.name)
self.assertEqual((data.msg, data.args), (msg, None))
+class FormatterTest(unittest.TestCase):
+ def setUp(self):
+ self.common = {
+ 'name': 'formatter.test',
+ 'level': logging.DEBUG,
+ 'pathname': os.path.join('path', 'to', 'dummy.ext'),
+ 'lineno': 42,
+ 'exc_info': None,
+ 'func': None,
+ 'msg': 'Message with %d %s',
+ 'args': (2, 'placeholders'),
+ }
+ self.variants = {
+ }
+
+ def get_record(self, name=None):
+ result = dict(self.common)
+ if name is not None:
+ result.update(self.variants[name])
+ return logging.makeLogRecord(result)
+
+ def test_percent(self):
+ "Test %-formatting"
+ r = self.get_record()
+ f = logging.Formatter('${%(message)s}')
+ self.assertEqual(f.format(r), '${Message with 2 placeholders}')
+ f = logging.Formatter('%(random)s')
+ self.assertRaises(KeyError, f.format, r)
+
+ def test_braces(self):
+ "Test {}-formatting"
+ r = self.get_record()
+ f = logging.Formatter('$%{message}%$', style='{')
+ self.assertEqual(f.format(r), '$%Message with 2 placeholders%$')
+ f = logging.Formatter('{random}', style='{')
+ self.assertRaises(KeyError, f.format, r)
+
+ def test_dollars(self):
+ "Test $-formatting"
+ r = self.get_record()
+ f = logging.Formatter('$message', style='$')
+ self.assertEqual(f.format(r), 'Message with 2 placeholders')
+ f = logging.Formatter('$$%${message}%$$', style='$')
+ self.assertEqual(f.format(r), '$%Message with 2 placeholders%$')
+ f = logging.Formatter('${random}', style='$')
+ self.assertRaises(KeyError, f.format, r)
+
class BaseFileTest(BaseTest):
"Base class for handler tests that write log files"
@@ -1945,6 +1992,7 @@ def test_main():
CustomLevelsAndFiltersTest, MemoryHandlerTest,
ConfigFileTest, SocketHandlerTest, MemoryTest,
EncodingTest, WarningsTest, ConfigDictTest, ManagerTest,
+ FormatterTest,
LogRecordClassTest, ChildLoggerTest, QueueHandlerTest,
RotatingFileHandlerTest,
#TimedRotatingFileHandlerTest