def setup_stream_handler(verbose: bool) -> logging.Handler: stream_handler = logging.StreamHandler() if verbose: pipx_str = bold(green("pipx >")) if sys.stdout.isatty() else "pipx >" stream_handler.setLevel(logging.INFO) stream_handler.setFormatter( logging.Formatter(pipx_str + "({funcName}:{lineno}): {message}", style="{")) else: stream_handler.setLevel(logging.WARNING) stream_handler.setFormatter(logging.Formatter("{message}", style="{")) return stream_handler
def setup_logging(verbose: bool) -> None: pipx_str = bold(green("pipx >")) if sys.stdout.isatty() else "pipx >" pipx.constants.pipx_log_file = setup_log_file() # "incremental" is False so previous pytest tests don't accumulate handlers logging_config = { "version": 1, "formatters": { "stream_nonverbose": { "class": "logging.Formatter", "format": "{message}", "style": "{", }, "stream_verbose": { "class": "logging.Formatter", "format": pipx_str + "({funcName}:{lineno}): {message}", "style": "{", }, "file": { "class": "logging.Formatter", "format": "{relativeCreated: >8.1f}ms ({funcName}:{lineno}): {message}", "style": "{", }, }, "handlers": { "stream": { "class": "logging.StreamHandler", "formatter": "stream_verbose" if verbose else "stream_nonverbose", "level": "INFO" if verbose else "WARNING", }, "file": { "class": "logging.FileHandler", "formatter": "file", "filename": str(pipx.constants.pipx_log_file), "encoding": "utf-8", "level": "DEBUG", }, }, "loggers": { "pipx": { "handlers": ["stream", "file"], "level": "DEBUG" } }, "incremental": False, } logging.config.dictConfig(logging_config)