Skip to content

Commit

Permalink
Merge pull request #98 from small-thinking/update-logger-2
Browse files Browse the repository at this point in the history
Update logger
  • Loading branch information
yxjiang authored Jul 23, 2024
2 parents dfee4f8 + f1d3458 commit ace4ca6
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ jobs:
echo "TAVILY_API_KEY=fake-api-key" >> $GITHUB_ENV
- name: Run Pytest
run: poetry run pytest -vv
run: poetry run pytest -vv --cov=polymind --cov-config=pyproject.toml -vv tests
87 changes: 86 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 52 additions & 43 deletions polymind/core/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import os
from enum import Enum
from typing import Optional
from typing import Optional, Union

from colorama import Fore, ansi
from dotenv import load_dotenv
Expand All @@ -12,57 +12,66 @@ class Logger:
_instance = None

class LoggingLevel(Enum):
DEBUG = 1
INFO = 2
TOOL = 3
TASK = 4
THOUGHT_PROCESS = 5
WARNING = 6
ERROR = 7
CRITICAL = 8

def __lt__(self, other):
return self.value < other.value

def __ge__(self, other):
return self.value >= other.value

def __le__(self, other):
return self.value <= other.value

def __gt__(self, other):
return self.value > other.value

def __str__(self) -> str:
return self.name

def __repr__(self) -> str:
return self.name
DEBUG = logging.DEBUG
INFO = logging.INFO
TOOL = 25
TASK = 26
THOUGHT_PROCESS = 27
WARNING = logging.WARNING
ERROR = logging.ERROR
CRITICAL = logging.CRITICAL

@classmethod
def from_string(cls, level_string: str):
try:
return cls[level_string.upper()]
except KeyError:
raise ValueError(f"Invalid logging level: {level_string}")

def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance

def __init__(self, logger_name: str, verbose: bool = True, level: Optional[LoggingLevel] = None):
if not hasattr(self, "logger"):
load_dotenv(override=True)
self.logging_level = level if level else Logger.LoggingLevel[os.getenv("LOGGING_LEVEL", "TOOL")]
self.logger = logging.getLogger(logger_name)
self.logger.setLevel(level=self.logging_level.value)
self.formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s (%(filename)s:%(lineno)d)")
self.console_handler = logging.StreamHandler()
self.console_handler.setLevel(level=self.logging_level.value)
self.console_handler.setFormatter(self.formatter)
self.logger.addHandler(self.console_handler)
def __init__(
self,
logger_name: str,
verbose: bool = True,
display_level: Optional[Union[LoggingLevel, str]] = None,
):
load_dotenv(override=True)

if display_level is None:
env_level = os.getenv("LOGGING_LEVEL", "INFO")
self.logging_level = self.LoggingLevel.from_string(env_level)
elif isinstance(display_level, str):
self.logging_level = self.LoggingLevel.from_string(display_level)
else:
self.logging_level = display_level

self.logger = logging.getLogger(logger_name)
self.logger.setLevel(self.logging_level.value)

if self.logger.hasHandlers():
self.logger.handlers.clear()

self.formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s (%(filename)s:%(lineno)d)")
self.console_handler = logging.StreamHandler()
self.console_handler.setLevel(self.logging_level.value)
self.console_handler.setFormatter(self.formatter)
self.logger.addHandler(self.console_handler)

def log(self, message: str, level: LoggingLevel, color: str = ansi.Fore.GREEN) -> None:
if level >= self.logging_level:
caller_frame = inspect.stack()[2]
caller_name = caller_frame[3]
caller_line = caller_frame[2]
if level.value >= self.logging_level.value:
if len(inspect.stack()) >= 4:
caller_frame = inspect.stack()[3]
else:
caller_frame = inspect.stack()[2]
caller_name = caller_frame.function
caller_line = caller_frame.lineno
message = f"{caller_name}({caller_line}): {message}"
self.logger.info(color + message + Fore.RESET)
log_message = color + message + Fore.RESET
self.logger.log(level.value, log_message)

def debug(self, message: str) -> None:
self.log(message, Logger.LoggingLevel.DEBUG, Fore.BLACK)
Expand Down
12 changes: 11 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "polymind"
version = "0.0.56" # Update this version before publishing to PyPI
version = "0.0.57" # Update this version before publishing to PyPI
description = "PolyMind is a customizable collaborative multi-agent framework for collective intelligence and distributed problem solving."
authors = ["TechTao"]
license = "MIT License"
Expand All @@ -24,6 +24,7 @@ pymilvus = { version = "2.3.7", optional = true }
dspy-ai = "^2.4.9"
rdflib = "^7.0.0"
flake8 = "^7.1.0"
pytest-cov = "^5.0.0"

[tool.poetry.group.dev.dependencies]
black = "^24.2.0"
Expand All @@ -34,6 +35,7 @@ pytest = "^8.1.1"
pytest-asyncio = "^0.23.5.post1"
vulture = "^2.11"
flake8-pyproject = "^1.2.3"
coverage = "^7.6.0"

[tool.poetry.extras]
openai = ["openai"]
Expand All @@ -54,3 +56,11 @@ ignore = ["Q000", "E203"]
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.pytest.ini_options]
minversion = "6.0"
addopts = [
"--cov=your_module_name",
"--cov-fail-under=67"
]

Loading

0 comments on commit ace4ca6

Please sign in to comment.