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

logger: allow exc_info in all record types #3290

Merged
merged 1 commit into from
Feb 9, 2020
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
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": "",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was never used.

"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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docstring said that exc_info is accounted for, but it wasn't unless record was at ERROR or CRITICAL levels.

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