Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor enhancements for log output #317

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"DEBUG": BLUE,
"CRITICAL": YELLOW,
"ERROR": RED,
"TRACE": MAGENTA,
}


Expand All @@ -51,6 +52,15 @@ def format(self, record):
return logging.Formatter.format(self, record)


class PlainFormatter(logging.Formatter):
def format(self, record):
if record.levelno == logging.ERROR:
self._style._fmt = "%(levelname)s: %(message)s"
else:
self._style._fmt = "%(message)s"
return logging.Formatter.format(self, record)


# Setup console handler - provided as separate function to be called
# prior to argument parsing
def setup_console_handler():
Expand All @@ -61,9 +71,9 @@ def setup_console_handler():
color = True

if color:
formatter = ColoredFormatter("%(levelname)s %(message)s")
formatter = ColoredFormatter("%(levelname)16s %(message)s")
else:
formatter = logging.Formatter("%(message)s")
formatter = PlainFormatter()

console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(formatter)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def wrap_function(*args, **kwargs):

def console_error(*argv, exit=True):
if len(argv) > 1:
logging.error("ERROR: " + f"[{argv[0]}] {argv[1]}")
logging.error(f"[{argv[0]}] {argv[1]}")
else:
logging.error("ERROR: " + f"{argv[0]}")
logging.error(f"{argv[0]}")
if exit:
sys.exit(1)

Expand Down