From a91993616fa7b8510c1dbe6e5a24f182f16f3adf Mon Sep 17 00:00:00 2001 From: skshetry <18718008+skshetry@users.noreply.github.com> Date: Thu, 6 Apr 2023 09:56:19 +0545 Subject: [PATCH] logger: add DVC_SHOW_TRACEBACK envvar to show traceback on exception (#9310) This will come useful in debugging, where you don't have to add --verbose just to see where it failed or go through verbose logs (which might be useful when working with UI issues as you want to see what the user sees). Ideally this should be handled in main when we exit with error instead of using logger.{warning,error,exception} to log exception as the intention is to only log last exception that makes it exit. But since we don't have that capability and we don't abuse logging.{warning,error,exception} to log and suppress exceptions, this is a good enough solution for now without complicating `dvc.cli.main()`. --- dvc/env.py | 1 + dvc/logger.py | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/dvc/env.py b/dvc/env.py index dac52bf4e..bc28b432c 100644 --- a/dvc/env.py +++ b/dvc/env.py @@ -9,3 +9,4 @@ DVC_EXP_NAME = "DVC_EXP_NAME" DVC_EXP_GIT_REMOTE = "DVC_EXP_GIT_REMOTE" DVC_EXP_AUTO_PUSH = "DVC_EXP_AUTO_PUSH" DVC_NO_ANALYTICS = "DVC_NO_ANALYTICS" +DVC_SHOW_TRACEBACK = "DVC_SHOW_TRACEBACK" diff --git a/dvc/logger.py b/dvc/logger.py index 1776d5472..6c173e3a0 100644 --- a/dvc/logger.py +++ b/dvc/logger.py @@ -3,10 +3,12 @@ import logging import logging.config import logging.handlers +import os import sys import colorama +from dvc.env import DVC_SHOW_TRACEBACK from dvc.progress import Tqdm @@ -86,9 +88,10 @@ class ColorFormatter(logging.Formatter): "CRITICAL": colorama.Fore.RED, } - def __init__(self, log_colors: bool = True) -> None: + def __init__(self, log_colors: bool = True, show_traceback: bool = False) -> None: super().__init__() self.log_colors = log_colors + self.show_traceback = show_traceback def format(self, record) -> str: # noqa: A003, C901 record.message = record.getMessage() @@ -106,8 +109,10 @@ class ColorFormatter(logging.Formatter): msg = msg + sep + cause asctime = "" - if _is_verbose(): + verbose = _is_verbose() + if verbose: asctime = self.formatTime(record, self.datefmt) + if verbose or self.show_traceback: if ei and not record.exc_text: record.exc_text = self.formatException(ei) if record.exc_text: @@ -198,7 +203,10 @@ def setup(level: int = logging.INFO, log_colors: bool = True) -> None: console_trace.setFormatter(formatter) console_trace.addFilter(exclude_filter(logging.DEBUG)) - err_formatter = ColorFormatter(log_colors=log_colors and sys.stderr.isatty()) + show_traceback = bool(os.environ.get(DVC_SHOW_TRACEBACK)) + err_formatter = ColorFormatter( + log_colors=log_colors and sys.stderr.isatty(), show_traceback=show_traceback + ) console_errors = LoggerHandler(sys.stderr) console_errors.setLevel(logging.WARNING) console_errors.setFormatter(err_formatter)