Använder numer LogLevel för att hantera nivån på loggar. Lade till get_endpoints_with_key_values()

This commit is contained in:
Joakim Persson
2024-08-05 14:18:29 +02:00
parent 01d4a5f314
commit 210a75e8bf
+24 -28
View File
@@ -5,7 +5,7 @@ import logging
import json import json
import requests import requests
from typing import Optional from typing import Optional
from enums import LogLevel from enums import LogLevel, LOG_LEVEL_MAPPING
class GlobalState: class GlobalState:
""" """
@@ -43,50 +43,34 @@ class GlobalState:
return cls._instance return cls._instance
# def configure_logging(self, level: Optional[LogLevel] = None) -> None:
# """
# Configure the logging system for this project.
# Args:
# level (LogLevel): The log level to use. If None, uses the default log level set in `self.log_level`.
# Notes:
# This method sets up logging for the project and logs a message at the debug level indicating the effective log level.
# """
def configure_logging(self, level: Optional[str] = None) -> None:
def configure_logging(self, level: Optional[LogLevel] = None) -> None:
""" """
Configure the logging system for this project. Configure the logging system for this project.
Args: Args:
level (str): The log level to use. Can be one of the standard Python log levels (e.g., 'DEBUG', 'INFO', 'WARNING', etc.). If None, uses the default log level set in `self.log_level`. level (LogLevel): The log level to use. If None, uses the default log level set in `self.log_level`.
Notes: Notes:
This method sets up logging for the project and logs a message at the debug level indicating the effective log level. This method sets up logging for the project and logs a message at the debug level indicating the effective log level.
""" """
if level is None: if level == None:
level = self.log_level level = self.log_level
# numeric_level = getattr(logging, level.upper()) # Convert string to numeric level if isinstance(level, LogLevel):
numeric_level = getattr(logging, level.upper()) # Convert string to numeric level logging.info(f"Trying to set up logging with level {level}")
numeric_level = LOG_LEVEL_MAPPING[level]
if numeric_level is None:
raise ValueError("Invalid log level")
self.logger.setLevel(numeric_level) self.logger.setLevel(numeric_level)
self.logger.debug(f"utils.py -- configure_logging(): effective log level is {level} which is {self.logger.getEffectiveLevel()}") self.logger.debug(f"utils.py -- configure_logging(): effective log level is {level} which is {self.logger.getEffectiveLevel()}")
# def set_log_level(self, level: LogLevel) -> None:
# """
# Set the log level for this project.
# Args: def set_log_level(self, level: LogLevel) -> None:
# level (LogLevel): The new log level to use. Can be one of the evels defined in enum.py (e.g., DEBUG, INFO, WARNING, CRITICAL etc.).
# Notes:
# This method updates the `self.log_level` attribute and calls `configure_logging()` to apply the change.
# """
def set_log_level(self, level: str = 'INFO') -> None:
""" """
Set the log level for this project. Set the log level for this project.
Args: Args:
level (str): The new log level to use. Can be one of the standard Python log levels (e.g., 'DEBUG', 'INFO', 'WARNING', etc.). level (LogLevel): The new log level to use. Can be one of the evels defined in enum.py (e.g., DEBUG, INFO, WARNING, CRITICAL etc.).
Notes: Notes:
This method updates the `self.log_level` attribute and calls `configure_logging()` to apply the change. This method updates the `self.log_level` attribute and calls `configure_logging()` to apply the change.
@@ -94,7 +78,7 @@ class GlobalState:
self.log_level = level self.log_level = level
self.configure_logging() self.configure_logging()
def get_log_level(self) -> str: def get_log_level(self) -> LogLevel:
""" """
Get the current log level. Get the current log level.
@@ -232,6 +216,18 @@ class GlobalState:
""" """
return [ep for ep in self.endpoints if key in ep] return [ep for ep in self.endpoints if key in ep]
def get_endpoints_with_key_values(self, key: str, value: any) -> list[dict]:
"""
Returns a list of endpoint dictionaries that contain the specified key-value pair.
Args:
key (str): The key to search for in the endpoint dictionaries.
value (Any): The value to search for in the endpoint dictionaries.
Returns:
list[dict]: A list of endpoint dictionaries containing the specified key.
"""
return [ep for ep in self.endpoints if key in ep and value == ep[key]]
def fetch_models(self) -> None: def fetch_models(self) -> None:
""" """