From a352e162d80505afacccef717889ce31c58ebaa2 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 28 Feb 2024 14:14:53 +0900 Subject: [PATCH] fixup --- tests/test_asynchandler.py | 41 +++++++++++++++++---------------- tests/test_handler.py | 47 +++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/tests/test_asynchandler.py b/tests/test_asynchandler.py index 8cd2681..bbbf52e 100644 --- a/tests/test_asynchandler.py +++ b/tests/test_asynchandler.py @@ -20,6 +20,12 @@ from tests import mockserver +def get_logger(name, level=logging.INFO): + logger = logging.getLogger(name) + logger.setLevel(level) + return logger + + class TestHandler(unittest.TestCase): def setUp(self): super(TestHandler, self).setUp() @@ -36,16 +42,11 @@ def get_handler_class(self): def get_data(self): return self._server.get_received() - def get_logger(self, name, level=logging.INFO): - logger = logging.getLogger(name) - logger.setLevel(level) - return logger - def test_simple(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info({ @@ -67,7 +68,7 @@ def test_custom_fmt(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '%(name)s', @@ -89,7 +90,7 @@ def test_custom_fmt_with_format_style(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '{name}', @@ -111,7 +112,7 @@ def test_custom_fmt_with_template_style(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '${name}', @@ -132,7 +133,7 @@ def test_custom_field_raise_exception(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '%(name)s', @@ -147,7 +148,7 @@ def test_custom_field_raise_exception(self): def test_custom_field_fill_missing_fmt_key_is_true(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '%(name)s', @@ -171,7 +172,7 @@ def test_json_encoded_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info('{"key": "hello world!", "param": "value"}') @@ -184,7 +185,7 @@ def test_unstructured_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info('hello %s', 'world') @@ -197,7 +198,7 @@ def test_unstructured_formatted_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info('hello world, %s', 'you!') @@ -210,7 +211,7 @@ def test_number_string_simple_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info("1") @@ -222,7 +223,7 @@ def test_non_string_simple_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info(42) @@ -234,7 +235,7 @@ def test_non_string_dict_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info({42: 'root'}) @@ -247,7 +248,7 @@ def test_exception_message(self): handler = self.get_handler_class()('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) try: @@ -289,7 +290,7 @@ def test_simple(self): self.assertEqual(handler.sender.queue_circular, True) self.assertEqual(handler.sender.queue_maxsize, self.Q_SIZE) - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info({'cnt': 1, 'from': 'userA', 'to': 'userB'}) @@ -350,7 +351,7 @@ def custom_full_queue(): self.assertEqual(handler.sender.queue_circular, True) self.assertEqual(handler.sender.queue_maxsize, self.Q_SIZE) - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) diff --git a/tests/test_handler.py b/tests/test_handler.py index 7c40c07..2ef0695 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -8,6 +8,12 @@ from tests import mockserver +def get_logger(name, level=logging.INFO): + logger = logging.getLogger(name) + logger.setLevel(level) + return logger + + class TestHandler(unittest.TestCase): def setUp(self): super(TestHandler, self).setUp() @@ -20,16 +26,11 @@ def tearDown(self): def get_data(self): return self._server.get_received() - def get_logger(self, name, level=logging.INFO): - logger = logging.getLogger(name) - logger.setLevel(level) - return logger - def test_simple(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) @@ -54,7 +55,7 @@ def test_custom_fmt(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '%(name)s', @@ -76,7 +77,7 @@ def test_exclude_attrs(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(exclude_attrs=[]) ) @@ -93,7 +94,7 @@ def test_exclude_attrs_with_exclusion(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(exclude_attrs=["funcName"]) ) @@ -110,7 +111,7 @@ def test_exclude_attrs_with_extra(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(exclude_attrs=[]) ) @@ -138,7 +139,7 @@ def formatter(record): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt=formatter) ) @@ -156,7 +157,7 @@ def test_custom_fmt_with_format_style(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '{name}', @@ -179,7 +180,7 @@ def test_custom_fmt_with_template_style(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '${name}', @@ -201,7 +202,7 @@ def test_custom_field_raise_exception(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '%(name)s', @@ -219,7 +220,7 @@ def test_custom_field_fill_missing_fmt_key_is_true(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter( fluent.handler.FluentRecordFormatter(fmt={ 'name': '%(name)s', @@ -243,7 +244,7 @@ def test_json_encoded_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) @@ -259,7 +260,7 @@ def test_json_encoded_message_without_json(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter(format_json=False)) log.addHandler(handler) @@ -275,7 +276,7 @@ def test_unstructured_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info('hello %s', 'world') @@ -289,7 +290,7 @@ def test_unstructured_formatted_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info('hello world, %s', 'you!') @@ -303,7 +304,7 @@ def test_number_string_simple_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info("1") @@ -316,7 +317,7 @@ def test_non_string_simple_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info(42) @@ -329,7 +330,7 @@ def test_non_string_dict_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) log.info({42: 'root'}) @@ -343,7 +344,7 @@ def test_exception_message(self): handler = fluent.handler.FluentHandler('app.follow', port=self._port) with handler: - log = self.get_logger('fluent.test') + log = get_logger('fluent.test') handler.setFormatter(fluent.handler.FluentRecordFormatter()) log.addHandler(handler) try: