[WIP] Added log command #44

This commit is contained in:
Nick Jungmann 2022-11-16 21:04:07 +01:00
parent 53604706c2
commit 2c7f4647af
3 changed files with 103 additions and 2 deletions

View File

@ -13,10 +13,14 @@ class CustomFileLoggerABC(Logger, ABC):
@abstractmethod @abstractmethod
def __init__(self, key: str, config: ConfigurationABC, time_format: TimeFormatSettings, env: ApplicationEnvironmentABC): def __init__(self, key: str, config: ConfigurationABC, time_format: TimeFormatSettings, env: ApplicationEnvironmentABC):
self._key = key self._key = key
settings: LoggingSettings = config.get_configuration(f'{FileLoggingSettings.__name__}_{key}') self._settings: LoggingSettings = config.get_configuration(f'{FileLoggingSettings.__name__}_{key}')
Logger.__init__(self, settings, time_format, env) Logger.__init__(self, self._settings, time_format, env)
self._begin_log() self._begin_log()
@property
def settings(self) -> LoggingSettings:
return self._settings
def _begin_log(self): def _begin_log(self):
console_level = self._console.value console_level = self._console.value
self._console = LoggingLevelEnum.OFF self._console = LoggingLevelEnum.OFF

View File

@ -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')

View File

@ -6,6 +6,7 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC
from bot_core.abc.module_abc import ModuleABC from bot_core.abc.module_abc import ModuleABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from modules.base.abc.base_helper_abc import BaseHelperABC 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.restart_command import RestartCommand
from modules.technician.command.shutdown_command import ShutdownCommand from modules.technician.command.shutdown_command import ShutdownCommand
from modules.base.service.base_helper_service import BaseHelperService from modules.base.service.base_helper_service import BaseHelperService
@ -24,4 +25,5 @@ class TechnicianModule(ModuleABC):
# commands # commands
self._dc.add_command(RestartCommand) self._dc.add_command(RestartCommand)
self._dc.add_command(ShutdownCommand) self._dc.add_command(ShutdownCommand)
self._dc.add_command(LogCommand)
# events # events