Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Make log level configurable; add TRACE level (#612)

Kilian Liereta795862c85cb
diff --git a/docs/config/env.md b/docs/config/env.md
index fb6630ff..f4e0cc80 100644
--- a/docs/config/env.md
+++ b/docs/config/env.md
@@ -9,7 +9,12 @@ This page details all environment variables that are currently in use by SWE-age
* All API keys (for LMs and GitHub) can be set as an environment variable. See [here](../installation/keys.md) for more information.
* `SWE_AGENT_CONFIG_ROOT`: Used to resolve relative paths in the [config](config.md)
* `SWE_AGENT_ENV_LONG_TIMEOUT` (default: 500): Timeout in seconds used for commands that install instance environment.
-* `SWE_AGENT_LOG_TIME`: Add timestamps to log (**can only be set as an environment variable**)
+
+The following three variables can only be set as environment variables, not in the config file
+
+* `SWE_AGENT_LOG_TIME`: Add timestamps to log
+* `SWE_AGENT_LOG_STREAM_LEVEL`: Level of logging that is shown on the command line interface (`TRACE` being a custom level below `DEBUG`)
+* `SWE_AGENT_LOG_FILE_LEVEL`: Like `SWE_AGENT_LOG_STREAM_LEVEL` but for the log file
!!! warning "Unstable"
The following variables might still be subject to change
diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.py
index 7a26692c..0ae91c42 100644
--- a/sweagent/environment/swe_env.py
+++ b/sweagent/environment/swe_env.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import datetime
import hashlib
import json
+import logging
import os
import random
import re
@@ -725,6 +726,7 @@ class SWEEnv(gym.Env):
output: output from container
"""
if input.strip() != "exit":
+ self.logger.log(logging.TRACE, "Input:\n%s", input) # type: ignore
output, valid = self._check_syntax(input)
if not valid:
return output # shows syntax errors
@@ -732,6 +734,7 @@ class SWEEnv(gym.Env):
input,
timeout_duration=timeout_duration,
)
+ self.logger.log(logging.TRACE, "Output:\n%s", output) # type: ignore
self.communicate_output = output
return output
else:
diff --git a/sweagent/utils/log.py b/sweagent/utils/log.py
index 5313d768..b7979d9e 100644
--- a/sweagent/utils/log.py
+++ b/sweagent/utils/log.py
@@ -9,6 +9,21 @@ from rich.logging import RichHandler
_SET_UP_LOGGERS = set()
_ADDITIONAL_HANDLERS = []
+logging.TRACE = 5 # type: ignore
+logging.addLevelName(logging.TRACE, "TRACE") # type: ignore
+
+
+def _interpret_level_from_env(level: str | None) -> int:
+ if not level:
+ return logging.DEBUG
+ if level.isnumeric():
+ return int(level)
+ return getattr(logging, level.upper(), logging.DEBUG)
+
+
+_STREAM_LEVEL = _interpret_level_from_env(os.environ.get("SWE_AGENT_LOG_STREAM_LEVEL"))
+_FILE_LEVEL = _interpret_level_from_env(os.environ.get("SWE_AGENT_LOG_FILE_LEVEL"))
+
def get_logger(name: str) -> logging.Logger:
"""Get logger. Use this instead of `logging.getLogger` to ensure
@@ -22,8 +37,8 @@ def get_logger(name: str) -> logging.Logger:
show_time=bool(os.environ.get("SWE_AGENT_LOG_TIME", False)),
show_path=False,
)
- handler.setLevel(logging.DEBUG)
- logger.setLevel(logging.DEBUG)
+ handler.setLevel(_STREAM_LEVEL)
+ logger.setLevel(min(_STREAM_LEVEL, _FILE_LEVEL))
logger.addHandler(handler)
logger.propagate = False
_SET_UP_LOGGERS.add(name)
@@ -39,7 +54,7 @@ def add_file_handler(path: PurePath | str) -> None:
handler = logging.FileHandler(path)
formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
handler.setFormatter(formatter)
- handler.setLevel(logging.DEBUG)
+ handler.setLevel(_FILE_LEVEL)
for name in _SET_UP_LOGGERS:
logger = logging.getLogger(name)
logger.addHandler(handler)