From cf00c33f907b12e48218404f849c11beb568001b Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Mon, 22 Apr 2024 17:14:22 +0200 Subject: fix(agent): Fix debug logging & amend `configure_logging` for easier use --- autogpts/autogpt/agbenchmark_config/benchmarks.py | 11 +++-- autogpts/autogpt/autogpt/app/configurator.py | 22 --------- autogpts/autogpt/autogpt/app/main.py | 21 ++++---- autogpts/autogpt/autogpt/config/config.py | 2 - autogpts/autogpt/autogpt/logs/config.py | 58 +++++++++++++++++------ autogpts/autogpt/tests/conftest.py | 8 ++-- 6 files changed, 65 insertions(+), 57 deletions(-) diff --git a/autogpts/autogpt/agbenchmark_config/benchmarks.py b/autogpts/autogpt/agbenchmark_config/benchmarks.py index ab809d86b..61b16984f 100644 --- a/autogpts/autogpt/agbenchmark_config/benchmarks.py +++ b/autogpts/autogpt/agbenchmark_config/benchmarks.py @@ -21,12 +21,13 @@ def run_specific_agent(task: str, continuous_mode: bool = False) -> None: def bootstrap_agent(task: str, continuous_mode: bool) -> Agent: - config = ConfigBuilder.build_config_from_env() - config.logging.level = logging.DEBUG - config.logging.log_dir = LOG_DIR - config.logging.plain_console_output = True - configure_logging(**config.logging.dict()) + configure_logging( + level=logging.DEBUG, + log_dir=LOG_DIR, + plain_console_output=True, + ) + config = ConfigBuilder.build_config_from_env() config.continuous_mode = continuous_mode config.continuous_limit = 20 config.noninteractive_mode = True diff --git a/autogpts/autogpt/autogpt/app/configurator.py b/autogpts/autogpt/autogpt/app/configurator.py index cd7890021..b22554d33 100644 --- a/autogpts/autogpt/autogpt/app/configurator.py +++ b/autogpts/autogpt/autogpt/app/configurator.py @@ -12,7 +12,6 @@ from autogpt import utils from autogpt.config import Config from autogpt.config.config import GPT_3_MODEL, GPT_4_MODEL from autogpt.llm.api_manager import ApiManager -from autogpt.logs.config import LogFormatName from autogpt.logs.helpers import request_user_double_check from autogpt.memory.vector import get_supported_memory_backends @@ -29,11 +28,6 @@ def apply_overrides_to_config( ai_settings_file: Optional[Path] = None, prompt_settings_file: Optional[Path] = None, skip_reprompt: bool = False, - speak: bool = False, - debug: bool = False, - log_level: Optional[str] = None, - log_format: Optional[str] = None, - log_file_format: Optional[str] = None, gpt3only: bool = False, gpt4only: bool = False, memory_type: Optional[str] = None, @@ -63,19 +57,6 @@ def apply_overrides_to_config( skips_news (bool): Whether to suppress the output of latest news on startup. """ config.continuous_mode = False - config.tts_config.speak_mode = False - - # Set log level - if debug: - config.logging.level = logging.DEBUG - elif log_level and type(_level := logging.getLevelName(log_level.upper())) is int: - config.logging.level = _level - - # Set log format - if log_format and log_format in LogFormatName._value2member_map_: - config.logging.log_format = LogFormatName(log_format) - if log_file_format and log_file_format in LogFormatName._value2member_map_: - config.logging.log_file_format = LogFormatName(log_file_format) if continuous: logger.warning( @@ -92,9 +73,6 @@ def apply_overrides_to_config( if continuous_limit and not continuous: raise click.UsageError("--continuous-limit can only be used with --continuous") - if speak: - config.tts_config.speak_mode = True - # Set the default LLM models if gpt3only: # --gpt3only should always use gpt-3.5-turbo, despite user's FAST_LLM config diff --git a/autogpts/autogpt/autogpt/app/main.py b/autogpts/autogpt/autogpt/app/main.py index 6c428ca6e..4a2c6927a 100644 --- a/autogpts/autogpt/autogpt/app/main.py +++ b/autogpts/autogpt/autogpt/app/main.py @@ -95,8 +95,13 @@ async def run_auto_gpt( file_storage.initialize() # Set up logging module + if speak: + config.tts_config.speak_mode = True configure_logging( - **config.logging.dict(), + debug=debug, + level=log_level, + log_format=log_format, + log_file_format=log_file_format, tts_config=config.tts_config, ) @@ -110,11 +115,6 @@ async def run_auto_gpt( ai_settings_file=ai_settings, prompt_settings_file=prompt_settings, skip_reprompt=skip_reprompt, - speak=speak, - debug=debug, - log_level=log_level, - log_format=log_format, - log_file_format=log_file_format, gpt3only=gpt3only, gpt4only=gpt4only, browser_name=browser_name, @@ -380,7 +380,10 @@ async def run_auto_gpt_server( # Set up logging module configure_logging( - **config.logging.dict(), + debug=debug, + level=log_level, + log_format=log_format, + log_file_format=log_file_format, tts_config=config.tts_config, ) @@ -390,10 +393,6 @@ async def run_auto_gpt_server( apply_overrides_to_config( config=config, prompt_settings_file=prompt_settings, - debug=debug, - log_level=log_level, - log_format=log_format, - log_file_format=log_file_format, gpt3only=gpt3only, gpt4only=gpt4only, browser_name=browser_name, diff --git a/autogpts/autogpt/autogpt/config/config.py b/autogpts/autogpt/autogpt/config/config.py index 040345f08..127719b72 100644 --- a/autogpts/autogpt/autogpt/config/config.py +++ b/autogpts/autogpt/autogpt/config/config.py @@ -23,7 +23,6 @@ from autogpt.core.resource.model_providers.openai import ( OpenAICredentials, ) from autogpt.file_storage import FileStorageBackendName -from autogpt.logs.config import LoggingConfig from autogpt.plugins.plugins_config import PluginsConfig from autogpt.speech import TTSConfig @@ -59,7 +58,6 @@ class Config(SystemSettings, arbitrary_types_allowed=True): # TTS configuration tts_config: TTSConfig = TTSConfig() - logging: LoggingConfig = LoggingConfig() # File storage file_storage_backend: FileStorageBackendName = UserConfigurable( diff --git a/autogpts/autogpt/autogpt/logs/config.py b/autogpts/autogpt/autogpt/logs/config.py index 437f68a8a..4a849736a 100644 --- a/autogpts/autogpt/autogpt/logs/config.py +++ b/autogpts/autogpt/autogpt/logs/config.py @@ -75,28 +75,58 @@ class LoggingConfig(SystemConfiguration): def configure_logging( - level: int = logging.INFO, - log_dir: Path = LOG_DIR, - log_format: Optional[LogFormatName] = None, - log_file_format: Optional[LogFormatName] = None, - plain_console_output: bool = False, + debug: bool = False, + level: Optional[int | str] = None, + log_dir: Optional[Path] = None, + log_format: Optional[LogFormatName | str] = None, + log_file_format: Optional[LogFormatName | str] = None, + plain_console_output: Optional[bool] = None, tts_config: Optional[TTSConfig] = None, ) -> None: - """Configure the native logging module. + """Configure the native logging module, based on the environment config and any + specified overrides. + + Arguments override values specified in the environment. Should be usable as `configure_logging(**config.logging.dict())`, where `config.logging` is a `LoggingConfig` object. """ - - # Auto-adjust default log format based on log level - log_format = log_format or ( - LogFormatName.SIMPLE if level != logging.DEBUG else LogFormatName.DEBUG + if debug and level: + raise ValueError("Only one of either 'debug' and 'level' arguments may be set") + + # Parse arguments + if isinstance(level, str): + if type(_level := logging.getLevelName(level.upper())) is int: + level = _level + else: + raise ValueError(f"Unknown log level '{level}'") + if isinstance(log_format, str): + if log_format in LogFormatName._value2member_map_: + log_format = LogFormatName(log_format) + elif not isinstance(log_format, LogFormatName): + raise ValueError(f"Unknown log format '{log_format}'") + if isinstance(log_file_format, str): + if log_file_format in LogFormatName._value2member_map_: + log_file_format = LogFormatName(log_file_format) + elif not isinstance(log_file_format, LogFormatName): + raise ValueError(f"Unknown log format '{log_format}'") + + config = LoggingConfig.from_env() + + # Aggregate arguments + env config + level = logging.DEBUG if debug else level or config.level + log_dir = log_dir or config.log_dir + log_format = log_format or (LogFormatName.DEBUG if debug else config.log_format) + log_file_format = log_file_format or log_format or config.log_file_format + plain_console_output = ( + plain_console_output + if plain_console_output is not None + else config.plain_console_output ) - log_file_format = log_file_format or log_format - - structured_logging = log_format == LogFormatName.STRUCTURED - if structured_logging: + # Structured logging is used for cloud environments, + # where logging to a file makes no sense. + if log_format == LogFormatName.STRUCTURED: plain_console_output = True log_file_format = None diff --git a/autogpts/autogpt/tests/conftest.py b/autogpts/autogpt/tests/conftest.py index 294f816f1..48c1a5ae9 100644 --- a/autogpts/autogpt/tests/conftest.py +++ b/autogpts/autogpt/tests/conftest.py @@ -80,8 +80,6 @@ def config( config.plugins_dir = "tests/unit/data/test_plugins" config.plugins_config_file = temp_plugins_config_file - config.logging.log_dir = Path(__file__).parent / "logs" - config.logging.plain_console_output = True config.noninteractive_mode = True # avoid circular dependency @@ -97,7 +95,11 @@ def config( @pytest.fixture(scope="session") def setup_logger(config: Config): - configure_logging(**config.logging.dict()) + configure_logging( + debug=True, + log_dir=Path(__file__).parent / "logs", + plain_console_output=True, + ) @pytest.fixture() -- cgit v1.2.3