From 4c52ef62aa7f56a6d97c193dc8935b5dec9c3a94 Mon Sep 17 00:00:00 2001 From: Vasilije <8619304+Vasilije1990@users.noreply.github.com> Date: Tue, 20 May 2025 20:25:49 +0200 Subject: [PATCH] feat: added util logger OS (#841) ## Description ## DCO Affirmation I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin. --------- Co-authored-by: Igor Ilic Co-authored-by: Igor Ilic <30923996+dexters1@users.noreply.github.com> --- cognee/__init__.py | 6 ++++++ cognee/shared/logging_utils.py | 25 +++++++++++++++++++++++-- cognee/version.py | 24 ++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 cognee/version.py diff --git a/cognee/__init__.py b/cognee/__init__.py index fe80eeb21..ee637ae9b 100644 --- a/cognee/__init__.py +++ b/cognee/__init__.py @@ -1,3 +1,9 @@ +from cognee.version import get_cognee_version + +# NOTE: __version__ extraction must be at the top of the __init__.py otherwise +# there will be circular import issues +__version__ = get_cognee_version() + from .api.v1.add import add from .api.v1.delete import delete from .api.v1.cognify import cognify diff --git a/cognee/shared/logging_utils.py b/cognee/shared/logging_utils.py index bfd4828f8..26bea77d9 100644 --- a/cognee/shared/logging_utils.py +++ b/cognee/shared/logging_utils.py @@ -4,8 +4,12 @@ import threading import logging import structlog import traceback +import platform from datetime import datetime from pathlib import Path +import importlib.metadata + +from cognee import __version__ as cognee_version # Export common log levels DEBUG = logging.DEBUG @@ -36,6 +40,13 @@ LOGS_DIR.mkdir(exist_ok=True) # Create logs dir if it doesn't exist # Maximum number of log files to keep MAX_LOG_FILES = 10 +# Version information +PYTHON_VERSION = platform.python_version() +STRUCTLOG_VERSION = structlog.__version__ +COGNEE_VERSION = cognee_version + +OS_INFO = f"{platform.system()} {platform.release()} ({platform.version()})" + class PlainFileHandler(logging.FileHandler): """A custom file handler that writes simpler plain text log entries.""" @@ -326,8 +337,18 @@ def setup_logging(log_level=None, name=None): # Clean up old log files, keeping only the most recent ones cleanup_old_logs(LOGS_DIR, MAX_LOG_FILES) - # Return a configured logger - return structlog.get_logger(name if name else __name__) + # Get a configured logger and log system information + logger = structlog.get_logger(name if name else __name__) + logger.info( + "Logging initialized", + python_version=PYTHON_VERSION, + structlog_version=STRUCTLOG_VERSION, + cognee_version=COGNEE_VERSION, + os_info=OS_INFO, + ) + + # Return the configured logger + return logger def get_log_file_location(): diff --git a/cognee/version.py b/cognee/version.py new file mode 100644 index 000000000..db242cb72 --- /dev/null +++ b/cognee/version.py @@ -0,0 +1,24 @@ +import os +from contextlib import suppress +import importlib.metadata +from pathlib import Path + + +def get_cognee_version() -> str: + """Returns either the version of installed cognee package or the one + found in nearby pyproject.toml""" + with suppress(FileNotFoundError, StopIteration): + with open( + os.path.join(Path(__file__).parent.parent, "pyproject.toml"), encoding="utf-8" + ) as pyproject_toml: + version = ( + next(line for line in pyproject_toml if line.startswith("version")) + .split("=")[1] + .strip("'\"\n ") + ) + # Mark the version as a local Cognee library by appending “-dev” + return f"{version}-dev" + try: + return importlib.metadata.version("cognee") + except importlib.metadata.PackageNotFoundError: + return "unknown"