sh_cpl/src/cpl/logging/logger_service.py

277 lines
9.4 KiB
Python
Raw Normal View History

import datetime
import os
import traceback
from string import Template
2021-03-03 10:47:52 +01:00
from cpl.application.application_runtime_abc import ApplicationRuntimeABC
from cpl.console.console import Console
2021-03-12 16:06:30 +01:00
from cpl.console.foreground_color_enum import ForegroundColorEnum
2021-03-03 10:47:52 +01:00
from cpl.logging.logger_abc import LoggerABC
2021-03-12 16:06:30 +01:00
from cpl.logging.logging_level_enum import LoggingLevelEnum
2021-03-03 10:47:52 +01:00
from cpl.logging.logging_settings import LoggingSettings
from cpl.time.time_format_settings import TimeFormatSettings
2021-03-03 10:47:52 +01:00
class Logger(LoggerABC):
2021-03-03 10:47:52 +01:00
def __init__(self, logging_settings: LoggingSettings, time_format: TimeFormatSettings, app_runtime: ApplicationRuntimeABC):
2021-03-14 16:13:07 +01:00
"""
Service for logging
:param logging_settings:
:param time_format:
:param app_runtime:
"""
2021-03-03 10:47:52 +01:00
LoggerABC.__init__(self)
self._app_runtime = app_runtime
2020-11-26 11:20:21 +01:00
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
2020-12-10 18:11:05 +01:00
self.create()
def _get_datetime_now(self) -> str:
2021-03-14 16:13:07 +01:00
"""
Returns the date and time by given format
:return:
"""
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:
2021-03-14 16:13:07 +01:00
"""
Returns the date by given format
:return:
"""
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:
2021-03-14 16:13:07 +01:00
"""
Creates path tree and logfile
:return:
"""
""" path """
try:
# check if log file path exists
if not os.path.exists(self._path):
2020-12-16 17:37:43 +01:00
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+")
2020-12-06 21:14:57 +01:00
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):
2021-03-14 16:13:07 +01:00
"""
Writes to logfile
:param string:
:return:
"""
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)
2021-03-12 16:06:30 +01:00
def _get_string(self, name: str, level: LoggingLevelEnum, message: str) -> str:
2021-03-14 16:13:07 +01:00
"""
Returns input as log entry format
:param name:
:param level:
:param message:
:return:
"""
log_level = level.name
return f'<{self._get_datetime_now()}> [ {log_level} ] [ {name} ]: {message}'
def header(self, string: str):
2021-03-14 16:13:07 +01:00
"""
Writes a header message
:param string:
:return:
"""
# append log and print message
self._append_log(string)
2021-03-12 16:06:30 +01:00
Console.set_foreground_color(ForegroundColorEnum.default)
2020-12-14 21:10:26 +01:00
Console.write_line(string)
2021-03-12 16:06:30 +01:00
Console.set_foreground_color(ForegroundColorEnum.default)
def trace(self, name: str, message: str):
2021-03-14 16:13:07 +01:00
"""
Writes a trace message
:param name:
:param message:
:return:
"""
2021-03-12 16:06:30 +01:00
output = self._get_string(name, LoggingLevelEnum.TRACE, message)
# check if message can be written to log
2021-03-12 16:06:30 +01:00
if self._level.value >= LoggingLevelEnum.TRACE.value:
self._append_log(output)
2021-03-10 11:28:04 +01:00
# check if message can be shown in console_old
2021-03-12 16:06:30 +01:00
if self._console.value >= LoggingLevelEnum.TRACE.value:
Console.set_foreground_color(ForegroundColorEnum.green)
2020-12-14 21:10:26 +01:00
Console.write_line(output)
2021-03-12 16:06:30 +01:00
Console.set_foreground_color(ForegroundColorEnum.default)
def debug(self, name: str, message: str):
2021-03-14 16:13:07 +01:00
"""
Writes a debug message
:param name:
:param message:
:return:
"""
2021-03-12 16:06:30 +01:00
output = self._get_string(name, LoggingLevelEnum.DEBUG, message)
# check if message can be written to log
2021-03-12 16:06:30 +01:00
if self._level.value >= LoggingLevelEnum.DEBUG.value:
self._append_log(output)
2021-03-10 11:28:04 +01:00
# check if message can be shown in console_old
2021-03-12 16:06:30 +01:00
if self._console.value >= LoggingLevelEnum.DEBUG.value:
Console.set_foreground_color(ForegroundColorEnum.green)
2020-12-14 21:10:26 +01:00
Console.write_line(output)
2021-03-12 16:06:30 +01:00
Console.set_foreground_color(ForegroundColorEnum.default)
def info(self, name: str, message: str):
2021-03-14 16:13:07 +01:00
"""
Writes an information
:param name:
:param message:
:return:
"""
2021-03-12 16:06:30 +01:00
output = self._get_string(name, LoggingLevelEnum.INFO, message)
# check if message can be written to log
2021-03-12 16:06:30 +01:00
if self._level.value >= LoggingLevelEnum.INFO.value:
self._append_log(output)
2021-03-10 11:28:04 +01:00
# check if message can be shown in console_old
2021-03-12 16:06:30 +01:00
if self._console.value >= LoggingLevelEnum.INFO.value:
Console.set_foreground_color(ForegroundColorEnum.green)
2020-12-14 21:10:26 +01:00
Console.write_line(output)
2021-03-12 16:06:30 +01:00
Console.set_foreground_color(ForegroundColorEnum.default)
def warn(self, name: str, message: str):
2021-03-14 16:13:07 +01:00
"""
Writes an warning
:param name:
:param message:
:return:
"""
2021-03-12 16:06:30 +01:00
output = self._get_string(name, LoggingLevelEnum.WARN, message)
# check if message can be written to log
2021-03-12 16:06:30 +01:00
if self._level.value >= LoggingLevelEnum.WARN.value:
self._append_log(output)
2021-03-10 11:28:04 +01:00
# check if message can be shown in console_old
2021-03-12 16:06:30 +01:00
if self._console.value >= LoggingLevelEnum.WARN.value:
Console.set_foreground_color(ForegroundColorEnum.yellow)
2020-12-14 21:10:26 +01:00
Console.write_line(output)
2021-03-12 16:06:30 +01:00
Console.set_foreground_color(ForegroundColorEnum.default)
def error(self, name: str, message: str, ex: Exception = None):
2021-03-14 16:13:07 +01:00
"""
Writes an error
:param name:
:param message:
:param ex:
:return:
"""
output = ''
if ex is not None:
tb = traceback.format_exc()
self.error(name, message)
2021-03-12 16:06:30 +01:00
output = self._get_string(name, LoggingLevelEnum.ERROR, f'{ex} -> {tb}')
else:
2021-03-12 16:06:30 +01:00
output = self._get_string(name, LoggingLevelEnum.ERROR, message)
# check if message can be written to log
2021-03-12 16:06:30 +01:00
if self._level.value >= LoggingLevelEnum.ERROR.value:
self._append_log(output)
2021-03-10 11:28:04 +01:00
# check if message can be shown in console_old
2021-03-12 16:06:30 +01:00
if self._console.value >= LoggingLevelEnum.ERROR.value:
Console.set_foreground_color(ForegroundColorEnum.red)
2020-12-14 21:10:26 +01:00
Console.write_line(output)
2021-03-12 16:06:30 +01:00
Console.set_foreground_color(ForegroundColorEnum.default)
def fatal(self, name: str, message: str, ex: Exception = None):
2021-03-14 16:13:07 +01:00
"""
Writes an error and exits
:param name:
:param message:
:param ex:
:return:
"""
output = ''
if ex is not None:
tb = traceback.format_exc()
self.error(name, message)
2021-03-12 16:06:30 +01:00
output = self._get_string(name, LoggingLevelEnum.FATAL, f'{ex} -> {tb}')
else:
2021-03-12 16:06:30 +01:00
output = self._get_string(name, LoggingLevelEnum.FATAL, message)
# check if message can be written to log
2021-03-12 16:06:30 +01:00
if self._level.value >= LoggingLevelEnum.FATAL.value:
self._append_log(output)
2021-03-10 11:28:04 +01:00
# check if message can be shown in console_old
2021-03-12 16:06:30 +01:00
if self._console.value >= LoggingLevelEnum.FATAL.value:
Console.set_foreground_color(ForegroundColorEnum.red)
2020-12-14 21:10:26 +01:00
Console.write_line(output)
2021-03-12 16:06:30 +01:00
Console.set_foreground_color(ForegroundColorEnum.default)
exit()
def _fatal_console(self, name: str, message: str, ex: Exception = None):
2021-03-14 16:13:07 +01:00
"""
Writes an error to console only
:param name:
:param message:
:param ex:
:return:
"""
output = ''
if ex is not None:
tb = traceback.format_exc()
self.error(name, message)
2021-03-12 16:06:30 +01:00
output = self._get_string(name, LoggingLevelEnum.ERROR, f'{ex} -> {tb}')
else:
2021-03-12 16:06:30 +01:00
output = self._get_string(name, LoggingLevelEnum.ERROR, message)
2021-03-10 11:28:04 +01:00
# check if message can be shown in console_old
2021-03-12 16:06:30 +01:00
if self._console.value >= LoggingLevelEnum.FATAL.value:
Console.set_foreground_color(ForegroundColorEnum.red)
2020-12-14 21:10:26 +01:00
Console.write_line(output)
2021-03-12 16:06:30 +01:00
Console.set_foreground_color(ForegroundColorEnum.default)
exit()