mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-10 07:18:27 +01:00
logger: allow exc_info in all record types (#3290)
This better reflects the original logger formatter. This also fixes annoying redundant newline between traceback and "need any help" footer.
This commit is contained in:
+10
-19
@@ -51,7 +51,6 @@ class ColorFormatter(logging.Formatter):
|
||||
|
||||
color_code = {
|
||||
"DEBUG": colorama.Fore.BLUE,
|
||||
"INFO": "",
|
||||
"WARNING": colorama.Fore.YELLOW,
|
||||
"ERROR": colorama.Fore.RED,
|
||||
"CRITICAL": colorama.Fore.RED,
|
||||
@@ -59,29 +58,21 @@ class ColorFormatter(logging.Formatter):
|
||||
|
||||
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
|
||||
return ""
|
||||
|
||||
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 "{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):
|
||||
|
||||
@@ -44,7 +44,7 @@ class TestColorFormatter:
|
||||
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])
|
||||
|
||||
@@ -55,7 +55,7 @@ class TestColorFormatter:
|
||||
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])
|
||||
|
||||
@@ -66,7 +66,7 @@ class TestColorFormatter:
|
||||
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])
|
||||
|
||||
@@ -77,9 +77,7 @@ class TestColorFormatter:
|
||||
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])
|
||||
|
||||
@@ -95,7 +93,7 @@ class TestColorFormatter:
|
||||
"{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
|
||||
)
|
||||
)
|
||||
@@ -114,7 +112,7 @@ class TestColorFormatter:
|
||||
"{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
|
||||
)
|
||||
)
|
||||
@@ -136,7 +134,7 @@ class TestColorFormatter:
|
||||
"{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
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user