mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-18 22:44:21 +01:00
* Consolidate all logging stuff into one module * Merge import statement for `logs` and `logs.log_cycle` --------- Co-authored-by: James Collins <collijk@uw.edu>
42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
import logging
|
|
import re
|
|
|
|
from colorama import Style
|
|
|
|
|
|
class AutoGptFormatter(logging.Formatter):
|
|
"""
|
|
Allows to handle custom placeholders 'title_color' and 'message_no_color'.
|
|
To use this formatter, make sure to pass 'color', 'title' as log extras.
|
|
"""
|
|
|
|
def format(self, record: logging.LogRecord) -> str:
|
|
if hasattr(record, "color"):
|
|
record.title_color = (
|
|
getattr(record, "color")
|
|
+ getattr(record, "title", "")
|
|
+ " "
|
|
+ Style.RESET_ALL
|
|
)
|
|
else:
|
|
record.title_color = getattr(record, "title", "")
|
|
|
|
# Add this line to set 'title' to an empty string if it doesn't exist
|
|
record.title = getattr(record, "title", "")
|
|
|
|
if hasattr(record, "msg"):
|
|
record.message_no_color = remove_color_codes(getattr(record, "msg"))
|
|
else:
|
|
record.message_no_color = ""
|
|
return super().format(record)
|
|
|
|
|
|
def remove_color_codes(s: str) -> str:
|
|
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
|
|
return ansi_escape.sub("", s)
|
|
|
|
|
|
class JsonFormatter(logging.Formatter):
|
|
def format(self, record: logging.LogRecord):
|
|
return record.msg
|