From e071aec2868c2e39ee59d24979551b8e3ca98ed4 Mon Sep 17 00:00:00 2001 From: Timofey Date: Wed, 4 Oct 2023 12:09:32 +0500 Subject: [PATCH] 0.2.2 Fix debug logger. Add logging about api docs. Add debug logging error repsonses (#4) Co-authored-by: Deys Timofey --- TODO.md | 1 + mlup/__init__.py | 2 +- mlup/config.py | 8 +++++--- mlup/console_scripts/command.py | 9 ++++++++- mlup/console_scripts/run.py | 4 +++- mlup/ml/model.py | 2 +- mlup/up.py | 4 ++-- mlup/web/api_errors.py | 13 +++++++++++++ mlup/web/app.py | 6 ++++++ 9 files changed, 40 insertions(+), 9 deletions(-) diff --git a/TODO.md b/TODO.md index 92ec819..538a3f3 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,7 @@ # TODO * [x] Full docs by all pymlup library. +* [x] Fix debug logger in mlup run command. * [ ] Added length inner array validation. * [ ] Modern mlup validate-config command. * [ ] Auto search params by model and test data. diff --git a/mlup/__init__.py b/mlup/__init__.py index e0d1625..473bc8e 100644 --- a/mlup/__init__.py +++ b/mlup/__init__.py @@ -3,4 +3,4 @@ from mlup.web.app import MLupWebApp, WebAppConfig -__version__ = "0.2.1" +__version__ = "0.2.2" diff --git a/mlup/config.py b/mlup/config.py index e7dd33d..c33a468 100644 --- a/mlup/config.py +++ b/mlup/config.py @@ -4,15 +4,17 @@ from dataclasses import dataclass from enum import Enum from pathlib import Path -from typing import Dict, Any, Set, Union +from typing import Dict, Any, Set, Union, Optional import yaml from mlup.constants import StorageType -def set_logging_settings(logging_config: Dict): +def set_logging_settings(logging_config: Dict, level: Optional[int] = None): logging.config.dictConfig(logging_config) + if level: + logging.basicConfig(level=level) LOGGING_CONFIG: Dict[str, Any] = { @@ -54,7 +56,7 @@ def set_logging_settings(logging_config: Dict): }, }, "loggers": { - "mlup": {"handlers": ["mlup"], "level": "INFO", "propagate": False}, + "mlup": {"handlers": ["mlup"], "propagate": False}, "uvicorn": {"handlers": ["uvicorn"], "level": "INFO", "propagate": False}, "uvicorn.error": {"handlers": ["uvicorn"], "level": "INFO", "propagate": False}, "uvicorn.access": {"handlers": ["access"], "level": "INFO", "propagate": False}, diff --git a/mlup/console_scripts/command.py b/mlup/console_scripts/command.py index cb974b9..42aa0f6 100644 --- a/mlup/console_scripts/command.py +++ b/mlup/console_scripts/command.py @@ -2,6 +2,8 @@ import sys from typing import List +import mlup + AVAILABLE_COMMANDS = """Available commands: * validate-config Validate your config for valid format for MLup. @@ -10,13 +12,14 @@ """ -HELP = f"""usage: mlup [-h] command +HELP = f"""usage: mlup [-h] [-v] command positional arguments: command Command name for run options: -h, --help show this help message and exit + -v, --version show current version mlup and exit {AVAILABLE_COMMANDS} """ @@ -33,6 +36,10 @@ def run_command(args: List[str]): print(HELP) sys.exit() + if command.strip() in ('-v', '--version', 'version'): + print(mlup.__version__) + sys.exit() + command = command.replace('-', '_') try: diff --git a/mlup/console_scripts/run.py b/mlup/console_scripts/run.py index 66072f1..5ced7da 100644 --- a/mlup/console_scripts/run.py +++ b/mlup/console_scripts/run.py @@ -69,7 +69,9 @@ def run( verbose: bool = False, ): if verbose: - logger.setLevel(logging.DEBUG) + logging.basicConfig() + logging.getLogger('mlup').setLevel(logging.DEBUG) + # logger.setLevel(logging.DEBUG) logger.debug('Run in verbose mode') if any(((use_conf and use_bin), (use_conf and use_model), (use_bin and use_model))): diff --git a/mlup/ml/model.py b/mlup/ml/model.py index 41b9839..6907eb4 100644 --- a/mlup/ml/model.py +++ b/mlup/ml/model.py @@ -26,7 +26,7 @@ from mlup.utils.interspection import analyze_method_params, get_class_by_path, auto_search_binarization_type -set_logging_settings(LOGGING_CONFIG) +set_logging_settings(LOGGING_CONFIG, level=logging.INFO) logger = logging.getLogger('mlup') diff --git a/mlup/up.py b/mlup/up.py index 1188d7c..235f62f 100644 --- a/mlup/up.py +++ b/mlup/up.py @@ -3,14 +3,14 @@ from pathlib import Path from typing import Dict, Any, Optional, Union -from mlup.config import ConfigProvider, LOGGING_CONFIG +from mlup.config import ConfigProvider, LOGGING_CONFIG, set_logging_settings from mlup.ml.empty import EmptyModel from mlup.ml.model import MLupModel, ModelConfig from mlup.utils.loop import run_async from mlup.web.app import MLupWebApp, WebAppConfig -logging.config.dictConfig(LOGGING_CONFIG) +set_logging_settings(LOGGING_CONFIG, level=logging.INFO) logger = logging.getLogger('mlup') diff --git a/mlup/web/api_errors.py b/mlup/web/api_errors.py index 750fb25..49a974b 100644 --- a/mlup/web/api_errors.py +++ b/mlup/web/api_errors.py @@ -1,3 +1,4 @@ +import logging from dataclasses import dataclass from typing import List, Union, Optional @@ -11,6 +12,9 @@ PredictValidationInnerDataError +logger = logging.getLogger('mlup') + + class ApiError(BaseModel): loc: List msg: str @@ -35,6 +39,9 @@ def api_exception_handler(request: Request, exc: Union[ValidationError, ApiReque if hasattr(exc, 'predict_id'): error_res.predict_id = exc.predict_id headers[PREDICT_ID_HEADER] = exc.predict_id + + logger.debug(f'Response error. Status: {status.HTTP_422_UNPROCESSABLE_ENTITY}. Body: {error_res.dict()}.') + return JSONResponse( content=jsonable_encoder(error_res.dict()), status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, @@ -48,6 +55,9 @@ def api_request_error_handler(request: Request, exc: ApiRequestError): if hasattr(exc, 'predict_id'): error_res.predict_id = exc.predict_id headers[PREDICT_ID_HEADER] = exc.predict_id + + logger.debug(f'Response error. Status: {exc.status_code}. Body: {error_res.dict()}.') + return JSONResponse( content=jsonable_encoder(error_res.dict()), status_code=exc.status_code, @@ -64,6 +74,9 @@ def predict_errors_handler( if hasattr(exc, 'predict_id'): error_res.predict_id = exc.predict_id headers[PREDICT_ID_HEADER] = exc.predict_id + + logger.debug(f'Response error. Status: {exc.http_status}. Body: {error_res.dict()}.') + return JSONResponse( content=jsonable_encoder(error_res.dict()), status_code=exc.http_status, diff --git a/mlup/web/app.py b/mlup/web/app.py index 7a8c0aa..aa73978 100644 --- a/mlup/web/app.py +++ b/mlup/web/app.py @@ -373,6 +373,12 @@ def _run(self, in_thread: bool = False): self.conf.uvicorn_kwargs['log_config'] = LOGGING_CONFIG configure_logging_formatter('web') + logger.info(f'MLup application will be launched at: http://{self.conf.host}:{self.conf.port}') + if self.conf.show_docs: + logger.info( + f"You can open your application's API documentation at http://{self.conf.host}:{self.conf.port}/docs" + ) + self._uvicorn_server = uvicorn.Server( uvicorn.Config(self.app, **self.conf.uvicorn_kwargs), )