From 40e40d90d81288e6cf771f2bb6eb18dee190efc0 Mon Sep 17 00:00:00 2001 From: james <81617086+je-cook@users.noreply.github.com> Date: Fri, 27 Sep 2024 11:31:40 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Better=20layout=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bluemira/base/logs.py | 140 +++++++++++---------------------- bluemira/base/look_and_feel.py | 10 +-- tests/base/test_logs.py | 37 +++++++++ 3 files changed, 90 insertions(+), 97 deletions(-) diff --git a/bluemira/base/logs.py b/bluemira/base/logs.py index 8061b78f5d..78b8744cfb 100644 --- a/bluemira/base/logs.py +++ b/bluemira/base/logs.py @@ -74,122 +74,76 @@ def format(self, record) -> str: class LoggerAdapter(logging.Logger): """Adapt the base logging class for our uses""" - def debug( - self, - msg, - *args, - flush: bool = False, - fmt: bool = True, - **kwargs, + def _base( + self, func, msg, colour, *args, flush: bool = False, fmt: bool = True, **kwargs ): + return self._terminator_handler( + func, + colourise(msg, colour=colour, flush=flush, fmt=fmt), + *args, + fhterm=logging.StreamHandler.terminator if flush else "", + shterm="" if flush or kwargs.pop("clean", False) else "\n", + **kwargs, + ) + + def debug(self, msg, *args, flush: bool = False, fmt: bool = True, **kwargs): """Debug""" - return super().debug( - colourise(msg, colour="green", flush=flush, fmt=fmt), *args, **kwargs + return self._base( + super().debug, msg, "green", *args, flush=flush, fmt=fmt, **kwargs ) - def info( - self, - msg, - *args, - flush: bool = False, - fmt: bool = True, - **kwargs, - ): + def info(self, msg, *args, flush: bool = False, fmt: bool = True, **kwargs): """Info""" - return super().info( - colourise(msg, colour="blue", flush=flush, fmt=fmt), *args, **kwargs + return self._base( + super().info, msg, "blue", *args, flush=flush, fmt=fmt, **kwargs ) - def warning( - self, - msg, - *args, - flush: bool = False, - fmt: bool = True, - **kwargs, - ): + def warning(self, msg, *args, flush: bool = False, fmt: bool = True, **kwargs): """Warning""" - return super().warning( - colourise(f"WARNING: {msg}", colour="orange", flush=flush, fmt=fmt), - *args, - **kwargs, + msg = f"WARNING: {msg}" + return self._base( + super().warning, msg, "orange", *args, flush=flush, fmt=fmt, **kwargs ) - def error( - self, - msg, - *args, - flush: bool = False, - fmt: bool = True, - **kwargs, - ): + def error(self, msg, *args, flush: bool = False, fmt: bool = True, **kwargs): """Error""" - return super().error( - colourise(f"ERROR: {msg}", colour="red", flush=flush, fmt=fmt), - *args, - **kwargs, + msg = f"ERROR: {msg}" + return self._base( + super().warning, msg, "red", *args, flush=flush, fmt=fmt, **kwargs ) - def critical( - self, - msg, - *args, - flush: bool = False, - fmt: bool = True, - **kwargs, - ): + def critical(self, msg, *args, flush: bool = False, fmt: bool = True, **kwargs): """Critical""" - return super().critical( - colourise(f"CRITICAL: {msg}", colour="darkred", flush=flush, fmt=fmt), - *args, - **kwargs, + msg = f"CRITICAL: {msg}" + return self._base( + super().warning, msg, "darkred", *args, flush=flush, fmt=fmt, **kwargs ) - def info_clean(self, msg, *args, **kwargs): - """Info no modification""" - return self._terminator_handler(super().info, msg, *args, **kwargs) - - def error_clean(self, msg, *args, **kwargs): - """Error colour modification only""" - return self._terminator_handler( - super().error, _print_colour(msg, "red"), *args, **kwargs - ) - - def clean_flush(self, msg, *args, **kwargs): + def clean( + self, msg, *args, colour: str | None = None, flush: bool = False, **kwargs + ): """Unmodified flush""" - return self._terminator_handler( + if colour is not None: + msg = _print_colour(msg, colour) + return self._base( super().info, - colourise(msg, colour=None, flush=True, fmt=False), - *args, - fhterm=logging.StreamHandler.terminator, - **kwargs, - ) - - def info_flush(self, msg, *args, **kwargs): - """Info coloured flush""" - return self._terminator_handler( - self.info, msg, + colour, *args, - fhterm=logging.StreamHandler.terminator, - flush=True, - **kwargs, - ) - - def debug_flush(self, msg, *args, **kwargs): - """Debug coloured flush""" - return self._terminator_handler( - self.debug, - msg, - *args, - fhterm=logging.StreamHandler.terminator, - flush=True, + flush=flush, + clean=True, + fmt=False, **kwargs, ) @staticmethod def _terminator_handler( - func: Callable[[str], None], string: str, *args, fhterm: str = "", **kwargs + func: Callable[[str], None], + string: str, + *args, + fhterm: str = "", + shterm: str = "", + **kwargs, ): """ Log string allowing modification to handler terminator @@ -202,9 +156,11 @@ def _terminator_handler( The string to colour flush print fhterm: FileHandler Terminator + shterm: + StreamHandler Terminator """ original_terminator = logging.StreamHandler.terminator - logging.StreamHandler.terminator = "" + logging.StreamHandler.terminator = shterm logging.FileHandler.terminator = fhterm try: func(string, *args, **kwargs) diff --git a/bluemira/base/look_and_feel.py b/bluemira/base/look_and_feel.py index 5ae0ac5c93..c2d2421ea1 100644 --- a/bluemira/base/look_and_feel.py +++ b/bluemira/base/look_and_feel.py @@ -227,7 +227,7 @@ def _bluemira_clean_flush( string: The string to colour flush print """ - LOGGER.clean_flush(string) + LOGGER.clean(string, flush=True) def bluemira_print_flush(string: str): @@ -240,7 +240,7 @@ def bluemira_print_flush(string: str): string: The string to colour flush print """ - LOGGER.info_flush(string) + LOGGER.info(string, flush=True) def bluemira_debug_flush(string: str): @@ -253,7 +253,7 @@ def bluemira_debug_flush(string: str): string: The string to colour flush print for debug messages. """ - LOGGER.debug_flush(string) + LOGGER.debug(string, flush=True) def bluemira_print_clean(string: str): @@ -266,7 +266,7 @@ def bluemira_print_clean(string: str): string: The string to print """ - LOGGER.info_clean(string) + LOGGER.clean(string) def bluemira_error_clean(string: str): @@ -279,7 +279,7 @@ def bluemira_error_clean(string: str): string: The string to colour print """ - LOGGER.error_clean(string) + LOGGER.clean(string, colour="red") # ============================================================================= diff --git a/tests/base/test_logs.py b/tests/base/test_logs.py index 9cc72918e3..087bf7f23c 100644 --- a/tests/base/test_logs.py +++ b/tests/base/test_logs.py @@ -18,6 +18,7 @@ logger_setup, set_log_level, ) +from bluemira.base.look_and_feel import LOGGER class TestLoggingLevel: @@ -100,3 +101,39 @@ def test_logging_context(self): with LoggingContext(original_log_level + 1): assert original_log_level != get_log_level(as_str=False) assert get_log_level(as_str=False) == original_log_level + + +class TestLoggerClass: + @pytest.mark.parametrize( + "logfunc", + [LOGGER.debug, LOGGER.info, LOGGER.warning, LOGGER.error, LOGGER.critical], + ) + @pytest.mark.parametrize("flush", [False, True]) + @pytest.mark.parametrize("fmt", [True, False]) + def test_basics(self, logfunc, flush, fmt, caplog): + logfunc("string1", flush=flush, fmt=fmt) + logfunc("string2", flush=flush, fmt=fmt) + + if flush: + assert all(c.startswith("\r") for c in caplog.messages) + else: + assert all(not c.startswith("\r") for c in caplog.messages) + + if fmt: + if flush: + assert all(len(c.split("|")) == 3 for c in caplog.messages) + else: + assert all(c.split("\n")[1].endswith("|") for c in caplog.messages) + else: + assert any("|" not in c for c in caplog.messages) + + @pytest.mark.parametrize("flush", [False, True]) + @pytest.mark.parametrize("colour", [None, "red"]) + def test_clean(self, flush, colour, caplog): + LOGGER.clean("string1", flush=flush, colour=colour) + LOGGER.clean("string2", flush=flush, colour=colour) + if flush: + assert all(c.startswith("\r") for c in caplog.messages) + else: + assert all(not c.startswith("\r") for c in caplog.messages) + assert any("|" not in c for c in caplog.messages)