Skip to content

Commit

Permalink
0.2.2 Fix debug logger. Add logging about api docs. Add debug logging…
Browse files Browse the repository at this point in the history
… error repsonses (#4)

Co-authored-by: Deys Timofey <[email protected]>
  • Loading branch information
nxexox and Deys Timofey authored Oct 4, 2023
1 parent f60a8f0 commit e071aec
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion mlup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from mlup.web.app import MLupWebApp, WebAppConfig


__version__ = "0.2.1"
__version__ = "0.2.2"
8 changes: 5 additions & 3 deletions mlup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down Expand Up @@ -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},
Expand Down
9 changes: 8 additions & 1 deletion mlup/console_scripts/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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}
"""
Expand All @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion mlup/console_scripts/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))):
Expand Down
2 changes: 1 addition & 1 deletion mlup/ml/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')


Expand Down
4 changes: 2 additions & 2 deletions mlup/up.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')


Expand Down
13 changes: 13 additions & 0 deletions mlup/web/api_errors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from dataclasses import dataclass
from typing import List, Union, Optional

Expand All @@ -11,6 +12,9 @@
PredictValidationInnerDataError


logger = logging.getLogger('mlup')


class ApiError(BaseModel):
loc: List
msg: str
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions mlup/web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down

0 comments on commit e071aec

Please sign in to comment.