Skip to content

Commit 04b842b

Browse files
authored
Merge pull request #20 from peterschutt/logging-config
Log via `Logger` objects.
2 parents 5649766 + eabff8d commit 04b842b

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1+
import logging
2+
13
from .v3 import *
4+
5+
6+
logging.getLogger(__name__).addHandler(logging.NullHandler())

openapi_schema_pydantic/util.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from . import Components, OpenAPI, Reference, Schema
88

9+
logger = logging.getLogger(__name__)
10+
911
PydanticType = TypeVar("PydanticType", bound=BaseModel)
1012
ref_prefix = "#/components/schemas/"
1113

@@ -46,7 +48,7 @@ def construct_open_api_with_schema_class(
4648
return open_api
4749

4850
schema_classes.sort(key=lambda x: x.__name__)
49-
logging.debug(f"schema_classes{schema_classes}")
51+
logger.debug(f"schema_classes{schema_classes}")
5052

5153
# update new_open_api with new #/components/schemas
5254
schema_definitions = schema(schema_classes, by_alias=by_alias, ref_prefix=ref_prefix)
@@ -55,7 +57,7 @@ def construct_open_api_with_schema_class(
5557
if new_open_api.components.schemas:
5658
for existing_key in new_open_api.components.schemas:
5759
if existing_key in schema_definitions.get("definitions"):
58-
logging.warning(
60+
logger.warning(
5961
f'"{existing_key}" already exists in {ref_prefix}. '
6062
f'The value of "{ref_prefix}{existing_key}" will be overwritten.'
6163
)
@@ -90,23 +92,23 @@ def _traverse(obj: Any):
9092
for field in fields:
9193
child_obj = obj.__getattribute__(field)
9294
if isinstance(child_obj, PydanticSchema):
93-
logging.debug(f"PydanticSchema found in {obj.__repr_name__()}: {child_obj}")
95+
logger.debug(f"PydanticSchema found in {obj.__repr_name__()}: {child_obj}")
9496
obj.__setattr__(field, _construct_ref_obj(child_obj))
9597
pydantic_types.add(child_obj.schema_class)
9698
else:
9799
_traverse(child_obj)
98100
elif isinstance(obj, list):
99101
for index, elem in enumerate(obj):
100102
if isinstance(elem, PydanticSchema):
101-
logging.debug(f"PydanticSchema found in list: {elem}")
103+
logger.debug(f"PydanticSchema found in list: {elem}")
102104
obj[index] = _construct_ref_obj(elem)
103105
pydantic_types.add(elem.schema_class)
104106
else:
105107
_traverse(elem)
106108
elif isinstance(obj, dict):
107109
for key, value in obj.items():
108110
if isinstance(value, PydanticSchema):
109-
logging.debug(f"PydanticSchema found in dict: {value}")
111+
logger.debug(f"PydanticSchema found in dict: {value}")
110112
obj[key] = _construct_ref_obj(value)
111113
pydantic_types.add(value.schema_class)
112114
else:
@@ -118,5 +120,5 @@ def _traverse(obj: Any):
118120

119121
def _construct_ref_obj(pydantic_schema: PydanticSchema):
120122
ref_obj = Reference(ref=ref_prefix + pydantic_schema.schema_class.__name__)
121-
logging.debug(f"ref_obj={ref_obj}")
123+
logger.debug(f"ref_obj={ref_obj}")
122124
return ref_obj

openapi_schema_pydantic/v3/v3_0_3/util.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from . import Components, OpenAPI, Reference, Schema
88

9+
logger = logging.getLogger(__name__)
10+
911
PydanticType = TypeVar("PydanticType", bound=BaseModel)
1012
ref_prefix = "#/components/schemas/"
1113

@@ -46,7 +48,7 @@ def construct_open_api_with_schema_class(
4648
return open_api
4749

4850
schema_classes.sort(key=lambda x: x.__name__)
49-
logging.debug(f"schema_classes{schema_classes}")
51+
logger.debug(f"schema_classes{schema_classes}")
5052

5153
# update new_open_api with new #/components/schemas
5254
schema_definitions = schema(schema_classes, by_alias=by_alias, ref_prefix=ref_prefix)
@@ -55,7 +57,7 @@ def construct_open_api_with_schema_class(
5557
if new_open_api.components.schemas:
5658
for existing_key in new_open_api.components.schemas:
5759
if existing_key in schema_definitions.get("definitions"):
58-
logging.warning(
60+
logger.warning(
5961
f'"{existing_key}" already exists in {ref_prefix}. '
6062
f'The value of "{ref_prefix}{existing_key}" will be overwritten.'
6163
)
@@ -90,23 +92,23 @@ def _traverse(obj: Any):
9092
for field in fields:
9193
child_obj = obj.__getattribute__(field)
9294
if isinstance(child_obj, PydanticSchema):
93-
logging.debug(f"PydanticSchema found in {obj.__repr_name__()}: {child_obj}")
95+
logger.debug(f"PydanticSchema found in {obj.__repr_name__()}: {child_obj}")
9496
obj.__setattr__(field, _construct_ref_obj(child_obj))
9597
pydantic_types.add(child_obj.schema_class)
9698
else:
9799
_traverse(child_obj)
98100
elif isinstance(obj, list):
99101
for index, elem in enumerate(obj):
100102
if isinstance(elem, PydanticSchema):
101-
logging.debug(f"PydanticSchema found in list: {elem}")
103+
logger.debug(f"PydanticSchema found in list: {elem}")
102104
obj[index] = _construct_ref_obj(elem)
103105
pydantic_types.add(elem.schema_class)
104106
else:
105107
_traverse(elem)
106108
elif isinstance(obj, dict):
107109
for key, value in obj.items():
108110
if isinstance(value, PydanticSchema):
109-
logging.debug(f"PydanticSchema found in dict: {value}")
111+
logger.debug(f"PydanticSchema found in dict: {value}")
110112
obj[key] = _construct_ref_obj(value)
111113
pydantic_types.add(value.schema_class)
112114
else:
@@ -118,5 +120,5 @@ def _traverse(obj: Any):
118120

119121
def _construct_ref_obj(pydantic_schema: PydanticSchema):
120122
ref_obj = Reference(ref=ref_prefix + pydantic_schema.schema_class.__name__)
121-
logging.debug(f"ref_obj={ref_obj}")
123+
logger.debug(f"ref_obj={ref_obj}")
122124
return ref_obj

0 commit comments

Comments
 (0)