From 4333b965de5b3d95150cd9a21cbd84d94abb09b7 Mon Sep 17 00:00:00 2001 From: Jonathan Liuti Date: Wed, 12 Jul 2017 15:14:02 +0200 Subject: [PATCH] Fix #7 Return formatted message if format string Currently logging with: ``` logger.info('hello %s', 'world') ``` will send the string "hello %s" to logz.io instead of "hello world" This commit fixes this situation. --- logzio/handler.py | 2 +- tests/test_logzioHandler.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/logzio/handler.py b/logzio/handler.py index 1c19562..5df4b12 100644 --- a/logzio/handler.py +++ b/logzio/handler.py @@ -39,7 +39,7 @@ class LogzioHandler(logging.Handler): "path_name": message.pathname, "log_level": message.levelname, "type": self.logzio_type, - "message": message.msg, + "message": message.getMessage(), "@timestamp": timestamp } diff --git a/tests/test_logzioHandler.py b/tests/test_logzioHandler.py index 89e67b2..c4e0194 100644 --- a/tests/test_logzioHandler.py +++ b/tests/test_logzioHandler.py @@ -77,6 +77,34 @@ class TestLogzioHandler(TestCase): } ) + def test_format_string_message(self): + record = logging.LogRecord( + name='my-logger', + level=0, + pathname='handler_test.py', + lineno=10, + msg="this is a test: %s.", + args=('moo',), + exc_info=None, + func='test_json' + ) + + formatted_message = self.handler.format_message(record) + formatted_message["@timestamp"] = None + + self.assertDictEqual( + formatted_message, + { + '@timestamp': None, + 'line_number': 10, + 'log_level': 'NOTSET', + 'logger': 'my-logger', + 'message': 'this is a test: moo.', + 'path_name': 'handler_test.py', + 'type': 'python' + } + ) + def test_exc(self): try: raise ValueError("oops.")