Skip to content

Commit

Permalink
fix(cli): bentoml cli verbosity not passed to the subprocess correctly (
Browse files Browse the repository at this point in the history
#4661)

* fix(cli): bentoml cli verbosity not passed to the subprocess correctly

Signed-off-by: Frost Ming <[email protected]>

* fix: default value

Signed-off-by: Frost Ming <[email protected]>

* fix: don't break old usage

Signed-off-by: Frost Ming <[email protected]>

* more compatibility

Signed-off-by: Frost Ming <[email protected]>

---------

Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming authored Apr 19, 2024
1 parent 606c975 commit bd47504
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
33 changes: 20 additions & 13 deletions src/bentoml/_internal/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

DEBUG_ENV_VAR = "BENTOML_DEBUG"
QUIET_ENV_VAR = "BENTOML_QUIET"
VERBOSITY_ENV_VAR = "BENTOML_VERBOSITY"
CONFIG_ENV_VAR = "BENTOML_CONFIG"
CONFIG_OVERRIDE_ENV_VAR = "BENTOML_CONFIG_OPTIONS"
CONFIG_OVERRIDE_JSON_ENV_VAR = "BENTOML_CONFIG_OVERRIDES"
Expand Down Expand Up @@ -129,29 +130,35 @@ def get_bentoml_override_config_json_from_env() -> dict[str, t.Any] | None:
return None


def set_debug_mode(enabled: bool) -> None:
os.environ[DEBUG_ENV_VAR] = str(enabled)
os.environ[GRPC_DEBUG_ENV_VAR] = "DEBUG"
def set_verbosity(verbosity: int) -> None:
os.environ[VERBOSITY_ENV_VAR] = str(verbosity)
if verbosity > 0:
os.environ[DEBUG_ENV_VAR] = "true"
os.environ[GRPC_DEBUG_ENV_VAR] = "DEBUG"
elif verbosity < 0:
os.environ[QUIET_ENV_VAR] = "true"
os.environ[GRPC_DEBUG_ENV_VAR] = "NONE"


def set_debug_mode(enabled: bool = True) -> None:
set_verbosity(1)

logger.info(
"%s debug mode for current BentoML session",
"Enabling" if enabled else "Disabling",
)

def set_quiet_mode(enabled: bool = True) -> None:
set_verbosity(-1)


def get_debug_mode() -> bool:
if VERBOSITY_ENV_VAR in os.environ:
return int(os.environ[VERBOSITY_ENV_VAR]) > 0
if DEBUG_ENV_VAR in os.environ:
return os.environ[DEBUG_ENV_VAR].lower() == "true"
return False


def set_quiet_mode(enabled: bool) -> None:
# do not log setting quiet mode
os.environ[QUIET_ENV_VAR] = str(enabled)
os.environ[GRPC_DEBUG_ENV_VAR] = "NONE"


def get_quiet_mode() -> bool:
if VERBOSITY_ENV_VAR in os.environ:
return int(os.environ[VERBOSITY_ENV_VAR]) < 0
if QUIET_ENV_VAR in os.environ:
return os.environ[QUIET_ENV_VAR].lower() == "true"
return False
Expand Down
2 changes: 1 addition & 1 deletion src/bentoml_cli/bentos.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def build( # type: ignore (not accessed)
from bentoml._internal.log import configure_logging

if output == "tag":
set_quiet_mode(True)
set_quiet_mode()
configure_logging()

try:
Expand Down
16 changes: 5 additions & 11 deletions src/bentoml_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,10 @@ def with_options(self, **attrs: t.Any) -> t.Any:


def setup_verbosity(ctx: Context, param: Parameter, value: int) -> int:
from bentoml._internal.configuration import set_debug_mode
from bentoml._internal.configuration import set_quiet_mode
from bentoml._internal.configuration import set_verbosity
from bentoml._internal.log import configure_logging

if value == 1:
set_debug_mode(True)
elif value == -1:
set_quiet_mode(True)

set_verbosity(value or 0)
configure_logging()
return value

Expand Down Expand Up @@ -261,8 +256,7 @@ def serve(): ...
@staticmethod
def bentoml_common_params(f: F[P]) -> ClickFunctionWrapper[P]:
# NOTE: update NUMBER_OF_COMMON_PARAMS when adding option.
from bentoml._internal.configuration import DEBUG_ENV_VAR
from bentoml._internal.configuration import QUIET_ENV_VAR
from bentoml._internal.configuration import VERBOSITY_ENV_VAR
from bentoml._internal.utils.analytics import BENTOML_DO_NOT_TRACK

f = cog.optgroup.option(
Expand All @@ -272,7 +266,8 @@ def bentoml_common_params(f: F[P]) -> ClickFunctionWrapper[P]:
flag_value=-1,
default=0,
expose_value=False,
envvar=QUIET_ENV_VAR,
envvar=VERBOSITY_ENV_VAR,
type=click.INT,
help="Suppress all warnings and info logs",
callback=setup_verbosity,
is_eager=True,
Expand All @@ -283,7 +278,6 @@ def bentoml_common_params(f: F[P]) -> ClickFunctionWrapper[P]:
"verbosity",
flag_value=1,
expose_value=False,
envvar=DEBUG_ENV_VAR,
help="Generate debug information",
)(f)
f = cog.optgroup.option(
Expand Down

0 comments on commit bd47504

Please sign in to comment.