Rewrite func. set statuses

This commit is contained in:
Kiryl
2021-12-01 14:19:57 +03:00
parent 2e5f666777
commit a5f2a47edf
4 changed files with 28 additions and 30 deletions

View File

@@ -23,6 +23,7 @@ class ColoredFormatter(logging.Formatter):
.format(self.PREFIX, seq, record.levelname, self.SUFFIX)
return logging.Formatter.format(self, record)
class BookLogger:
def __init__(self, name, book_id, main_logger=None,
filemode='w+', logging_level=logging.INFO):
@@ -38,12 +39,14 @@ class BookLogger:
self.main_logger = main_logger
self.logger = logging.getLogger(name)
folder_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
folder_path = os.path.dirname(
os.path.dirname(os.path.abspath(__file__)))
folder_path = os.path.dirname(folder_path)
filename = f'logs/{book_id}.log'
file_path = os.path.join(folder_path, filename)
file_handler = logging.FileHandler(file_path, mode=filemode)
file_format = ColoredFormatter('%(asctime)s - %(levelname)s - %(message)s [%(filename)s:%(lineno)d in %(funcName)s]')
file_format = ColoredFormatter(
'%(asctime)s - %(levelname)s - %(message)s [%(filename)s:%(lineno)d in %(funcName)s]')
file_handler.setFormatter(file_format)
self.logger.addHandler(file_handler)
self.logger.setLevel(logging_level)
@@ -73,32 +76,29 @@ class BookStatusWrapper:
self.logger_object = logger_object
self.book_id = book_id
def set_processing(self):
def set_status(self, status: str):
str_2_status = {
'[PROCESS]': self.access.PROCESS,
'[GENERATE]': self.access.GENERATE,
'[ERROR]': self.access.ERROR
}
try:
if self.access:
self.access.update_status(self.book_id, self.access.PROCESS)
self.logger_object.log(f'Status has been updated to [PROCESS].')
self.access.update_status(self.book_id, str_2_status[status])
self.logger_object.log(f'Status has been updated to {status}.')
except Exception as exc:
self.logger_object.log("Can't update status of the book [PROCESS].", logging.ERROR)
self.logger_object.log(
f"Can't update status of the book {status}.", logging.ERROR)
self.logger_object.log_error_to_main_log()
raise exc
def set_processing(self):
self.set_status('[PROCESS]')
def set_generating(self):
try:
if self.access:
self.access.update_status(self.book_id, self.access.GENERATE)
self.logger_object.log(f'Status has been updated to [GENERATE].')
except Exception as exc:
self.logger_object.log("Can't update status of the book [GENERATE].", logging.ERROR)
self.logger_object.log_error_to_main_log()
raise exc
self.set_status('[GENERATE]')
def set_error(self):
try:
if self.access:
self.access.update_status(self.book_id, self.access.ERROR)
self.logger_object.log(f'Status has been updated to [ERROR].')
except Exception as exc:
self.logger_object.log("Can't update status of the book [ERROR].", logging.ERROR)
self.logger_object.log_error_to_main_log()
raise exc
self.set_status('[ERROR]')