Improved service providing and app hosting

This commit is contained in:
2020-11-26 19:17:05 +01:00
parent c6d1dce577
commit 7d4efe7bda
9 changed files with 97 additions and 19 deletions

View File

@@ -3,7 +3,7 @@ import os
import traceback
from string import Template
from sh_edraft.hosting.base import ApplicationHostBase
from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase
from sh_edraft.logging.base.logger_base import LoggerBase
from sh_edraft.logging.model import LoggingSettings
from sh_edraft.logging.model.logging_level import LoggingLevel
@@ -13,16 +13,16 @@ from sh_edraft.utils.console import Console
class Logger(LoggerBase):
def __init__(self, logging_settings: LoggingSettings, time_format: TimeFormatSettings, app_host: ApplicationHostBase):
def __init__(self, logging_settings: LoggingSettings, time_format: TimeFormatSettings, app_runtime: ApplicationRuntimeBase):
LoggerBase.__init__(self)
self._app_runtime = app_runtime
self._log_settings: LoggingSettings = logging_settings
self._time_format_settings: TimeFormatSettings = time_format
self._app_host = app_host
self._log = Template(self._log_settings.filename).substitute(
date_time_now=self._app_host.date_time_now.strftime(self._time_format_settings.date_time_format),
start_time=self._app_host.start_time.strftime(self._time_format_settings.date_time_log_format)
date_time_now=self._app_runtime.date_time_now.strftime(self._time_format_settings.date_time_format),
start_time=self._app_runtime.start_time.strftime(self._time_format_settings.date_time_log_format)
)
self._path = self._log_settings.path
self._level = self._log_settings.level
@@ -47,7 +47,7 @@ class Logger(LoggerBase):
if not os.path.exists(self._path):
os.mkdir(self._path)
except Exception as e:
self.fatal(__name__, 'Cannot create log dir', ex=e)
self._fatal_console(__name__, 'Cannot create log dir', ex=e)
""" create new log file """
try:
@@ -57,16 +57,19 @@ class Logger(LoggerBase):
Console.write_line(f'[{__name__}]: Using log file: {path}')
f.close()
except Exception as e:
self.fatal(__name__, 'Cannot open log file', ex=e)
self._fatal_console(__name__, 'Cannot open log file', ex=e)
def _append_log(self, string):
try:
# open log file and append always
if not os.path.isdir(self._path):
self._fatal_console(__name__, 'Log directory not found')
with open(self._path + self._log, "a+", encoding="utf-8") as f:
f.write(string + '\n')
f.close()
except Exception as e:
self.error(__name__, f'Cannot append log file, message: {string}', ex=e)
self._fatal_console(__name__, f'Cannot append log file, message: {string}', ex=e)
def _get_string(self, name: str, level: LoggingLevel, message: str) -> str:
log_level = level.name
@@ -156,3 +159,18 @@ class Logger(LoggerBase):
Console.write_line(output, 'red')
exit()
def _fatal_console(self, name: str, message: str, ex: Exception = None):
output = ''
if ex is not None:
tb = traceback.format_exc()
self.error(name, message)
output = self._get_string(name, LoggingLevel.ERROR, f'{ex} -> {tb}')
else:
output = self._get_string(name, LoggingLevel.ERROR, message)
# check if message can be shown in console
if self._console.value >= LoggingLevel.FATAL.value:
Console.write_line(output, 'red')
exit()