diff --git a/src/core/logging.py b/src/core/logging.py index e022732..48a06e7 100644 --- a/src/core/logging.py +++ b/src/core/logging.py @@ -1,47 +1,29 @@ -import logging -import http.client -import json import os +import json +import logging +from datetime import datetime class Logger: - def __init__(self, settings_path="../config/settings.json"): + def __init__(self, settings_path=None): + if settings_path is None: + settings_path = os.path.join(os.path.dirname(__file__), '../../config/settings.json') self.settings = self.load_settings(settings_path) - self.log_path = self.settings["log_path"] - os.makedirs(os.path.dirname(self.log_path), exist_ok=True) - - # Create a logger instance - self.logger = logging.getLogger("AgentMLogger") - self.logger.setLevel(logging.DEBUG if self.settings.get("debug", False) else logging.INFO) - - # File handler for logging to a file - file_handler = logging.FileHandler(self.log_path) - file_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")) - self.logger.addHandler(file_handler) - - # Console handler for output to the console - console_handler = logging.StreamHandler() - console_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")) - self.logger.addHandler(console_handler) - - # Enable HTTP-level logging if debug is enabled - if self.settings.get("debug", False): - self.enable_http_debug() + log_file_path = self.settings.get('log_path', './var/logs/error.log') + os.makedirs(os.path.dirname(log_file_path), exist_ok=True) + logging.basicConfig(filename=log_file_path, level=logging.INFO, + format='%(asctime)s - %(levelname)s - %(message)s') + self.logger = logging.getLogger() def load_settings(self, settings_path): if not os.path.exists(settings_path): raise FileNotFoundError(f"Settings file not found at {settings_path}") - with open(settings_path, "r") as f: + with open(settings_path, 'r') as f: return json.load(f) - def enable_http_debug(self): - """Enable HTTP-level logging for API communication.""" - http.client.HTTPConnection.debuglevel = 1 - logging.getLogger("http.client").setLevel(logging.DEBUG) - logging.getLogger("http.client").propagate = True - def info(self, message): + print(message) self.logger.info(message) def error(self, message): + print(message) self.logger.error(message) - print(f"ERROR: {message}")