Skip to content

Commit 4068b95

Browse files
committed
feat: enhance BaseSearcher
1 parent 628b8bd commit 4068b95

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

graphgen/bases/base_searcher.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1+
import logging
2+
import os
13
from abc import ABC, abstractmethod
24
from typing import Any, Dict, List
35

6+
from graphgen.utils.log import set_logger
7+
48

59
class BaseSearcher(ABC):
610
"""
711
Abstract base class for searching and retrieving data.
812
"""
913

14+
def __init__(self, working_dir: str = "cache"):
15+
"""
16+
Initialize the base searcher with a logger.
17+
18+
:param working_dir: Working directory for log files.
19+
"""
20+
log_dir = os.path.join(working_dir, "logs")
21+
searcher_name = self.__class__.__name__
22+
23+
# e.g. cache/logs/NCBISearch.log
24+
log_file = os.path.join(log_dir, f"{searcher_name}.log")
25+
26+
self.logger = set_logger(
27+
log_file=log_file, name=searcher_name,
28+
console_level=logging.ERROR, force=True
29+
)
30+
31+
self.logger.info(
32+
"[%s] Searcher initialized", searcher_name
33+
)
34+
1035
@abstractmethod
1136
async def search(self, query: str, **kwargs) -> List[Dict[str, Any]]:
1237
"""
@@ -16,3 +41,7 @@ async def search(self, query: str, **kwargs) -> List[Dict[str, Any]]:
1641
:param kwargs: Additional keyword arguments for the searcher.
1742
:return: List of dictionaries containing the searcher results.
1843
"""
44+
45+
def get_logger(self):
46+
"""Get the logger instance."""
47+
return self.logger

0 commit comments

Comments
 (0)