feat: Implement logging functionality; add logger configuration and retrieval
This commit is contained in:
parent
52c455b20c
commit
670066100f
@ -52,7 +52,6 @@ def reprompt(nextsteps: str, processor) -> None:
|
|||||||
scr = screenshot_to_base64(take_screenshot())
|
scr = screenshot_to_base64(take_screenshot())
|
||||||
return processor.process(nextsteps, img_data=scr)
|
return processor.process(nextsteps, img_data=scr)
|
||||||
|
|
||||||
|
|
||||||
def _execute(name, args, processor):
|
def _execute(name, args, processor):
|
||||||
if name == "click_button":
|
if name == "click_button":
|
||||||
press_mouse(MouseInput(**args))
|
press_mouse(MouseInput(**args))
|
||||||
|
@ -4,6 +4,10 @@ import openai
|
|||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
from objects import aic
|
from objects import aic
|
||||||
import ai.compute
|
import ai.compute
|
||||||
|
from objects import logger as logger_module
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger: logging.Logger = logger_module.get_logger(__name__)
|
||||||
|
|
||||||
class AIProcessor:
|
class AIProcessor:
|
||||||
def __init__(self, api_key: str, model: str = "gpt-4.1"):
|
def __init__(self, api_key: str, model: str = "gpt-4.1"):
|
||||||
@ -62,6 +66,12 @@ class AIProcessor:
|
|||||||
processor=self,
|
processor=self,
|
||||||
)
|
)
|
||||||
outputs.append(r) if r else None
|
outputs.append(r) if r else None
|
||||||
|
# Make sure the two last messages from user has an image, but set disable_image to True for the others
|
||||||
|
for msg in self.session.messages[-2:]:
|
||||||
|
msg.disable_image = True
|
||||||
|
logger.debug(
|
||||||
|
self.session.messages
|
||||||
|
)
|
||||||
if reexec:
|
if reexec:
|
||||||
img = ai.compute.screenshot_to_base64(
|
img = ai.compute.screenshot_to_base64(
|
||||||
ai.compute.take_screenshot()
|
ai.compute.take_screenshot()
|
||||||
@ -86,15 +96,6 @@ class AIProcessor:
|
|||||||
aic.Message(role="assistant", content=output_text)
|
aic.Message(role="assistant", content=output_text)
|
||||||
)
|
)
|
||||||
|
|
||||||
# re-execute if needed
|
|
||||||
if reexec:
|
|
||||||
img = ai.compute.screenshot_to_base64(
|
|
||||||
ai.compute.take_screenshot()
|
|
||||||
)
|
|
||||||
|
|
||||||
outputs.append(
|
|
||||||
*self.process(nextsteps, img)
|
|
||||||
)
|
|
||||||
return outputs
|
return outputs
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
42
objects/logger.py
Normal file
42
objects/logger.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import logging
|
||||||
|
import os
|
||||||
|
from logging.handlers import RotatingFileHandler
|
||||||
|
|
||||||
|
# Configuration values
|
||||||
|
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
|
||||||
|
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
||||||
|
LOG_DIR = os.getenv("LOG_DIR", os.path.join(os.getcwd(), "logs"))
|
||||||
|
|
||||||
|
# Ensure log directory exists
|
||||||
|
os.makedirs(LOG_DIR, exist_ok=True)
|
||||||
|
|
||||||
|
# Log file path
|
||||||
|
LOG_FILE = os.path.join(LOG_DIR, "app.log")
|
||||||
|
|
||||||
|
# Create root logger
|
||||||
|
logger = logging.getLogger("gpt-agent")
|
||||||
|
logger.setLevel(LOG_LEVEL)
|
||||||
|
|
||||||
|
# Formatter
|
||||||
|
formatter = logging.Formatter(LOG_FORMAT)
|
||||||
|
|
||||||
|
# Console handler
|
||||||
|
console_handler = logging.StreamHandler()
|
||||||
|
console_handler.setLevel(LOG_LEVEL)
|
||||||
|
console_handler.setFormatter(formatter)
|
||||||
|
logger.addHandler(console_handler)
|
||||||
|
|
||||||
|
# Rotating file handler
|
||||||
|
file_handler = RotatingFileHandler(LOG_FILE, maxBytes=5*1024*1024, backupCount=5)
|
||||||
|
file_handler.setLevel(LOG_LEVEL)
|
||||||
|
file_handler.setFormatter(formatter)
|
||||||
|
logger.addHandler(file_handler)
|
||||||
|
|
||||||
|
def get_logger(name: str | None = None) -> logging.Logger:
|
||||||
|
"""
|
||||||
|
Retrieve a configured logger instance. If name is provided,
|
||||||
|
returns a child logger of the configured root logger.
|
||||||
|
"""
|
||||||
|
if name:
|
||||||
|
return logger.getChild(name)
|
||||||
|
return logger
|
Loading…
Reference in New Issue
Block a user