From d03018de9bd6dfa6c2aa6fa10c3b818c62832562 Mon Sep 17 00:00:00 2001 From: Spencer Date: Thu, 21 Aug 2025 12:18:08 -0500 Subject: [PATCH] feat: Log LLM responses to console - Add a StreamHandler to the logger to output all logs to the console. - Log the LLM response to the console for real-time monitoring. - Update PROGRESS.md to reflect the completion of the task. - Fix a syntax error in monitor_agent.py. --- PROGRESS.md | 2 +- monitor_agent.py | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/PROGRESS.md b/PROGRESS.md index 068fc05..8723ea4 100644 --- a/PROGRESS.md +++ b/PROGRESS.md @@ -70,7 +70,7 @@ - [x] Change baseline calculations to only use integers instead of floats. - [x] Add a log file that only keeps records for the past 24 hours. -- [ ] Log all LLM responses to the console. +- [x] Log all LLM responses to the console. - [ ] Reduce alerts to only happen between 9am and 12am. - [ ] Get hostnames of devices in Nmap scan. - [ ] Filter out RTT fluctuations below 10 seconds. diff --git a/monitor_agent.py b/monitor_agent.py index f99a6e6..77670d4 100644 --- a/monitor_agent.py +++ b/monitor_agent.py @@ -26,10 +26,15 @@ logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) # Create a handler that rotates logs daily, keeping 1 backup -handler = TimedRotatingFileHandler(LOG_FILE, when="midnight", interval=1, backupCount=1) -handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) +file_handler = TimedRotatingFileHandler(LOG_FILE, when="midnight", interval=1, backupCount=1) +file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) -logger.addHandler(handler) +# Create a handler for console output +console_handler = logging.StreamHandler() +console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) + +logger.addHandler(file_handler) +logger.addHandler(console_handler) LOG_POSITION_FILE = 'log_position.txt' @@ -257,7 +262,9 @@ def analyze_data_with_llm(data, baselines): end_index = sanitized_response.rfind('}') if start_index != -1 and end_index != -1: json_string = sanitized_response[start_index:end_index+1] - return json.loads(json_string) + llm_response = json.loads(json_string) + logger.info(f"LLM Response: {llm_response}") + return llm_response else: # Handle cases where the response is not valid JSON logger.warning(f"LLM returned a non-JSON response: {sanitized_response}") @@ -379,4 +386,4 @@ def main(): time.sleep(300) # Run every 5 minutes if __name__ == "__main__": - main() \ No newline at end of file + main()