Skip to content
Snippets Groups Projects
Commit 59127ee1 authored by Ashwin Kumar Karnad's avatar Ashwin Kumar Karnad
Browse files

Merge branch 'use-rich-if-available' into 'main'

use rich for logging formatting (if package available)

See merge request mpsd-cs/mpsd-software-environments!23
parents 0f0c8dd3 517d0463
No related branches found
No related tags found
1 merge request!23use rich for logging formatting (if package available)
Pipeline #368958 passed
...@@ -29,7 +29,7 @@ stages: ...@@ -29,7 +29,7 @@ stages:
- echo "Install Python3" - echo "Install Python3"
- apt-get update - apt-get update
- apt-get install -y python3 python3-venv - apt-get install -y python3 python3-venv python3-rich
- python3 -m venv --help - python3 -m venv --help
- python3 -m venv venv - python3 -m venv venv
- source venv/bin/activate - source venv/bin/activate
......
...@@ -12,6 +12,15 @@ import time ...@@ -12,6 +12,15 @@ import time
from pathlib import Path from pathlib import Path
from typing import List, Tuple from typing import List, Tuple
# 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
about_tool = """ about_tool = """
Build toolchains using Spack.\n Build toolchains using Spack.\n
...@@ -61,16 +70,28 @@ def set_up_logging(loglevel="warning", filename=None): ...@@ -61,16 +70,28 @@ def set_up_logging(loglevel="warning", filename=None):
if filename: if filename:
handlers.append(logging.FileHandler(filename)) handlers.append(logging.FileHandler(filename))
handlers.append(logging.StreamHandler()) if rich_available:
linenumbers = " %(lineno)4d" if log_level_numeric == logging.DEBUG else "" # set up logging as recommended for rich, see
# https://rich.readthedocs.io/en/stable/logging.html
handlers.append(rich.logging.RichHandler())
logging_format = "%(message)s"
else: # rich not available, define our own output
# include line numbers in output if level is DEBUG
linenumbers = " %(lineno)4d" if log_level_numeric == logging.DEBUG else ""
handlers.append(logging.StreamHandler())
logging_format = "%(asctime)s %(levelname)7s" + linenumbers + " | %(message)s"
logging.basicConfig( logging.basicConfig(
format="%(asctime)s %(levelname)7s" + linenumbers + " | %(message)s",
datefmt="[%X]",
level=log_level_numeric, level=log_level_numeric,
format=logging_format,
datefmt="[%X]",
handlers=handlers, handlers=handlers,
force=True, force=True,
) )
logging.debug(f"Logging has been setup, loglevel={loglevel.upper()} {filename=}") logging.debug(
f"Logging has been setup, loglevel={loglevel.upper()}"
+ f"{filename=} {rich_available=}"
)
# Helper class to change directory via context manager # Helper class to change directory via context manager
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment