diff --git a/kdb-bot/src/bot_core/abc/custom_file_logger_abc.py b/kdb-bot/src/bot_core/abc/custom_file_logger_abc.py index c7354f65..d1cb8160 100644 --- a/kdb-bot/src/bot_core/abc/custom_file_logger_abc.py +++ b/kdb-bot/src/bot_core/abc/custom_file_logger_abc.py @@ -13,10 +13,14 @@ 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._settings: LoggingSettings = config.get_configuration(f'{FileLoggingSettings.__name__}_{key}') + Logger.__init__(self, self._settings, time_format, env) self._begin_log() + @property + def settings(self) -> LoggingSettings: + return self._settings + def _begin_log(self): console_level = self._console.value self._console = LoggingLevelEnum.OFF diff --git a/kdb-bot/src/modules/technician/command/log_command.py b/kdb-bot/src/modules/technician/command/log_command.py new file mode 100644 index 00000000..d7395d46 --- /dev/null +++ b/kdb-bot/src/modules/technician/command/log_command.py @@ -0,0 +1,95 @@ +import os +from datetime import datetime + +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_core.logging import LoggingSettings +from cpl_discord.command import DiscordCommandABC +from cpl_query.extension import List +from cpl_translation import TranslatePipe +from discord.ext import commands +from discord.ext.commands import Context + +from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC +from bot_core.abc.custom_file_logger_abc import CustomFileLoggerABC +from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.command_checks import CommandChecks +from bot_core.logging.command_logger import CommandLogger +from modules.permission.abc.permission_service_abc import PermissionServiceABC + + +class LogCommand(DiscordCommandABC): + + def __init__( + self, + logger: CommandLogger, + logging_settings: LoggingSettings, + services: ServiceProviderABC, + message_service: MessageServiceABC, + client_utils: ClientUtilsServiceABC, + translate: TranslatePipe, + permissions: PermissionServiceABC, + ): + DiscordCommandABC.__init__(self) + + self._logger = logger + self._logging_settings = logging_settings + self._services = services + self._message_service = message_service + self._client_utils = client_utils + self._t = translate + self._permissions = permissions + + self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') + + def _reduce_path(self, p: str) -> str: + if p.count('/') == 1 or p == '': + return p + + return self._reduce_path(os.path.dirname(p)) + + @commands.hybrid_command() + @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_technician() + async def log(self, ctx: Context, date_from: datetime = datetime.now()): + self._logger.debug(__name__, f'Received command log {ctx}') + + possible_log_paths = List(str) + possible_log_paths.append(self._reduce_path(self._logging_settings.path)) + + file_extensions = List(str) + if '.' in self._logging_settings.filename: + split_filename = self._logging_settings.filename.split(".") + file_extensions.append(f'.{split_filename[len(split_filename) - 1]}') + + for subclass in CustomFileLoggerABC.__subclasses__(): + logger: CustomFileLoggerABC = self._services.get_service(subclass) + if logger is None: + continue + + path = self._reduce_path(logger.settings.path) + if '.' in logger.settings.filename: + split_filename = logger.settings.filename.split(".") + file_extension = f'.{split_filename[len(split_filename) - 1]}' + if file_extension not in file_extensions: + file_extensions.append(file_extension) + + if path in possible_log_paths: + continue + possible_log_paths.append(path) + + files = List(str) + now = datetime.now() + for possible_path in possible_log_paths: + for r, d, f in os.walk(possible_path): + for file in f: + if '.' not in file: + continue + + split_filename = file.split(".") + if f'.{split_filename[len(split_filename) - 1]}' not in file_extensions: + continue + + files.append(os.path.join(r, file)) + + self._logger.trace(__name__, f'Finished log command') diff --git a/kdb-bot/src/modules/technician/technician_module.py b/kdb-bot/src/modules/technician/technician_module.py index 089e36de..5c074ca8 100644 --- a/kdb-bot/src/modules/technician/technician_module.py +++ b/kdb-bot/src/modules/technician/technician_module.py @@ -6,6 +6,7 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from modules.base.abc.base_helper_abc import BaseHelperABC +from modules.technician.command.log_command import LogCommand from modules.technician.command.restart_command import RestartCommand from modules.technician.command.shutdown_command import ShutdownCommand from modules.base.service.base_helper_service import BaseHelperService @@ -24,4 +25,5 @@ class TechnicianModule(ModuleABC): # commands self._dc.add_command(RestartCommand) self._dc.add_command(ShutdownCommand) + self._dc.add_command(LogCommand) # events