2020-11-22 20:17:57 +01:00
|
|
|
import datetime
|
|
|
|
import os
|
|
|
|
import traceback
|
|
|
|
from string import Template
|
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
from cpl.console.console import Console
|
2021-03-12 16:06:30 +01:00
|
|
|
from cpl.console.foreground_color_enum import ForegroundColorEnum
|
2021-03-29 08:56:18 +02:00
|
|
|
from cpl.environment.application_environment_abc import ApplicationEnvironmentABC
|
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
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
|
2021-03-03 10:47:52 +01:00
|
|
|
class Logger(LoggerABC):
|
2020-11-22 20:17:57 +01:00
|
|
|
|
2021-03-29 08:56:18 +02:00
|
|
|
def __init__(self, logging_settings: LoggingSettings, time_format: TimeFormatSettings, env: ApplicationEnvironmentABC):
|
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)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
2021-03-29 08:56:18 +02:00
|
|
|
self._env = env
|
2020-11-26 11:20:21 +01:00
|
|
|
self._log_settings: LoggingSettings = logging_settings
|
|
|
|
self._time_format_settings: TimeFormatSettings = time_format
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
self._log = Template(self._log_settings.filename).substitute(
|
2021-03-29 08:56:18 +02:00
|
|
|
date_time_now=self._env.date_time_now.strftime(self._time_format_settings.date_time_format),
|
|
|
|
start_time=self._env.start_time.strftime(self._time_format_settings.date_time_log_format)
|
2020-11-22 20:17:57 +01:00
|
|
|
)
|
|
|
|
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()
|
|
|
|
|
2020-11-22 20:17:57 +01:00
|
|
|
def _get_datetime_now(self) -> str:
|
2021-03-14 16:13:07 +01:00
|
|
|
"""
|
|
|
|
Returns the date and time by given format
|
|
|
|
:return:
|
|
|
|
"""
|
2020-11-22 20:17:57 +01:00
|
|
|
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:
|
|
|
|
"""
|
2020-11-22 20:17:57 +01:00
|
|
|
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:
|
|
|
|
"""
|
|
|
|
|
2020-11-22 20:17:57 +01:00
|
|
|
""" 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)
|
2020-11-22 20:17:57 +01:00
|
|
|
except Exception as e:
|
2020-11-26 19:17:05 +01:00
|
|
|
self._fatal_console(__name__, 'Cannot create log dir', ex=e)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
""" 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}')
|
2020-11-22 20:17:57 +01:00
|
|
|
f.close()
|
|
|
|
except Exception as e:
|
2020-11-26 19:17:05 +01:00
|
|
|
self._fatal_console(__name__, 'Cannot open log file', ex=e)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
def _append_log(self, string):
|
2021-03-14 16:13:07 +01:00
|
|
|
"""
|
|
|
|
Writes to logfile
|
|
|
|
:param string:
|
|
|
|
:return:
|
|
|
|
"""
|
2020-11-22 20:17:57 +01:00
|
|
|
try:
|
|
|
|
# open log file and append always
|
2020-11-26 19:17:05 +01:00
|
|
|
if not os.path.isdir(self._path):
|
|
|
|
self._fatal_console(__name__, 'Log directory not found')
|
|
|
|
|
2020-11-22 20:17:57 +01:00
|
|
|
with open(self._path + self._log, "a+", encoding="utf-8") as f:
|
|
|
|
f.write(string + '\n')
|
|
|
|
f.close()
|
|
|
|
except Exception as e:
|
2020-11-26 19:17:05 +01:00
|
|
|
self._fatal_console(__name__, f'Cannot append log file, message: {string}', ex=e)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
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:
|
|
|
|
"""
|
2020-11-22 20:17:57 +01:00
|
|
|
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)
|
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)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
def trace(self, name: str, message: str):
|
2021-03-12 16:06:30 +01:00
|
|
|
output = self._get_string(name, LoggingLevelEnum.TRACE, message)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
# check if message can be written to log
|
2021-03-12 16:06:30 +01:00
|
|
|
if self._level.value >= LoggingLevelEnum.TRACE.value:
|
2020-11-22 20:17:57 +01:00
|
|
|
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)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
def debug(self, name: str, message: str):
|
2021-03-12 16:06:30 +01:00
|
|
|
output = self._get_string(name, LoggingLevelEnum.DEBUG, message)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
# check if message can be written to log
|
2021-03-12 16:06:30 +01:00
|
|
|
if self._level.value >= LoggingLevelEnum.DEBUG.value:
|
2020-11-22 20:17:57 +01:00
|
|
|
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)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
def info(self, name: str, message: str):
|
2021-03-12 16:06:30 +01:00
|
|
|
output = self._get_string(name, LoggingLevelEnum.INFO, message)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
# check if message can be written to log
|
2021-03-12 16:06:30 +01:00
|
|
|
if self._level.value >= LoggingLevelEnum.INFO.value:
|
2020-11-22 20:17:57 +01:00
|
|
|
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)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
def warn(self, name: str, message: str):
|
2021-03-12 16:06:30 +01:00
|
|
|
output = self._get_string(name, LoggingLevelEnum.WARN, message)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
# check if message can be written to log
|
2021-03-12 16:06:30 +01:00
|
|
|
if self._level.value >= LoggingLevelEnum.WARN.value:
|
2020-11-22 20:17:57 +01:00
|
|
|
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)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
def error(self, name: str, message: str, ex: Exception = None):
|
|
|
|
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}')
|
2020-11-22 20:17:57 +01:00
|
|
|
else:
|
2021-03-12 16:06:30 +01:00
|
|
|
output = self._get_string(name, LoggingLevelEnum.ERROR, message)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
# check if message can be written to log
|
2021-03-12 16:06:30 +01:00
|
|
|
if self._level.value >= LoggingLevelEnum.ERROR.value:
|
2020-11-22 20:17:57 +01:00
|
|
|
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)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
def fatal(self, name: str, message: str, ex: Exception = None):
|
|
|
|
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}')
|
2020-11-22 20:17:57 +01:00
|
|
|
else:
|
2021-03-12 16:06:30 +01:00
|
|
|
output = self._get_string(name, LoggingLevelEnum.FATAL, message)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
# check if message can be written to log
|
2021-03-12 16:06:30 +01:00
|
|
|
if self._level.value >= LoggingLevelEnum.FATAL.value:
|
2020-11-22 20:17:57 +01:00
|
|
|
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)
|
2020-11-22 20:17:57 +01:00
|
|
|
|
|
|
|
exit()
|
2020-11-26 19:17:05 +01:00
|
|
|
|
|
|
|
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:
|
|
|
|
"""
|
2020-11-26 19:17:05 +01:00
|
|
|
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}')
|
2020-11-26 19:17:05 +01:00
|
|
|
else:
|
2021-03-12 16:06:30 +01:00
|
|
|
output = self._get_string(name, LoggingLevelEnum.ERROR, message)
|
2020-11-26 19:17:05 +01:00
|
|
|
|
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)
|
2020-11-26 19:17:05 +01:00
|
|
|
|
|
|
|
exit()
|