Refactored code
This commit is contained in:
26
src_old/sh_edraft/logging/__init__.py
Normal file
26
src_old/sh_edraft/logging/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
sh_edraft.logging
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
:copyright: (c) 2020 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'sh_edraft.logging'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
|
||||
__version__ = '2020.12.9'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from .logger import Logger
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=9)
|
26
src_old/sh_edraft/logging/base/__init__.py
Normal file
26
src_old/sh_edraft/logging/base/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
sh_edraft.logging.base
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
:copyright: (c) 2020 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'sh_edraft.logging.base'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
|
||||
__version__ = '2020.12.9'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from .logger_base import LoggerBase
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=9)
|
31
src_old/sh_edraft/logging/base/logger_base.py
Normal file
31
src_old/sh_edraft/logging/base/logger_base.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from abc import abstractmethod
|
||||
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
|
||||
|
||||
class LoggerBase(ServiceBase):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
ServiceBase.__init__(self)
|
||||
|
||||
@abstractmethod
|
||||
def header(self, string: str): pass
|
||||
|
||||
@abstractmethod
|
||||
def trace(self, name: str, message: str): pass
|
||||
|
||||
@abstractmethod
|
||||
def debug(self, name: str, message: str): pass
|
||||
|
||||
@abstractmethod
|
||||
def info(self, name: str, message: str): pass
|
||||
|
||||
@abstractmethod
|
||||
def warn(self, name: str, message: str): pass
|
||||
|
||||
@abstractmethod
|
||||
def error(self, name: str, message: str, ex: Exception = None): pass
|
||||
|
||||
@abstractmethod
|
||||
def fatal(self, name: str, message: str, ex: Exception = None): pass
|
195
src_old/sh_edraft/logging/logger.py
Normal file
195
src_old/sh_edraft/logging/logger.py
Normal file
@@ -0,0 +1,195 @@
|
||||
import datetime
|
||||
import os
|
||||
import traceback
|
||||
from string import Template
|
||||
|
||||
from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase
|
||||
from sh_edraft.logging.base.logger_base import LoggerBase
|
||||
from sh_edraft.logging.model.logging_settings import LoggingSettings
|
||||
from sh_edraft.logging.model.logging_level import LoggingLevel
|
||||
from sh_edraft.time.model.time_format_settings import TimeFormatSettings
|
||||
from sh_edraft.console.console import Console
|
||||
from sh_edraft.console.model.foreground_color import ForegroundColor
|
||||
|
||||
|
||||
class Logger(LoggerBase):
|
||||
|
||||
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._log = Template(self._log_settings.filename).substitute(
|
||||
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
|
||||
self._console = self._log_settings.console
|
||||
|
||||
self.create()
|
||||
|
||||
def _get_datetime_now(self) -> str:
|
||||
try:
|
||||
return datetime.datetime.now().strftime(self._time_format_settings.date_time_format)
|
||||
except Exception as e:
|
||||
self.error(__name__, 'Cannot get time', ex=e)
|
||||
|
||||
def _get_date(self) -> str:
|
||||
try:
|
||||
return datetime.datetime.now().strftime(self._time_format_settings.date_format)
|
||||
except Exception as e:
|
||||
self.error(__name__, 'Cannot get date', ex=e)
|
||||
|
||||
def create(self) -> None:
|
||||
""" path """
|
||||
try:
|
||||
# check if log file path exists
|
||||
if not os.path.exists(self._path):
|
||||
os.makedirs(self._path)
|
||||
except Exception as e:
|
||||
self._fatal_console(__name__, 'Cannot create log dir', ex=e)
|
||||
|
||||
""" create new log file """
|
||||
try:
|
||||
# open log file, create if not exists
|
||||
path = f'{self._path}{self._log}'
|
||||
f = open(path, "w+")
|
||||
Console.write_line(f'[{__name__}]: Using log file: {path}')
|
||||
f.close()
|
||||
except Exception as 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._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
|
||||
return f'<{self._get_datetime_now()}> [ {log_level} ] [ {name} ]: {message}'
|
||||
|
||||
def header(self, string: str):
|
||||
# append log and print message
|
||||
self._append_log(string)
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
Console.write_line(string)
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
|
||||
def trace(self, name: str, message: str):
|
||||
output = self._get_string(name, LoggingLevel.TRACE, message)
|
||||
|
||||
# check if message can be written to log
|
||||
if self._level.value >= LoggingLevel.TRACE.value:
|
||||
self._append_log(output)
|
||||
|
||||
# check if message can be shown in console
|
||||
if self._console.value >= LoggingLevel.TRACE.value:
|
||||
Console.set_foreground_color(ForegroundColor.green)
|
||||
Console.write_line(output)
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
|
||||
def debug(self, name: str, message: str):
|
||||
output = self._get_string(name, LoggingLevel.DEBUG, message)
|
||||
|
||||
# check if message can be written to log
|
||||
if self._level.value >= LoggingLevel.DEBUG.value:
|
||||
self._append_log(output)
|
||||
|
||||
# check if message can be shown in console
|
||||
if self._console.value >= LoggingLevel.DEBUG.value:
|
||||
Console.set_foreground_color(ForegroundColor.green)
|
||||
Console.write_line(output)
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
|
||||
def info(self, name: str, message: str):
|
||||
output = self._get_string(name, LoggingLevel.INFO, message)
|
||||
|
||||
# check if message can be written to log
|
||||
if self._level.value >= LoggingLevel.INFO.value:
|
||||
self._append_log(output)
|
||||
|
||||
# check if message can be shown in console
|
||||
if self._console.value >= LoggingLevel.INFO.value:
|
||||
Console.set_foreground_color(ForegroundColor.green)
|
||||
Console.write_line(output)
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
|
||||
def warn(self, name: str, message: str):
|
||||
output = self._get_string(name, LoggingLevel.WARN, message)
|
||||
|
||||
# check if message can be written to log
|
||||
if self._level.value >= LoggingLevel.WARN.value:
|
||||
self._append_log(output)
|
||||
|
||||
# check if message can be shown in console
|
||||
if self._console.value >= LoggingLevel.WARN.value:
|
||||
Console.set_foreground_color(ForegroundColor.yellow)
|
||||
Console.write_line(output)
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
|
||||
def error(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 written to log
|
||||
if self._level.value >= LoggingLevel.ERROR.value:
|
||||
self._append_log(output)
|
||||
|
||||
# check if message can be shown in console
|
||||
if self._console.value >= LoggingLevel.ERROR.value:
|
||||
Console.set_foreground_color(ForegroundColor.red)
|
||||
Console.write_line(output)
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
|
||||
def fatal(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.FATAL, f'{ex} -> {tb}')
|
||||
else:
|
||||
output = self._get_string(name, LoggingLevel.FATAL, message)
|
||||
|
||||
# check if message can be written to log
|
||||
if self._level.value >= LoggingLevel.FATAL.value:
|
||||
self._append_log(output)
|
||||
|
||||
# check if message can be shown in console
|
||||
if self._console.value >= LoggingLevel.FATAL.value:
|
||||
Console.set_foreground_color(ForegroundColor.red)
|
||||
Console.write_line(output)
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
|
||||
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.set_foreground_color(ForegroundColor.red)
|
||||
Console.write_line(output)
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
||||
|
||||
exit()
|
28
src_old/sh_edraft/logging/model/__init__.py
Normal file
28
src_old/sh_edraft/logging/model/__init__.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
sh_edraft.logging.model
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
:copyright: (c) 2020 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'sh_edraft.logging.model'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
|
||||
__version__ = '2020.12.9'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from .logging_level import LoggingLevel
|
||||
from .logging_settings import LoggingSettings
|
||||
from .logging_settings_name import LoggingSettingsName
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=9)
|
12
src_old/sh_edraft/logging/model/logging_level.py
Normal file
12
src_old/sh_edraft/logging/model/logging_level.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class LoggingLevel(Enum):
|
||||
|
||||
OFF = 0 # Nothing
|
||||
FATAL = 1 # Error that cause exit
|
||||
ERROR = 2 # Non fatal error
|
||||
WARN = 3 # Error that can later be fatal
|
||||
INFO = 4 # Normal information's
|
||||
DEBUG = 5 # Detailed app state
|
||||
TRACE = 6 # Detailed app information's
|
62
src_old/sh_edraft/logging/model/logging_settings.py
Normal file
62
src_old/sh_edraft/logging/model/logging_settings.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import traceback
|
||||
from typing import Optional
|
||||
|
||||
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
|
||||
from sh_edraft.logging.model.logging_settings_name import LoggingSettingsName
|
||||
from sh_edraft.logging.model.logging_level import LoggingLevel
|
||||
from sh_edraft.console.console import Console
|
||||
from sh_edraft.console.model.foreground_color import ForegroundColor
|
||||
|
||||
|
||||
class LoggingSettings(ConfigurationModelBase):
|
||||
|
||||
def __init__(self):
|
||||
ConfigurationModelBase.__init__(self)
|
||||
self._path: Optional[str] = None
|
||||
self._filename: Optional[str] = None
|
||||
self._console: Optional[LoggingLevel] = None
|
||||
self._level: Optional[LoggingLevel] = None
|
||||
|
||||
@property
|
||||
def path(self) -> str:
|
||||
return self._path
|
||||
|
||||
@path.setter
|
||||
def path(self, path: str) -> None:
|
||||
self._path = path
|
||||
|
||||
@property
|
||||
def filename(self) -> str:
|
||||
return self._filename
|
||||
|
||||
@filename.setter
|
||||
def filename(self, filename: str) -> None:
|
||||
self._filename = filename
|
||||
|
||||
@property
|
||||
def console(self) -> LoggingLevel:
|
||||
return self._console
|
||||
|
||||
@console.setter
|
||||
def console(self, console: LoggingLevel) -> None:
|
||||
self._console = console
|
||||
|
||||
@property
|
||||
def level(self) -> LoggingLevel:
|
||||
return self._level
|
||||
|
||||
@level.setter
|
||||
def level(self, level: LoggingLevel) -> None:
|
||||
self._level = level
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
self._path = settings[LoggingSettingsName.path.value]
|
||||
self._filename = settings[LoggingSettingsName.filename.value]
|
||||
self._console = LoggingLevel[settings[LoggingSettingsName.console_level.value]]
|
||||
self._level = LoggingLevel[settings[LoggingSettingsName.file_level.value]]
|
||||
except Exception as e:
|
||||
Console.set_foreground_color(ForegroundColor.red)
|
||||
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
|
||||
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
9
src_old/sh_edraft/logging/model/logging_settings_name.py
Normal file
9
src_old/sh_edraft/logging/model/logging_settings_name.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class LoggingSettingsName(Enum):
|
||||
|
||||
path = 'Path'
|
||||
filename = 'Filename'
|
||||
console_level = 'ConsoleLogLevel'
|
||||
file_level = 'FileLogLevel'
|
Reference in New Issue
Block a user