fix: use lazy %-style interpolation in log calls
38 log calls eagerly interpolated the collection name into an f-string
(log.exception(f"db.method() on {collection}")) - the string was built even when the
log record was filtered out. switch to the lazy form (log.exception("db.method() on %s",
collection)) so interpolation happens only if the record is emitted. no level or handling
change - the swallow-and-default contract and the ERROR+traceback on the swallow path are
unchanged (that level is the D2 ruling: leave as-is).
Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
+38
-38
@@ -97,20 +97,20 @@ class MongoDB:
|
|||||||
await self._db.create_collection(collection)
|
await self._db.create_collection(collection)
|
||||||
if index:
|
if index:
|
||||||
await self._db[collection].create_index(index)
|
await self._db[collection].create_index(index)
|
||||||
log.info(f"created index for {collection}")
|
log.info("created index for %s", collection)
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.create_collection() for {collection}")
|
log.exception("db.create_collection() for %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def create_index(self, collection: str, index, **kwargs) -> bool:
|
async def create_index(self, collection: str, index, **kwargs) -> bool:
|
||||||
"""create an index; kwargs pass through (unique=True, name=..., etc)"""
|
"""create an index; kwargs pass through (unique=True, name=..., etc)"""
|
||||||
try:
|
try:
|
||||||
await self._db[collection].create_index(index, **kwargs)
|
await self._db[collection].create_index(index, **kwargs)
|
||||||
log.info(f"created index for {collection}")
|
log.info("created index for %s", collection)
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.create_index() for {collection}")
|
log.exception("db.create_index() for %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def drop_collection(self, collection: str) -> bool:
|
async def drop_collection(self, collection: str) -> bool:
|
||||||
@@ -119,7 +119,7 @@ class MongoDB:
|
|||||||
await self._db.drop_collection(collection)
|
await self._db.drop_collection(collection)
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.drop_collection() for {collection}")
|
log.exception("db.drop_collection() for %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def check_collection_exists(self, collection: str) -> bool:
|
async def check_collection_exists(self, collection: str) -> bool:
|
||||||
@@ -127,7 +127,7 @@ class MongoDB:
|
|||||||
try:
|
try:
|
||||||
return collection in await self._db.list_collection_names()
|
return collection in await self._db.list_collection_names()
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.check_collection_exists() for {collection}")
|
log.exception("db.check_collection_exists() for %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def list_collections(self) -> List[str]:
|
async def list_collections(self) -> List[str]:
|
||||||
@@ -147,7 +147,7 @@ class MongoDB:
|
|||||||
await self._db[collection].insert_one(document)
|
await self._db[collection].insert_one(document)
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.create_document() on {collection}")
|
log.exception("db.create_document() on %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def create_documents(
|
async def create_documents(
|
||||||
@@ -164,10 +164,10 @@ class MongoDB:
|
|||||||
result = await self._db[collection].insert_many(documents, ordered=ordered)
|
result = await self._db[collection].insert_many(documents, ordered=ordered)
|
||||||
return len(result.inserted_ids)
|
return len(result.inserted_ids)
|
||||||
except BulkWriteError as e:
|
except BulkWriteError as e:
|
||||||
log.exception(f"db.create_documents() on {collection}")
|
log.exception("db.create_documents() on %s", collection)
|
||||||
return e.details.get("nInserted", 0)
|
return e.details.get("nInserted", 0)
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.create_documents() on {collection}")
|
log.exception("db.create_documents() on %s", collection)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
@@ -180,7 +180,7 @@ class MongoDB:
|
|||||||
try:
|
try:
|
||||||
return await self._db[collection].find_one(target, fields)
|
return await self._db[collection].find_one(target, fields)
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.get_document() on {collection}")
|
log.exception("db.get_document() on %s", collection)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def get_documents(
|
async def get_documents(
|
||||||
@@ -191,7 +191,7 @@ class MongoDB:
|
|||||||
cursor = self._db[collection].find(target, fields)
|
cursor = self._db[collection].find(target, fields)
|
||||||
return [doc async for doc in cursor]
|
return [doc async for doc in cursor]
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.get_documents() on {collection}")
|
log.exception("db.get_documents() on %s", collection)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
async def get_documents_sorted(
|
async def get_documents_sorted(
|
||||||
@@ -209,7 +209,7 @@ class MongoDB:
|
|||||||
)
|
)
|
||||||
return [doc async for doc in cursor]
|
return [doc async for doc in cursor]
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.get_documents_sorted() on {collection}")
|
log.exception("db.get_documents_sorted() on %s", collection)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
async def get_document_hashmap(self, collection: str, target: dict, key: str) -> dict:
|
async def get_document_hashmap(self, collection: str, target: dict, key: str) -> dict:
|
||||||
@@ -222,7 +222,7 @@ class MongoDB:
|
|||||||
cursor = self._db[collection].find(target)
|
cursor = self._db[collection].find(target)
|
||||||
return {doc[key]: doc async for doc in cursor if key in doc}
|
return {doc[key]: doc async for doc in cursor if key in doc}
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.get_document_hashmap() on {collection}")
|
log.exception("db.get_document_hashmap() on %s", collection)
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
async def get_document_fields(
|
async def get_document_fields(
|
||||||
@@ -237,7 +237,7 @@ class MongoDB:
|
|||||||
cursor = self._db[collection].find(target, fields)
|
cursor = self._db[collection].find(target, fields)
|
||||||
return [doc[key] async for doc in cursor if key in doc]
|
return [doc[key] async for doc in cursor if key in doc]
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.get_document_fields() on {collection}")
|
log.exception("db.get_document_fields() on %s", collection)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
async def count_documents(self, collection: str, target: dict) -> int:
|
async def count_documents(self, collection: str, target: dict) -> int:
|
||||||
@@ -245,7 +245,7 @@ class MongoDB:
|
|||||||
try:
|
try:
|
||||||
return await self._db[collection].count_documents(target)
|
return await self._db[collection].count_documents(target)
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.count_documents() on {collection}")
|
log.exception("db.count_documents() on %s", collection)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
async def check_document_exists(self, collection: str, target: dict) -> bool:
|
async def check_document_exists(self, collection: str, target: dict) -> bool:
|
||||||
@@ -253,7 +253,7 @@ class MongoDB:
|
|||||||
try:
|
try:
|
||||||
return await self._db[collection].count_documents(target, limit=1) > 0
|
return await self._db[collection].count_documents(target, limit=1) > 0
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.check_document_exists() on {collection}")
|
log.exception("db.check_document_exists() on %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def exists(self, collection: str, target: dict) -> bool:
|
async def exists(self, collection: str, target: dict) -> bool:
|
||||||
@@ -265,7 +265,7 @@ class MongoDB:
|
|||||||
try:
|
try:
|
||||||
return await self._db[collection].count_documents({array_field: value})
|
return await self._db[collection].count_documents({array_field: value})
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.count_value_in_array() on {collection}")
|
log.exception("db.count_value_in_array() on %s", collection)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
async def exists_in_array(self, collection: str, array_field: str, value: str) -> bool:
|
async def exists_in_array(self, collection: str, array_field: str, value: str) -> bool:
|
||||||
@@ -274,7 +274,7 @@ class MongoDB:
|
|||||||
doc = await self._db[collection].find_one({array_field: value}, {"_id": 1})
|
doc = await self._db[collection].find_one({array_field: value}, {"_id": 1})
|
||||||
return doc is not None
|
return doc is not None
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.exists_in_array() on {collection}")
|
log.exception("db.exists_in_array() on %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def distinct(self, collection: str, field: str, target: Optional[dict] = None) -> List:
|
async def distinct(self, collection: str, field: str, target: Optional[dict] = None) -> List:
|
||||||
@@ -282,7 +282,7 @@ class MongoDB:
|
|||||||
try:
|
try:
|
||||||
return await self._db[collection].distinct(field, target or {})
|
return await self._db[collection].distinct(field, target or {})
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.distinct() on {collection}")
|
log.exception("db.distinct() on %s", collection)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
async def run_aggregation(
|
async def run_aggregation(
|
||||||
@@ -293,7 +293,7 @@ class MongoDB:
|
|||||||
stages = [{"$match": match_filter}, *pipeline] if match_filter else pipeline
|
stages = [{"$match": match_filter}, *pipeline] if match_filter else pipeline
|
||||||
return await self._db[collection].aggregate(stages).to_list(length=None)
|
return await self._db[collection].aggregate(stages).to_list(length=None)
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.run_aggregation() on {collection}")
|
log.exception("db.run_aggregation() on %s", collection)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
async def search_documents(
|
async def search_documents(
|
||||||
@@ -304,7 +304,7 @@ class MongoDB:
|
|||||||
cursor = self._db[collection].find(search_query).sort(sort_key, sort_order)
|
cursor = self._db[collection].find(search_query).sort(sort_key, sort_order)
|
||||||
return [doc async for doc in cursor]
|
return [doc async for doc in cursor]
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.search_documents() on {collection}")
|
log.exception("db.search_documents() on %s", collection)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
async def search_indexes(self, collection: str, search_term: str) -> List[dict]:
|
async def search_indexes(self, collection: str, search_term: str) -> List[dict]:
|
||||||
@@ -316,7 +316,7 @@ class MongoDB:
|
|||||||
).sort([("score", {"$meta": "textScore"})])
|
).sort([("score", {"$meta": "textScore"})])
|
||||||
return [doc async for doc in cursor]
|
return [doc async for doc in cursor]
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.search_indexes() on {collection}")
|
log.exception("db.search_indexes() on %s", collection)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
@@ -330,7 +330,7 @@ class MongoDB:
|
|||||||
response = await self._db[collection].replace_one(target, document, upsert=upsert)
|
response = await self._db[collection].replace_one(target, document, upsert=upsert)
|
||||||
return bool(response.matched_count or response.upserted_id)
|
return bool(response.matched_count or response.upserted_id)
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.update_document() on {collection}")
|
log.exception("db.update_document() on %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def update_documents(self, collection: str, target: dict, values: dict) -> int:
|
async def update_documents(self, collection: str, target: dict, values: dict) -> int:
|
||||||
@@ -339,7 +339,7 @@ class MongoDB:
|
|||||||
response = await self._db[collection].update_many(target, values)
|
response = await self._db[collection].update_many(target, values)
|
||||||
return response.modified_count
|
return response.modified_count
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.update_documents() on {collection}")
|
log.exception("db.update_documents() on %s", collection)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
async def update_document_field(self, collection: str, target: dict, updates: dict) -> bool:
|
async def update_document_field(self, collection: str, target: dict, updates: dict) -> bool:
|
||||||
@@ -353,7 +353,7 @@ class MongoDB:
|
|||||||
response = await self._db[collection].update_one(target, {"$set": updates})
|
response = await self._db[collection].update_one(target, {"$set": updates})
|
||||||
return response.matched_count > 0
|
return response.matched_count > 0
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.update_document_field() on {collection}")
|
log.exception("db.update_document_field() on %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def update_document_operator(
|
async def update_document_operator(
|
||||||
@@ -368,7 +368,7 @@ class MongoDB:
|
|||||||
response = await self._db[collection].update_one(target, update_query)
|
response = await self._db[collection].update_one(target, update_query)
|
||||||
return response.matched_count > 0
|
return response.matched_count > 0
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.update_document_operator() on {collection}")
|
log.exception("db.update_document_operator() on %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def upsert_document_field(self, collection: str, target: dict, updates: dict) -> bool:
|
async def upsert_document_field(self, collection: str, target: dict, updates: dict) -> bool:
|
||||||
@@ -384,7 +384,7 @@ class MongoDB:
|
|||||||
)
|
)
|
||||||
return bool(response.matched_count or response.upserted_id)
|
return bool(response.matched_count or response.upserted_id)
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.upsert_document_field() on {collection}")
|
log.exception("db.upsert_document_field() on %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def update_documents_pipeline(
|
async def update_documents_pipeline(
|
||||||
@@ -395,7 +395,7 @@ class MongoDB:
|
|||||||
response = await self._db[collection].update_many(target, pipeline)
|
response = await self._db[collection].update_many(target, pipeline)
|
||||||
return response.modified_count
|
return response.modified_count
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.update_documents_pipeline() on {collection}")
|
log.exception("db.update_documents_pipeline() on %s", collection)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
async def document_push_array(
|
async def document_push_array(
|
||||||
@@ -410,7 +410,7 @@ class MongoDB:
|
|||||||
response = await self._db[collection].update_one(target, {"$push": {array: value}})
|
response = await self._db[collection].update_one(target, {"$push": {array: value}})
|
||||||
return response.matched_count > 0
|
return response.matched_count > 0
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.document_push_array() on {collection}")
|
log.exception("db.document_push_array() on %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def document_push_and_set(
|
async def document_push_and_set(
|
||||||
@@ -428,7 +428,7 @@ class MongoDB:
|
|||||||
)
|
)
|
||||||
return response.matched_count > 0
|
return response.matched_count > 0
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.document_push_and_set() on {collection}")
|
log.exception("db.document_push_and_set() on %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def document_pop_array(
|
async def document_pop_array(
|
||||||
@@ -443,7 +443,7 @@ class MongoDB:
|
|||||||
response = await self._db[collection].update_one(target, {"$pull": {array: value}})
|
response = await self._db[collection].update_one(target, {"$pull": {array: value}})
|
||||||
return response.matched_count > 0
|
return response.matched_count > 0
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.document_pop_array() on {collection}")
|
log.exception("db.document_pop_array() on %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def find_one_and_update(
|
async def find_one_and_update(
|
||||||
@@ -459,7 +459,7 @@ class MongoDB:
|
|||||||
return_document=ReturnDocument.AFTER if return_after else ReturnDocument.BEFORE,
|
return_document=ReturnDocument.AFTER if return_after else ReturnDocument.BEFORE,
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.find_one_and_update() on {collection}")
|
log.exception("db.find_one_and_update() on %s", collection)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def find_one_and_replace(
|
async def find_one_and_replace(
|
||||||
@@ -475,7 +475,7 @@ class MongoDB:
|
|||||||
return_document=ReturnDocument.AFTER if return_after else ReturnDocument.BEFORE,
|
return_document=ReturnDocument.AFTER if return_after else ReturnDocument.BEFORE,
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.find_one_and_replace() on {collection}")
|
log.exception("db.find_one_and_replace() on %s", collection)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def find_one_and_delete(
|
async def find_one_and_delete(
|
||||||
@@ -485,7 +485,7 @@ class MongoDB:
|
|||||||
try:
|
try:
|
||||||
return await self._db[collection].find_one_and_delete(target, projection=fields)
|
return await self._db[collection].find_one_and_delete(target, projection=fields)
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.find_one_and_delete() on {collection}")
|
log.exception("db.find_one_and_delete() on %s", collection)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
@@ -497,7 +497,7 @@ class MongoDB:
|
|||||||
response = await self._db[collection].delete_one(target)
|
response = await self._db[collection].delete_one(target)
|
||||||
return response.deleted_count
|
return response.deleted_count
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.delete_document() on {collection}")
|
log.exception("db.delete_document() on %s", collection)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
async def delete(self, collection: str, target: dict) -> int:
|
async def delete(self, collection: str, target: dict) -> int:
|
||||||
@@ -510,7 +510,7 @@ class MongoDB:
|
|||||||
response = await self._db[collection].delete_many(target)
|
response = await self._db[collection].delete_many(target)
|
||||||
return response.deleted_count
|
return response.deleted_count
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.delete_documents() on {collection}")
|
log.exception("db.delete_documents() on %s", collection)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
@@ -534,7 +534,7 @@ class MongoDB:
|
|||||||
"upserted": result.upserted_count,
|
"upserted": result.upserted_count,
|
||||||
}
|
}
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.bulk_write() on {collection}")
|
log.exception("db.bulk_write() on %s", collection)
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
async def document_check_multi(
|
async def document_check_multi(
|
||||||
@@ -551,7 +551,7 @@ class MongoDB:
|
|||||||
}
|
}
|
||||||
return await self._db[collection].find_one(query) is not None
|
return await self._db[collection].find_one(query) is not None
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception(f"db.document_check_multi() on {collection}")
|
log.exception("db.document_check_multi() on %s", collection)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user