aboutsummaryrefslogtreecommitdiff
path: root/autogpts/autogpt/autogpt/core/runner/client_lib/logging/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'autogpts/autogpt/autogpt/core/runner/client_lib/logging/config.py')
-rw-r--r--autogpts/autogpt/autogpt/core/runner/client_lib/logging/config.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/autogpts/autogpt/autogpt/core/runner/client_lib/logging/config.py b/autogpts/autogpt/autogpt/core/runner/client_lib/logging/config.py
new file mode 100644
index 000000000..56f79f5fe
--- /dev/null
+++ b/autogpts/autogpt/autogpt/core/runner/client_lib/logging/config.py
@@ -0,0 +1,82 @@
+import logging
+import sys
+
+from colorama import Fore, Style
+from openai._base_client import log as openai_logger
+
+SIMPLE_LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
+DEBUG_LOG_FORMAT = (
+ "%(asctime)s.%(msecs)03d %(levelname)s %(filename)s:%(lineno)d %(message)s"
+)
+
+
+def configure_root_logger():
+ console_formatter = FancyConsoleFormatter(SIMPLE_LOG_FORMAT)
+
+ stdout = logging.StreamHandler(stream=sys.stdout)
+ stdout.setLevel(logging.DEBUG)
+ stdout.addFilter(BelowLevelFilter(logging.WARNING))
+ stdout.setFormatter(console_formatter)
+ stderr = logging.StreamHandler()
+ stderr.setLevel(logging.WARNING)
+ stderr.setFormatter(console_formatter)
+
+ logging.basicConfig(level=logging.DEBUG, handlers=[stdout, stderr])
+
+ # Disable debug logging from OpenAI library
+ openai_logger.setLevel(logging.WARNING)
+
+
+class FancyConsoleFormatter(logging.Formatter):
+ """
+ A custom logging formatter designed for console output.
+
+ This formatter enhances the standard logging output with color coding. The color
+ coding is based on the level of the log message, making it easier to distinguish
+ between different types of messages in the console output.
+
+ The color for each level is defined in the LEVEL_COLOR_MAP class attribute.
+ """
+
+ # level -> (level & text color, title color)
+ LEVEL_COLOR_MAP = {
+ logging.DEBUG: Fore.LIGHTBLACK_EX,
+ logging.INFO: Fore.BLUE,
+ logging.WARNING: Fore.YELLOW,
+ logging.ERROR: Fore.RED,
+ logging.CRITICAL: Fore.RED + Style.BRIGHT,
+ }
+
+ def format(self, record: logging.LogRecord) -> str:
+ # Make sure `msg` is a string
+ if not hasattr(record, "msg"):
+ record.msg = ""
+ elif not type(record.msg) is str:
+ record.msg = str(record.msg)
+
+ # Determine default color based on error level
+ level_color = ""
+ if record.levelno in self.LEVEL_COLOR_MAP:
+ level_color = self.LEVEL_COLOR_MAP[record.levelno]
+ record.levelname = f"{level_color}{record.levelname}{Style.RESET_ALL}"
+
+ # Determine color for message
+ color = getattr(record, "color", level_color)
+ color_is_specified = hasattr(record, "color")
+
+ # Don't color INFO messages unless the color is explicitly specified.
+ if color and (record.levelno != logging.INFO or color_is_specified):
+ record.msg = f"{color}{record.msg}{Style.RESET_ALL}"
+
+ return super().format(record)
+
+
+class BelowLevelFilter(logging.Filter):
+ """Filter for logging levels below a certain threshold."""
+
+ def __init__(self, below_level: int):
+ super().__init__()
+ self.below_level = below_level
+
+ def filter(self, record: logging.LogRecord):
+ return record.levelno < self.below_level