Skip to content
Snippets Groups Projects

Remove conditional dependence on rich

Merged Hans Fangohr requested to merge depend-on-rich-from-here-on into main
1 file
+ 10
25
Compare changes
  • Side-by-side
  • Inline
@@ -20,14 +20,7 @@ import importlib.metadata
__version__ = importlib.metadata.version(__package__ or __name__)
# If 'rich' is available ("pip install rich" or "apt-get install python3-rich"),
# then use coloured output, otherwise proceed as before
try:
import rich.logging
except ModuleNotFoundError:
rich_available = False
else:
rich_available = True
import rich.logging
command_name = Path(sys.argv[0]).name
@@ -246,8 +239,7 @@ def set_up_logging(loglevel="warning", file_path=None):
1. log = logging.getLogger('')
This is the 'root' logger. It uses a RichHandler if rich is available for
output to the shell, otherwise plain text.
This is the 'root' logger.
Typical use:
@@ -294,19 +286,13 @@ def set_up_logging(loglevel="warning", file_path=None):
logger.setLevel(0)
# the handler determines where the logs go: stdout/file
if rich_available:
# https://rich.readthedocs.io/en/stable/logging.html
shell_handler = rich.logging.RichHandler()
# rich handler provides metadata automatically:
logging_format = "%(message)s"
# for shell output, only show time (not date and time)
shell_formatter = logging.Formatter(logging_format, datefmt="[%X]")
else:
shell_handler = logging.StreamHandler()
# include line numbers in output if level is DEBUG
linenumbers = " %(lineno)4d" if log_level_numeric == logging.DEBUG else ""
logging_format = "%(asctime)s %(levelname)7s" + linenumbers + " | %(message)s"
shell_formatter = logging.Formatter(logging_format)
# We use 'rich' to provide a Handler:
# https://rich.readthedocs.io/en/stable/logging.html
shell_handler = rich.logging.RichHandler()
# rich handler provides metadata automatically:
logging_format = "%(message)s"
# for shell output, only show time (not date and time)
shell_formatter = logging.Formatter(logging_format, datefmt="[%X]")
# here we hook everything together
shell_handler.setFormatter(shell_formatter)
@@ -355,8 +341,7 @@ def set_up_logging(loglevel="warning", file_path=None):
# short message
#
logging.debug(
f"Logging has been setup, loglevel={loglevel.upper()} "
+ f"{file_path=} {rich_available=}"
f"Logging has been setup, loglevel={loglevel.upper()} " + f"{file_path=}"
)
Loading