refactor(logging): move CustomFormatter to agent_logger and update log handlers
Browse files- app.py +5 -29
- utils/agent_logger.py +32 -2
app.py
CHANGED
|
@@ -51,30 +51,6 @@ LOCAL_LOG_DIR = "./hf_inference_logs"
|
|
| 51 |
HF_DATASET_NAME="aiwithoutborders-xyz/degentic_rd0"
|
| 52 |
|
| 53 |
|
| 54 |
-
class CustomFormatter(logging.Formatter):
|
| 55 |
-
|
| 56 |
-
green = "\x1b[32;20m"
|
| 57 |
-
blue = "\x1b[34;20m"
|
| 58 |
-
yellow = "\x1b[33;20m"
|
| 59 |
-
red = "\x1b[31;20m"
|
| 60 |
-
bold_red = "\x1b[31;1m"
|
| 61 |
-
reset = "\x1b[0m"
|
| 62 |
-
format = "%(asctime)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
|
| 63 |
-
|
| 64 |
-
FORMATS = {
|
| 65 |
-
logging.DEBUG: blue + format + reset,
|
| 66 |
-
logging.INFO: green + format + reset,
|
| 67 |
-
logging.WARNING: yellow + format + reset,
|
| 68 |
-
logging.ERROR: red + format + reset,
|
| 69 |
-
logging.CRITICAL: bold_red + format + reset,
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
def format(self, record):
|
| 73 |
-
log_fmt = self.FORMATS.get(record.levelno)
|
| 74 |
-
formatter = logging.Formatter(log_fmt)
|
| 75 |
-
return formatter.format(record)
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
# Custom JSON Encoder to handle numpy types
|
| 80 |
class NumpyEncoder(json.JSONEncoder):
|
|
@@ -471,12 +447,12 @@ with gr.Blocks() as detection_model_eval_playground:
|
|
| 471 |
with gr.Accordion("Agent Logs", open=False, elem_id="agent-logs-accordion"):
|
| 472 |
with gr.Row():
|
| 473 |
with gr.Column():
|
| 474 |
-
context_intelligence_log = Log(label="Context Log", dark=True, xterm_font_size=12, log_file=AGENT_LOG_FILES["context_intelligence"])
|
| 475 |
-
ensemble_monitor_log = Log(label="Ensemble Monitor Log", dark=True, xterm_font_size=12, log_file=AGENT_LOG_FILES["ensemble_monitor"])
|
| 476 |
with gr.Column():
|
| 477 |
-
weight_optimization_log = Log(label="Weight Optimization Log", dark=True, xterm_font_size=12, log_file=AGENT_LOG_FILES["weight_optimization"])
|
| 478 |
-
forensic_log = Log(label="Forensic Anomaly Log", dark=True, xterm_font_size=12, log_file=AGENT_LOG_FILES["forensic_anomaly_detection"])
|
| 479 |
-
system_health_log = Log(label="System Health Log", dark=True, xterm_font_size=12, log_file=AGENT_LOG_FILES["system_health"], visible=False)
|
| 480 |
|
| 481 |
predict_btn.click(
|
| 482 |
full_prediction,
|
|
|
|
| 51 |
HF_DATASET_NAME="aiwithoutborders-xyz/degentic_rd0"
|
| 52 |
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# Custom JSON Encoder to handle numpy types
|
| 56 |
class NumpyEncoder(json.JSONEncoder):
|
|
|
|
| 447 |
with gr.Accordion("Agent Logs", open=False, elem_id="agent-logs-accordion"):
|
| 448 |
with gr.Row():
|
| 449 |
with gr.Column():
|
| 450 |
+
context_intelligence_log = Log(label="Context Log", dark=True, xterm_font_size=12, log_file=AGENT_LOG_FILES["context_intelligence"], container=False, tail=40)
|
| 451 |
+
ensemble_monitor_log = Log(label="Ensemble Monitor Log", dark=True, xterm_font_size=12, log_file=AGENT_LOG_FILES["ensemble_monitor"], tail=40)
|
| 452 |
with gr.Column():
|
| 453 |
+
weight_optimization_log = Log(label="Weight Optimization Log", dark=True, xterm_font_size=12, log_file=AGENT_LOG_FILES["weight_optimization"], tail=40)
|
| 454 |
+
forensic_log = Log(label="Forensic Anomaly Log", dark=True, xterm_font_size=12, log_file=AGENT_LOG_FILES["forensic_anomaly_detection"], tail=40)
|
| 455 |
+
system_health_log = Log(label="System Health Log", dark=True, xterm_font_size=12, log_file=AGENT_LOG_FILES["system_health"], visible=False, tail=40)
|
| 456 |
|
| 457 |
predict_btn.click(
|
| 458 |
full_prediction,
|
utils/agent_logger.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import logging
|
| 2 |
from pathlib import Path
|
|
|
|
| 3 |
|
| 4 |
AGENT_LOG_FILES = {
|
| 5 |
"ensemble_monitor": "./agent_logs/ensemble_monitor.log",
|
|
@@ -9,6 +10,30 @@ AGENT_LOG_FILES = {
|
|
| 9 |
"forensic_anomaly_detection": "./agent_logs/forensic_anomaly_detection.log",
|
| 10 |
}
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
class AgentLogger:
|
| 13 |
def __init__(self, agent_log_files=None):
|
| 14 |
self.agent_log_files = agent_log_files or AGENT_LOG_FILES
|
|
@@ -16,6 +41,12 @@ class AgentLogger:
|
|
| 16 |
self._setup_loggers()
|
| 17 |
|
| 18 |
def _setup_loggers(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
for agent, log_file in self.agent_log_files.items():
|
| 20 |
Path(log_file).parent.mkdir(parents=True, exist_ok=True)
|
| 21 |
Path(log_file).touch(exist_ok=True)
|
|
@@ -26,8 +57,7 @@ class AgentLogger:
|
|
| 26 |
logger.removeHandler(handler)
|
| 27 |
handler = logging.FileHandler(log_file)
|
| 28 |
handler.setLevel(logging.DEBUG)
|
| 29 |
-
|
| 30 |
-
handler.setFormatter(formatter)
|
| 31 |
logger.addHandler(handler)
|
| 32 |
self.loggers[agent] = logger
|
| 33 |
|
|
|
|
| 1 |
import logging
|
| 2 |
from pathlib import Path
|
| 3 |
+
from gradio_log import Log
|
| 4 |
|
| 5 |
AGENT_LOG_FILES = {
|
| 6 |
"ensemble_monitor": "./agent_logs/ensemble_monitor.log",
|
|
|
|
| 10 |
"forensic_anomaly_detection": "./agent_logs/forensic_anomaly_detection.log",
|
| 11 |
}
|
| 12 |
|
| 13 |
+
class CustomFormatter(logging.Formatter):
|
| 14 |
+
|
| 15 |
+
green = "\x1b[32;20m"
|
| 16 |
+
blue = "\x1b[34;20m"
|
| 17 |
+
yellow = "\x1b[33;20m"
|
| 18 |
+
red = "\x1b[31;20m"
|
| 19 |
+
bold_red = "\x1b[31;1m"
|
| 20 |
+
reset = "\x1b[0m"
|
| 21 |
+
format = "%(asctime)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
|
| 22 |
+
|
| 23 |
+
FORMATS = {
|
| 24 |
+
logging.DEBUG: blue + format + reset,
|
| 25 |
+
logging.INFO: green + format + reset,
|
| 26 |
+
logging.WARNING: yellow + format + reset,
|
| 27 |
+
logging.ERROR: red + format + reset,
|
| 28 |
+
logging.CRITICAL: bold_red + format + reset,
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
def format(self, record):
|
| 32 |
+
log_fmt = self.FORMATS.get(record.levelno)
|
| 33 |
+
formatter = logging.Formatter(log_fmt)
|
| 34 |
+
return formatter.format(record)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
class AgentLogger:
|
| 38 |
def __init__(self, agent_log_files=None):
|
| 39 |
self.agent_log_files = agent_log_files or AGENT_LOG_FILES
|
|
|
|
| 41 |
self._setup_loggers()
|
| 42 |
|
| 43 |
def _setup_loggers(self):
|
| 44 |
+
# Import CustomFormatter from app.py or define it here if needed
|
| 45 |
+
try:
|
| 46 |
+
custom_formatter = CustomFormatter()
|
| 47 |
+
except ImportError:
|
| 48 |
+
# Fallback: use default formatter if CustomFormatter is not available
|
| 49 |
+
custom_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)")
|
| 50 |
for agent, log_file in self.agent_log_files.items():
|
| 51 |
Path(log_file).parent.mkdir(parents=True, exist_ok=True)
|
| 52 |
Path(log_file).touch(exist_ok=True)
|
|
|
|
| 57 |
logger.removeHandler(handler)
|
| 58 |
handler = logging.FileHandler(log_file)
|
| 59 |
handler.setLevel(logging.DEBUG)
|
| 60 |
+
handler.setFormatter(custom_formatter)
|
|
|
|
| 61 |
logger.addHandler(handler)
|
| 62 |
self.loggers[agent] = logger
|
| 63 |
|