-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e43b118
commit 3eb3d75
Showing
1 changed file
with
14 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}") |