Skip to content

Commit

Permalink
logging
Browse files Browse the repository at this point in the history
  • Loading branch information
siyangqiu committed Jul 30, 2024
1 parent adaf6c7 commit e591114
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
13 changes: 10 additions & 3 deletions agentops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,12 @@ def init(
(i.e. Crew determining when tasks are complete and ending the session)
Attributes:
"""
Client().unsuppress_logs()
if Client().is_initialized:
return logger.warning("AgentOps has already been initialized")

if tags is not None:
Client().add_pre_init_warning(
"The 'tags' parameter is deprecated. Use 'default_tags' instead"
)
logger.warning("The 'tags' parameter is deprecated. Use 'default_tags' instead")
if default_tags is None:
default_tags = tags

Expand Down Expand Up @@ -147,6 +146,8 @@ def start_session(
e.g. ["test_run"].
inherited_session_id: (str, optional): Set the session ID to inherit from another client
"""
Client().unsuppress_logs()

if not Client().is_initialized:
return logger.warning(
"AgentOps has not been initialized yet. Please call agentops.init() before starting a session"
Expand All @@ -169,6 +170,8 @@ def end_session(
end_state_reason (str, optional): The reason for ending the session.
video (str, optional): URL to a video recording of the session
"""
Client().unsuppress_logs()

if Client().is_multi_session:
return logger.warning(
"Could not end session - multiple sessions detected. You must use session.end_session() instead of agentops.end_session()"
Expand All @@ -193,6 +196,8 @@ def record(event: Union[Event, ErrorEvent]):
Args:
event (Event): The event to record.
"""
Client().unsuppress_logs()

if Client().is_multi_session:
return logger.warning(
"Could not record event - multiple sessions detected. You must use session.record() instead of agentops.record()"
Expand Down Expand Up @@ -294,6 +299,8 @@ def get_session(session_id: str):
Args:
session_id (str): the session id for the session to be retreived
"""
Client().unsuppress_logs()

return Client().get_session(session_id)


Expand Down
21 changes: 16 additions & 5 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
from .session import Session, active_sessions
from .host_env import get_host_env
from .log_config import logger, unsuppress_logs
from .log_config import logger
from .meta_client import MetaClient
from .config import Configuration
from .llm_tracker import LlmTracker
Expand Down Expand Up @@ -82,10 +82,7 @@ def configure(
)

def initialize(self) -> Union[Session, None]:
unsuppress_logs()

for message in Client()._pre_init_messages:
logger.warning(message)
self.unsuppress_logs()

if self._config.api_key is None:
return logger.error(
Expand Down Expand Up @@ -404,6 +401,20 @@ def get_session(self, session_id: str):
if session.session_id == session_id:
return session

def unsuppress_logs(self):
logging_level = os.getenv("AGENTOPS_LOGGING_LEVEL", "INFO")
log_levels = {
"CRITICAL": logging.CRITICAL,
"ERROR": logging.ERROR,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"DEBUG": logging.DEBUG,
}
logger.setLevel(log_levels.get(logging_level, "INFO"))

for message in self._pre_init_messages:
logger.warning(message)

def end_all_sessions(self):
for s in self._sessions:
s.end_session()
Expand Down
12 changes: 0 additions & 12 deletions agentops/log_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,3 @@ def format(self, record):
file_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)


def unsuppress_logs():
logging_level = os.getenv("AGENTOPS_LOGGING_LEVEL", "INFO")
log_levels = {
"CRITICAL": logging.CRITICAL,
"ERROR": logging.ERROR,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"DEBUG": logging.DEBUG,
}
logger.setLevel(log_levels.get(logging_level, "INFO"))

0 comments on commit e591114

Please sign in to comment.