Added custom logging support #45
This commit is contained in:
60
src/bot_core/abc/custom_file_logger_abc.py
Normal file
60
src/bot_core/abc/custom_file_logger_abc.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
from cpl_core.logging import LoggingSettings, Logger, LoggingLevelEnum, LoggerABC
|
||||
from cpl_core.time import TimeFormatSettings
|
||||
|
||||
from bot_core.configuration.file_logging_settings import FileLoggingSettings
|
||||
|
||||
|
||||
class CustomFileLoggerABC(Logger, ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self, key: str, config: ConfigurationABC, time_format: TimeFormatSettings, env: ApplicationEnvironmentABC):
|
||||
self._key = key
|
||||
settings: LoggingSettings = config.get_configuration(f'{FileLoggingSettings.__name__}_{key}')
|
||||
Logger.__init__(self, settings, time_format, env)
|
||||
self._begin_log()
|
||||
|
||||
def _begin_log(self):
|
||||
console_level = self._console.value
|
||||
self._console = LoggingLevelEnum.OFF
|
||||
self.info(__name__, f'Starting...\n')
|
||||
self._console = LoggingLevelEnum(console_level)
|
||||
|
||||
def _get_string(self, name_list_as_str: str, level: LoggingLevelEnum, message: str) -> str:
|
||||
names = name_list_as_str.split(' ')
|
||||
log_level = level.name
|
||||
string = f'<{self._get_datetime_now()}> [ {log_level} ] '
|
||||
for name in names:
|
||||
string += f'[ {name} ] '
|
||||
string += f': {message}'
|
||||
return string
|
||||
|
||||
def header(self, string: str):
|
||||
super().header(string)
|
||||
|
||||
def trace(self, name: str, message: str):
|
||||
name = f'{name} {self._key}'
|
||||
super().trace(name, message)
|
||||
|
||||
def debug(self, name: str, message: str):
|
||||
name = f'{name} {self._key}'
|
||||
super().debug(name, message)
|
||||
|
||||
def info(self, name: str, message: str):
|
||||
name = f'{name} {self._key}'
|
||||
super().info(name, message)
|
||||
|
||||
def warn(self, name: str, message: str):
|
||||
name = f'{name} {self._key}'
|
||||
super().warn(name, message)
|
||||
|
||||
def error(self, name: str, message: str, ex: Exception = None):
|
||||
name = f'{name} {self._key}'
|
||||
super().error(name, message, ex)
|
||||
|
||||
def fatal(self, name: str, message: str, ex: Exception = None):
|
||||
name = f'{name} {self._key}'
|
||||
super().fatal(name, message, ex)
|
33
src/bot_core/configuration/bot_logging_settings.py
Normal file
33
src/bot_core/configuration/bot_logging_settings.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import traceback
|
||||
|
||||
from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
|
||||
from cpl_core.console import Console, ForegroundColorEnum
|
||||
from cpl_query.extension import List
|
||||
|
||||
from bot_core.configuration.file_logging_settings import FileLoggingSettings
|
||||
|
||||
|
||||
class BotLoggingSettings(ConfigurationModelABC):
|
||||
|
||||
def __init__(self):
|
||||
ConfigurationModelABC.__init__(self)
|
||||
self._files: List[FileLoggingSettings] = List(FileLoggingSettings)
|
||||
|
||||
@property
|
||||
def files(self) -> List[FileLoggingSettings]:
|
||||
return self._files
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
files = List(FileLoggingSettings)
|
||||
for s in settings:
|
||||
st = FileLoggingSettings()
|
||||
settings[s]['Key'] = s
|
||||
st.from_dict(settings[s])
|
||||
files.append(st)
|
||||
self._files = files
|
||||
except Exception as e:
|
||||
Console.set_foreground_color(ForegroundColorEnum.red)
|
||||
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings')
|
||||
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
||||
Console.set_foreground_color(ForegroundColorEnum.default)
|
24
src/bot_core/configuration/file_logging_settings.py
Normal file
24
src/bot_core/configuration/file_logging_settings.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import traceback
|
||||
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.logging import LoggingSettings
|
||||
|
||||
|
||||
class FileLoggingSettings(LoggingSettings):
|
||||
|
||||
def __init__(self):
|
||||
LoggingSettings.__init__(self)
|
||||
|
||||
self._key = ''
|
||||
|
||||
@property
|
||||
def key(self) -> str:
|
||||
return self._key
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
self._key = settings['Key']
|
||||
super().from_dict(settings)
|
||||
except Exception as e:
|
||||
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings')
|
||||
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
1
src/bot_core/logging/__init__.py
Normal file
1
src/bot_core/logging/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# imports
|
11
src/bot_core/logging/command_logger.py
Normal file
11
src/bot_core/logging/command_logger.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
from cpl_core.time import TimeFormatSettings
|
||||
|
||||
from bot_core.abc.custom_file_logger_abc import CustomFileLoggerABC
|
||||
|
||||
|
||||
class CommandLogger(CustomFileLoggerABC):
|
||||
|
||||
def __init__(self, config: ConfigurationABC, time_format: TimeFormatSettings, env: ApplicationEnvironmentABC):
|
||||
CustomFileLoggerABC.__init__(self, 'Command', config, time_format, env)
|
11
src/bot_core/logging/event_logger.py
Normal file
11
src/bot_core/logging/event_logger.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.environment import ApplicationEnvironmentABC
|
||||
from cpl_core.time import TimeFormatSettings
|
||||
|
||||
from bot_core.abc.custom_file_logger_abc import CustomFileLoggerABC
|
||||
|
||||
|
||||
class EventLogger(CustomFileLoggerABC):
|
||||
|
||||
def __init__(self, config: ConfigurationABC, time_format: TimeFormatSettings, env: ApplicationEnvironmentABC):
|
||||
CustomFileLoggerABC.__init__(self, 'Event', config, time_format, env)
|
Reference in New Issue
Block a user