Browse Source

🔊 Refactor logging (#781)

pull/783/head
Sebastián Ramírez 5 years ago
committed by GitHub
parent
commit
83d04df8a6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      fastapi/encoders.py
  2. 3
      fastapi/logger.py
  3. 6
      fastapi/openapi/models.py
  4. 4
      fastapi/routing.py
  5. 14
      fastapi/utils.py

9
fastapi/encoders.py

@ -2,7 +2,8 @@ from enum import Enum
from types import GeneratorType from types import GeneratorType
from typing import Any, Dict, List, Set, Union from typing import Any, Dict, List, Set, Union
from fastapi.utils import PYDANTIC_1, logger from fastapi.logger import logger
from fastapi.utils import PYDANTIC_1
from pydantic import BaseModel from pydantic import BaseModel
from pydantic.json import ENCODERS_BY_TYPE from pydantic.json import ENCODERS_BY_TYPE
@ -23,9 +24,9 @@ def jsonable_encoder(
) -> Any: ) -> Any:
if skip_defaults is not None: if skip_defaults is not None:
logger.warning( # pragma: nocover logger.warning( # pragma: nocover
"skip_defaults in jsonable_encoder has been deprecated in \ "skip_defaults in jsonable_encoder has been deprecated in favor of "
favor of exclude_unset to keep in line with Pydantic v1, support for it \ "exclude_unset to keep in line with Pydantic v1, support for it will be "
will be removed soon." "removed soon."
) )
if include is not None and not isinstance(include, set): if include is not None and not isinstance(include, set):
include = set(include) include = set(include)

3
fastapi/logger.py

@ -0,0 +1,3 @@
import logging
logger = logging.getLogger("fastapi")

6
fastapi/openapi/models.py

@ -1,7 +1,7 @@
from enum import Enum from enum import Enum
from typing import Any, Dict, List, Optional, Union from typing import Any, Dict, List, Optional, Union
from fastapi.utils import logger from fastapi.logger import logger
from pydantic import BaseModel from pydantic import BaseModel
try: try:
@ -21,9 +21,9 @@ try:
# TODO: remove when removing support for Pydantic < 1.0.0 # TODO: remove when removing support for Pydantic < 1.0.0
from pydantic.types import EmailStr # type: ignore from pydantic.types import EmailStr # type: ignore
except ImportError: # pragma: no cover except ImportError: # pragma: no cover
logger.warning( logger.info(
"email-validator not installed, email fields will be treated as str.\n" "email-validator not installed, email fields will be treated as str.\n"
+ "To install, run: pip install email-validator" "To install, run: pip install email-validator"
) )
class EmailStr(str): # type: ignore class EmailStr(str): # type: ignore

4
fastapi/routing.py

@ -1,6 +1,5 @@
import asyncio import asyncio
import inspect import inspect
import logging
from typing import Any, Callable, Dict, List, Optional, Sequence, Set, Type, Union from typing import Any, Callable, Dict, List, Optional, Sequence, Set, Type, Union
from fastapi import params from fastapi import params
@ -13,6 +12,7 @@ from fastapi.dependencies.utils import (
) )
from fastapi.encoders import DictIntStrAny, SetIntStr, jsonable_encoder from fastapi.encoders import DictIntStrAny, SetIntStr, jsonable_encoder
from fastapi.exceptions import RequestValidationError, WebSocketRequestValidationError from fastapi.exceptions import RequestValidationError, WebSocketRequestValidationError
from fastapi.logger import logger
from fastapi.openapi.constants import STATUS_CODES_WITH_NO_BODY from fastapi.openapi.constants import STATUS_CODES_WITH_NO_BODY
from fastapi.utils import ( from fastapi.utils import (
PYDANTIC_1, PYDANTIC_1,
@ -108,7 +108,7 @@ def get_request_handler(
if body_bytes: if body_bytes:
body = await request.json() body = await request.json()
except Exception as e: except Exception as e:
logging.error(f"Error getting request body: {e}") logger.error(f"Error getting request body: {e}")
raise HTTPException( raise HTTPException(
status_code=400, detail="There was an error parsing the body" status_code=400, detail="There was an error parsing the body"
) from e ) from e

14
fastapi/utils.py

@ -1,17 +1,15 @@
import logging
import re import re
from dataclasses import is_dataclass from dataclasses import is_dataclass
from typing import Any, Dict, List, Sequence, Set, Type, cast from typing import Any, Dict, List, Sequence, Set, Type, cast
from fastapi import routing from fastapi import routing
from fastapi.logger import logger
from fastapi.openapi.constants import REF_PREFIX from fastapi.openapi.constants import REF_PREFIX
from pydantic import BaseConfig, BaseModel, create_model from pydantic import BaseConfig, BaseModel, create_model
from pydantic.schema import get_flat_models_from_fields, model_process_schema from pydantic.schema import get_flat_models_from_fields, model_process_schema
from pydantic.utils import lenient_issubclass from pydantic.utils import lenient_issubclass
from starlette.routing import BaseRoute from starlette.routing import BaseRoute
logger = logging.getLogger("fastapi")
try: try:
from pydantic.fields import FieldInfo, ModelField from pydantic.fields import FieldInfo, ModelField
@ -22,8 +20,8 @@ except ImportError: # pragma: nocover
from pydantic import Schema as FieldInfo # type: ignore from pydantic import Schema as FieldInfo # type: ignore
logger.warning( logger.warning(
"Pydantic versions < 1.0.0 are deprecated in FastAPI and support will be \ "Pydantic versions < 1.0.0 are deprecated in FastAPI and support will be "
removed soon" "removed soon."
) )
PYDANTIC_1 = False PYDANTIC_1 = False
@ -39,9 +37,9 @@ def get_field_info(field: ModelField) -> FieldInfo:
# TODO: remove when removing support for Pydantic < 1.0.0 # TODO: remove when removing support for Pydantic < 1.0.0
def warning_response_model_skip_defaults_deprecated() -> None: def warning_response_model_skip_defaults_deprecated() -> None:
logger.warning( # pragma: nocover logger.warning( # pragma: nocover
"response_model_skip_defaults has been deprecated in favor \ "response_model_skip_defaults has been deprecated in favor of "
of response_model_exclude_unset to keep in line with Pydantic v1, \ "response_model_exclude_unset to keep in line with Pydantic v1, support for "
support for it will be removed soon." "it will be removed soon."
) )

Loading…
Cancel
Save