Skip to content

Commit

Permalink
🎨 Better layout 2
Browse files Browse the repository at this point in the history
  • Loading branch information
je-cook committed Sep 27, 2024
1 parent 219a623 commit 40e40d9
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 97 deletions.
140 changes: 48 additions & 92 deletions bluemira/base/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions bluemira/base/look_and_feel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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")


# =============================================================================
Expand Down
37 changes: 37 additions & 0 deletions tests/base/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
logger_setup,
set_log_level,
)
from bluemira.base.look_and_feel import LOGGER


class TestLoggingLevel:
Expand Down Expand Up @@ -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)

0 comments on commit 40e40d9

Please sign in to comment.