Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logger for gptcache #216

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gptcache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from gptcache.similarity_evaluation.similarity_evaluation import SimilarityEvaluation
from gptcache.similarity_evaluation.exact_match import ExactMatchEvaluation
from gptcache.utils.error import CacheError
from gptcache.utils.log import gptcache_log


def cache_all(*_, **__):
Expand Down Expand Up @@ -183,7 +184,7 @@ def close():
try:
self.data_manager.close()
except Exception as e: # pylint: disable=W0703
print(e)
gptcache_log.error(e)

def import_data(self, questions: List[Any], answers: List[Any]) -> None:
""" Import data to GPTCache
Expand Down
3 changes: 2 additions & 1 deletion gptcache/manager/data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from gptcache.manager.vector_data.base import VectorBase, VectorData
from gptcache.manager.object_data.base import ObjectBase
from gptcache.manager.eviction import EvictionManager
from gptcache.utils.log import gptcache_log


class DataManager(metaclass=ABCMeta):
Expand Down Expand Up @@ -103,7 +104,7 @@ def close(self):
with open(self.data_path, "wb") as f:
pickle.dump(self.data, f)
except PermissionError:
print(f"You don't have permission to access this file <{self.data_path}>.")
gptcache_log.error("You don't have permission to access this file %s.", self.data_path)


def normalize(vec):
Expand Down
6 changes: 2 additions & 4 deletions gptcache/manager/object_data/local_storage.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from typing import Any, List
import os
import logging
import uuid
from pathlib import Path
from gptcache.manager.object_data.base import ObjectBase

logger = logging.getLogger()
from gptcache.utils.log import gptcache_log


class LocalObjectStorage(ObjectBase):
Expand Down Expand Up @@ -38,5 +36,5 @@ def delete(self, to_delete: List[str]):
try:
os.remove(obj)
except Exception: # pylint: disable=broad-except
logger.warning("Can not find obj: %s", obj)
gptcache_log.warning("Can not find obj: %s", obj)
pass
6 changes: 2 additions & 4 deletions gptcache/manager/object_data/s3_storage.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from typing import Any, List
import uuid
import os
import logging

from gptcache.manager.object_data.base import ObjectBase

from gptcache.utils import import_boto3
from gptcache.utils.log import gptcache_log

import_boto3()
import boto3 # pylint: disable=wrong-import-position

logger = logging.getLogger()


class S3Storage(ObjectBase):
"""S3 storage
Expand All @@ -36,7 +34,7 @@ def get(self, obj: str) -> Any:
try:
return self._s3.Bucket(self._bucket).Object(obj).get()["Body"].read()
except Exception: # pylint: disable=broad-except
logger.error("obj:%s not exist", obj)
gptcache_log.error("obj:%s not exist", obj)
return None

def get_access_link(self, obj: str, expires: int = 3600) -> str:
Expand Down
10 changes: 5 additions & 5 deletions gptcache/manager/vector_data/milvus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np

from gptcache.utils import import_pymilvus
from gptcache.utils.log import gptcache_log
from gptcache.manager.vector_data.base import VectorBase, VectorData


Expand Down Expand Up @@ -142,19 +143,18 @@ def _create_collection(self, collection_name):
using=self.alias,
)
else:
print(f"There already has {collection_name} collection, will using it directly.")
gptcache_log.warning("The %s collection already exists, and it will be used directly.", collection_name)
self.col = Collection(
collection_name, consistency_level="Strong", using=self.alias
)

if len(self.col.indexes) == 0:
try:
print("Attempting creation of Milvus index")
gptcache_log.info("Attempting creation of Milvus index.")
self.col.create_index("embedding", index_params=self.index_params)
print("Creation of Milvus index successful")
gptcache_log.info("Creation of Milvus index successful.")
except MilvusException as e:
print("Error with building index: ", e)
print("Attempting creation of default index")
gptcache_log.warning("Error with building index: %s, and attempting creation of default index.", e)
i_p = {"metric_type": "L2", "index_type": "AUTOINDEX", "params": {}}
self.col.create_index("embedding", index_params=i_p)
self.index_params = i_p
Expand Down
3 changes: 2 additions & 1 deletion gptcache/utils/dependency_control.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import subprocess

from gptcache.utils.error import PipInstallError
from gptcache.utils.log import gptcache_log


def prompt_install(package: str, warn: bool = False): # pragma: no cover
Expand All @@ -12,6 +13,6 @@ def prompt_install(package: str, warn: bool = False): # pragma: no cover
if warn and input(f"Install {package}? Y/n: ") != "Y":
raise ModuleNotFoundError(f"No module named {package}")
subprocess.check_call(cmd, shell=True)
print(f"{package} installed successfully!")
gptcache_log.info("%s installed successfully!", package)
except subprocess.CalledProcessError as e:
raise PipInstallError(package) from e
6 changes: 6 additions & 0 deletions gptcache/utils/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import logging

FORMAT = '%(asctime)s - %(thread)d - %(filename)s-%(module)s:%(lineno)s - %(levelname)s: %(message)s'
logging.basicConfig(format=FORMAT)

gptcache_log = logging.getLogger('gptcache')
9 changes: 9 additions & 0 deletions tests/unit_tests/utils/test_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from gptcache.utils.log import gptcache_log


def test_error_type():
gptcache_log.setLevel("INFO")
gptcache_log.error("Cache log error.")
gptcache_log.warning("Cache log warning.")
gptcache_log.info("Cache log info.")
assert gptcache_log.level == 20