AutoGPT: Remove color codes in file log output

This commit is contained in:
Reinier van der Leer
2023-09-27 20:50:18 -04:00
parent d466689c94
commit 5acb5ad9b7
2 changed files with 15 additions and 4 deletions

View File

@@ -57,18 +57,22 @@ def configure_logging(config: Config, log_dir: Path = LOG_DIR) -> None:
# INFO log file handler
activity_log_handler = logging.FileHandler(log_dir / LOG_FILE, "a", "utf-8")
activity_log_handler.setLevel(logging.INFO)
activity_log_handler.setFormatter(AutoGptFormatter(SIMPLE_LOG_FORMAT))
activity_log_handler.setFormatter(
AutoGptFormatter(SIMPLE_LOG_FORMAT, no_color=True)
)
if config.debug_mode:
# DEBUG log file handler
debug_log_handler = logging.FileHandler(log_dir / DEBUG_LOG_FILE, "a", "utf-8")
debug_log_handler.setLevel(logging.DEBUG)
debug_log_handler.setFormatter(AutoGptFormatter(DEBUG_LOG_FORMAT))
debug_log_handler.setFormatter(
AutoGptFormatter(DEBUG_LOG_FORMAT, no_color=True)
)
# ERROR log file handler
error_log_handler = logging.FileHandler(log_dir / ERROR_LOG_FILE, "a", "utf-8")
error_log_handler.setLevel(logging.ERROR)
error_log_handler.setFormatter(AutoGptFormatter(DEBUG_LOG_FORMAT))
error_log_handler.setFormatter(AutoGptFormatter(DEBUG_LOG_FORMAT, no_color=True))
# Configure the root logger
logging.basicConfig(

View File

@@ -8,6 +8,10 @@ from .utils import remove_color_codes
class AutoGptFormatter(FancyConsoleFormatter):
def __init__(self, *args, no_color: bool = False, **kwargs):
super().__init__(*args, **kwargs)
self.no_color = no_color
def format(self, record: logging.LogRecord) -> str:
# Make sure `msg` is a string
if not hasattr(record, "msg"):
@@ -29,4 +33,7 @@ class AutoGptFormatter(FancyConsoleFormatter):
# Make sure record.title is set, and padded with a space if not empty
record.title = f"{title} " if title else ""
return super().format(record)
if self.no_color:
return remove_color_codes(super().format(record))
else:
return super().format(record)