Skip to content

Commit

Permalink
logger: allow exc_info in all record types (#3290)
Browse files Browse the repository at this point in the history
This better reflects the original logger formatter.

This also fixes annoying redundant newline between traceback and "need
any help" footer.
  • Loading branch information
efiop committed Feb 9, 2020
1 parent afcbc7e commit 2786a4e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
29 changes: 10 additions & 19 deletions dvc/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,28 @@ class ColorFormatter(logging.Formatter):

color_code = {
"DEBUG": colorama.Fore.BLUE,
"INFO": "",
"WARNING": colorama.Fore.YELLOW,
"ERROR": colorama.Fore.RED,
"CRITICAL": colorama.Fore.RED,
}

def format(self, record):
msg = record.msg.format(*record.args) if record.args else record.msg
exception, stack_trace = self._parse_exc(record)
return ("{prefix}{description}{stack_trace}").format(
prefix=self._prefix(record),
description=self._description(msg, exception),
stack_trace=stack_trace,
)

def _prefix(self, record):
if record.levelname == "INFO":
return msg

if record.levelname == "ERROR" or record.levelname == "CRITICAL":
exception, stack_trace = self._parse_exc(record)

return (
"{color}{levelname}{nc}: {description}" "{stack_trace}\n"
).format(
color=self.color_code.get(record.levelname, ""),
nc=colorama.Fore.RESET,
levelname=record.levelname,
description=self._description(msg, exception),
msg=msg,
stack_trace=stack_trace,
)
return ""

return "{color}{levelname}{nc}: {msg}".format(
return "{color}{levelname}{nc}: ".format(
color=self.color_code.get(record.levelname, ""),
nc=colorama.Fore.RESET,
levelname=record.levelname,
msg=msg,
nc=colorama.Fore.RESET,
)

def _current_level(self):
Expand Down
16 changes: 7 additions & 9 deletions tests/unit/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_error(self, caplog):
with caplog.at_level(logging.INFO, logger="dvc"):
logger.error("message")

expected = "{red}ERROR{nc}: message\n".format(**colors)
expected = "{red}ERROR{nc}: message".format(**colors)

assert expected == formatter.format(caplog.records[0])

Expand All @@ -55,7 +55,7 @@ def test_exception(self, caplog):
except Exception:
logger.exception("message")

expected = "{red}ERROR{nc}: message\n".format(**colors)
expected = "{red}ERROR{nc}: message".format(**colors)

assert expected == formatter.format(caplog.records[0])

Expand All @@ -66,7 +66,7 @@ def test_exception_with_description_and_without_message(self, caplog):
except Exception:
logger.exception("")

expected = "{red}ERROR{nc}: description\n".format(**colors)
expected = "{red}ERROR{nc}: description".format(**colors)

assert expected == formatter.format(caplog.records[0])

Expand All @@ -77,9 +77,7 @@ def test_exception_with_description_and_message(self, caplog):
except Exception:
logger.exception("message")

expected = "{red}ERROR{nc}: message - description\n".format(
**colors
)
expected = "{red}ERROR{nc}: message - description".format(**colors)

assert expected == formatter.format(caplog.records[0])

Expand All @@ -95,7 +93,7 @@ def test_exception_under_verbose(self, caplog):
"{red}ERROR{nc}: description\n"
"{red}{line}{nc}\n"
"{stack_trace}"
"{red}{line}{nc}\n".format(
"{red}{line}{nc}".format(
line="-" * 60, stack_trace=stack_trace, **colors
)
)
Expand All @@ -114,7 +112,7 @@ def test_tb_only(self, caplog):
"{red}ERROR{nc}: something\n"
"{red}{line}{nc}\n"
"{stack_trace}"
"{red}{line}{nc}\n".format(
"{red}{line}{nc}".format(
line="-" * 60, stack_trace=stack_trace, **colors
)
)
Expand All @@ -136,7 +134,7 @@ def test_nested_exceptions(self, caplog):
"{red}ERROR{nc}: message - second: first\n"
"{red}{line}{nc}\n"
"{stack_trace}"
"{red}{line}{nc}\n".format(
"{red}{line}{nc}".format(
line="-" * 60, stack_trace=stack_trace, **colors
)
)
Expand Down

0 comments on commit 2786a4e

Please sign in to comment.