Added docs for cpl.logging

This commit is contained in:
Sven Heidemann 2021-05-17 17:45:22 +02:00
parent 89f3c5833f
commit 6fc4cebbfe
3 changed files with 112 additions and 72 deletions

View File

@ -2,81 +2,101 @@ from abc import abstractmethod, ABC
class LoggerABC(ABC): class LoggerABC(ABC):
r"""ABC for :class:`cpl.logging.logger_service.Logger`"""
@abstractmethod @abstractmethod
def __init__(self): def __init__(self):
"""
ABC for logging
"""
ABC.__init__(self) ABC.__init__(self)
@abstractmethod @abstractmethod
def header(self, string: str): def header(self, string: str):
""" r"""Writes a header message
Writes a header message
:param string: Parameter
:return: ---------
string: :class:`str`
String to write as header
""" """
pass pass
@abstractmethod @abstractmethod
def trace(self, name: str, message: str): def trace(self, name: str, message: str):
""" r"""Writes a trace message
Writes a trace message
:param name: Parameter
:param message: ---------
:return: name: :class:`str`
Message name
message: :class:`str`
Message string
""" """
pass pass
@abstractmethod @abstractmethod
def debug(self, name: str, message: str): def debug(self, name: str, message: str):
""" r"""Writes a debug message
Writes a debug message
:param name: Parameter
:param message: ---------
:return: name: :class:`str`
Message name
message: :class:`str`
Message string
""" """
pass pass
@abstractmethod @abstractmethod
def info(self, name: str, message: str): def info(self, name: str, message: str):
""" r"""Writes an information
Writes an information
:param name: Parameter
:param message: ---------
:return: name: :class:`str`
Message name
message: :class:`str`
Message string
""" """
pass pass
@abstractmethod @abstractmethod
def warn(self, name: str, message: str): def warn(self, name: str, message: str):
""" r"""Writes an warning
Writes an warning
:param name: Parameter
:param message: ---------
:return: name: :class:`str`
Message name
message: :class:`str`
Message string
""" """
pass pass
@abstractmethod @abstractmethod
def error(self, name: str, message: str, ex: Exception = None): def error(self, name: str, message: str, ex: Exception = None):
""" r"""Writes an error
Writes an error
:param name: Parameter
:param message: ---------
:param ex: name: :class:`str`
:return: Error name
message: :class:`str`
Error message
ex: :class:`Exception`
Thrown exception
""" """
pass pass
@abstractmethod @abstractmethod
def fatal(self, name: str, message: str, ex: Exception = None): def fatal(self, name: str, message: str, ex: Exception = None):
""" r"""Writes an error and ends the program
Writes an error and exits
:param name: Parameter
:param message: ---------
:param ex: name: :class:`str`
:return: Error name
message: :class:`str`
Error message
ex: :class:`Exception`
Thrown exception
""" """
pass pass

View File

@ -13,14 +13,19 @@ from cpl.time.time_format_settings import TimeFormatSettings
class Logger(LoggerABC): class Logger(LoggerABC):
r"""Service for logging
Parameter
---------
logging_settings: :class:`cpl.logging.logging_settings.LoggingSettings`
Settings for the logger
time_format: :class:`cpl.time.time_format_settings.TimeFormatSettings`
Time format settings
env: :class:`cpl.environment.application_environment_abc.ApplicationEnvironmentABC`
Environment of the application
"""
def __init__(self, logging_settings: LoggingSettings, time_format: TimeFormatSettings, env: ApplicationEnvironmentABC): def __init__(self, logging_settings: LoggingSettings, time_format: TimeFormatSettings, env: ApplicationEnvironmentABC):
"""
Service for logging
:param logging_settings:
:param time_format:
:param app_runtime:
"""
LoggerABC.__init__(self) LoggerABC.__init__(self)
self._env = env self._env = env
@ -38,9 +43,11 @@ class Logger(LoggerABC):
self.create() self.create()
def _get_datetime_now(self) -> str: def _get_datetime_now(self) -> str:
""" r"""Returns the date and time by given format
Returns the date and time by given format
:return: Returns
-------
Date and time in given format
""" """
try: try:
return datetime.datetime.now().strftime(self._time_format_settings.date_time_format) return datetime.datetime.now().strftime(self._time_format_settings.date_time_format)
@ -48,9 +55,11 @@ class Logger(LoggerABC):
self.error(__name__, 'Cannot get time', ex=e) self.error(__name__, 'Cannot get time', ex=e)
def _get_date(self) -> str: def _get_date(self) -> str:
""" r"""Returns the date by given format
Returns the date by given format
:return: Returns
-------
Date in given format
""" """
try: try:
return datetime.datetime.now().strftime(self._time_format_settings.date_format) return datetime.datetime.now().strftime(self._time_format_settings.date_format)
@ -58,10 +67,7 @@ class Logger(LoggerABC):
self.error(__name__, 'Cannot get date', ex=e) self.error(__name__, 'Cannot get date', ex=e)
def create(self) -> None: def create(self) -> None:
""" r"""Creates path tree and logfile"""
Creates path tree and logfile
:return:
"""
""" path """ """ path """
try: try:
@ -81,11 +87,12 @@ class Logger(LoggerABC):
except Exception as e: except Exception as e:
self._fatal_console(__name__, 'Cannot open log file', ex=e) self._fatal_console(__name__, 'Cannot open log file', ex=e)
def _append_log(self, string): def _append_log(self, string: str):
""" r"""Writes to logfile
Writes to logfile
:param string: Parameter
:return: ---------
string: :class:`str`
""" """
try: try:
# open log file and append always # open log file and append always
@ -99,12 +106,20 @@ class Logger(LoggerABC):
self._fatal_console(__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: LoggingLevelEnum, message: str) -> str: def _get_string(self, name: str, level: LoggingLevelEnum, message: str) -> str:
""" r"""Returns input as log entry format
Returns input as log entry format
:param name: Parameter
:param level: ---------
:param message: name: :class:`str`
:return: Name of the message
level: :class:`cpl.logging.logging_level_enum.LoggingLevelEnum`
Logging level
message: :class:`str`
Log message
Returns
-------
Formatted string for logging
""" """
log_level = level.name log_level = level.name
return f'<{self._get_datetime_now()}> [ {log_level} ] [ {name} ]: {message}' return f'<{self._get_datetime_now()}> [ {log_level} ] [ {name} ]: {message}'
@ -209,12 +224,16 @@ class Logger(LoggerABC):
exit() exit()
def _fatal_console(self, name: str, message: str, ex: Exception = None): def _fatal_console(self, name: str, message: str, ex: Exception = None):
""" r"""Writes an error to console only
Writes an error to console only
:param name: Parameter
:param message: ---------
:param ex: name: :class:`str`
:return: Error name
message: :class:`str`
Error message
ex: :class:`Exception`
Thrown exception
""" """
output = '' output = ''
if ex is not None: if ex is not None:

View File

@ -9,6 +9,7 @@ from cpl.logging.logging_settings_name_enum import LoggingSettingsNameEnum
class LoggingSettings(ConfigurationModelABC): class LoggingSettings(ConfigurationModelABC):
r"""Representation of logging settings"""
def __init__(self): def __init__(self):
ConfigurationModelABC.__init__(self) ConfigurationModelABC.__init__(self)