Logger that handle titles in different colors. Outputs logs in console, activity.log, and errors.log For console handler: simulates typing
| 30 | |
| 31 | |
| 32 | class Logger(metaclass=Singleton): |
| 33 | """ |
| 34 | Logger that handle titles in different colors. |
| 35 | Outputs logs in console, activity.log, and errors.log |
| 36 | For console handler: simulates typing |
| 37 | """ |
| 38 | |
| 39 | def __init__(self): |
| 40 | # create log directory if it doesn't exist |
| 41 | this_files_dir_path = os.path.dirname(__file__) |
| 42 | log_dir = os.path.join(this_files_dir_path, "../logs") |
| 43 | if not os.path.exists(log_dir): |
| 44 | os.makedirs(log_dir) |
| 45 | |
| 46 | log_file = "activity.log" |
| 47 | error_file = "error.log" |
| 48 | |
| 49 | console_formatter = AutoGptFormatter("%(title_color)s %(message)s") |
| 50 | |
| 51 | # Create a handler for console which simulate typing |
| 52 | self.typing_console_handler = TypingConsoleHandler() |
| 53 | self.typing_console_handler.setLevel(logging.INFO) |
| 54 | self.typing_console_handler.setFormatter(console_formatter) |
| 55 | |
| 56 | # Create a handler for console without typing simulation |
| 57 | self.console_handler = ConsoleHandler() |
| 58 | self.console_handler.setLevel(logging.DEBUG) |
| 59 | self.console_handler.setFormatter(console_formatter) |
| 60 | |
| 61 | # Info handler in activity.log |
| 62 | self.file_handler = logging.FileHandler( |
| 63 | os.path.join(log_dir, log_file), "a", "utf-8" |
| 64 | ) |
| 65 | self.file_handler.setLevel(logging.DEBUG) |
| 66 | info_formatter = AutoGptFormatter( |
| 67 | "%(asctime)s %(levelname)s %(title)s %(message_no_color)s" |
| 68 | ) |
| 69 | self.file_handler.setFormatter(info_formatter) |
| 70 | |
| 71 | # Error handler error.log |
| 72 | error_handler = logging.FileHandler( |
| 73 | os.path.join(log_dir, error_file), "a", "utf-8" |
| 74 | ) |
| 75 | error_handler.setLevel(logging.ERROR) |
| 76 | error_formatter = AutoGptFormatter( |
| 77 | "%(asctime)s %(levelname)s %(module)s:%(funcName)s:%(lineno)d %(title)s" |
| 78 | " %(message_no_color)s" |
| 79 | ) |
| 80 | error_handler.setFormatter(error_formatter) |
| 81 | |
| 82 | self.typing_logger = logging.getLogger("TYPER") |
| 83 | self.typing_logger.addHandler(self.typing_console_handler) |
| 84 | self.typing_logger.addHandler(self.file_handler) |
| 85 | self.typing_logger.addHandler(error_handler) |
| 86 | self.typing_logger.setLevel(logging.DEBUG) |
| 87 | |
| 88 | self.logger = logging.getLogger("LOGGER") |
| 89 | self.logger.addHandler(self.console_handler) |