Added custom logging support #45

This commit is contained in:
Sven Heidemann 2022-10-02 12:14:44 +02:00
parent 6e5e5d4550
commit 12cd5c9045
20 changed files with 286 additions and 56 deletions

View File

@ -1,4 +1,4 @@
from typing import Optional, Type
from typing import Optional, Type, Callable
from cpl_core.configuration import ConfigurationABC, ConfigurationModelABC
from cpl_core.console import Console
@ -9,8 +9,11 @@ from cpl_discord.configuration import DiscordBotSettings
from cpl_discord.service import DiscordBotServiceABC, DiscordBotService
from cpl_translation import TranslatePipe, TranslationServiceABC, TranslationSettings
from bot_core.configuration.bot_logging_settings import BotLoggingSettings
from bot_core.configuration.bot_settings import BotSettings
from bot_core.configuration.server_settings import ServerSettings
from bot_core.logging.command_logger import CommandLogger
from bot_core.logging.event_logger import EventLogger
from modules.base.configuration.base_server_settings import BaseServerSettings
from modules.base.configuration.base_settings import BaseSettings
from modules.boot_log.configuration.boot_log_server_settings import BootLogServerSettings
@ -24,6 +27,10 @@ class Application(DiscordBotApplicationABC):
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
DiscordBotApplicationABC.__init__(self, config, services)
self._services = services
self._cmd_logger: Optional[LoggerABC] = None
self._event_logger: Optional[LoggerABC] = None
# cpl-core
self._logger: LoggerABC = services.get_service(LoggerABC)
# cpl-discord
@ -33,27 +40,32 @@ class Application(DiscordBotApplicationABC):
self._translation: TranslationServiceABC = services.get_service(TranslationServiceABC)
self._translate: TranslatePipe = services.get_service(TranslatePipe)
def _configure_settings_with_servers(self, settings: Type, server_settings: Type):
def _configure_settings_with_sub_settings(self, settings: Type, list_atr: Callable, atr: Callable):
settings: Optional[settings] = self._configuration.get_configuration(settings)
if settings is None:
return
for server in settings.servers:
self._logger.trace(__name__, f'Saved config: {type(server).__name__}_{server.id}')
self._configuration.add_configuration(f'{type(server).__name__}_{server.id}', server)
for sub_settings in list_atr(settings):
self._logger.trace(__name__, f'Saved config: {type(sub_settings).__name__}_{atr(sub_settings)}')
self._configuration.add_configuration(f'{type(sub_settings).__name__}_{atr(sub_settings)}', sub_settings)
async def configure(self):
self._translation.load_by_settings(self._configuration.get_configuration(TranslationSettings))
self._configure_settings_with_servers(BotSettings, ServerSettings)
self._configure_settings_with_servers(BaseSettings, BaseServerSettings)
self._configure_settings_with_servers(BootLogSettings, BootLogServerSettings)
self._configure_settings_with_servers(PermissionSettings, PermissionServerSettings)
self._configure_settings_with_sub_settings(BotSettings, lambda x: x.servers, lambda x: x.id)
self._configure_settings_with_sub_settings(BaseSettings, lambda x: x.servers, lambda x: x.id)
self._configure_settings_with_sub_settings(BootLogSettings, lambda x: x.servers, lambda x: x.id)
self._configure_settings_with_sub_settings(PermissionSettings, lambda x: x.servers, lambda x: x.id)
self._configure_settings_with_sub_settings(BotLoggingSettings, lambda x: x.files, lambda x: x.key)
# custom loggers need to be loaded AFTER config is loaded correctly
self._cmd_logger: LoggerABC = self._services.get_service(CommandLogger)
self._event_logger: LoggerABC = self._services.get_service(EventLogger)
async def main(self):
try:
self._logger.debug(__name__, f'Starting...\n')
self._logger.trace(__name__, f'Try to start {DiscordBotService.__name__}')
await self._bot.start_async()
await self._bot.stop_async()
except Exception as e:
self._logger.error(__name__, 'Start failed', e)

View File

@ -1,10 +1,24 @@
{
"LoggingSettings": {
"Path": "logs/",
"Filename": "log_dev.log",
"Filename": "dev.log",
"ConsoleLogLevel": "DEBUG",
"FileLogLevel": "TRACE"
},
"BotLoggingSettings": {
"Command": {
"Path": "logs/",
"Filename": "commands.log",
"ConsoleLogLevel": "DEBUG",
"FileLogLevel": "TRACE"
},
"Event": {
"Path": "logs/",
"Filename": "event.log",
"ConsoleLogLevel": "DEBUG",
"FileLogLevel": "TRACE"
}
},
"DatabaseSettings": {
"Host": "localhost",
"User": "kd_kdb",

View File

@ -1,3 +1,5 @@
import os
from datetime import datetime
from typing import Optional
from cpl_core.application import StartupABC
@ -5,10 +7,13 @@ from cpl_core.configuration import ConfigurationABC
from cpl_core.database import DatabaseSettings
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
from cpl_core.environment import ApplicationEnvironment
from cpl_core.logging import LoggerABC
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.logging.command_logger import CommandLogger
from bot_core.logging.event_logger import EventLogger
from bot_core.pipes.date_time_offset_pipe import DateTimeOffsetPipe
from bot_core.service.client_utils_service import ClientUtilsService
from bot_core.service.message_service import MessageService
@ -35,22 +40,35 @@ class Startup(StartupABC):
def __init__(self):
StartupABC.__init__(self)
self._start_time = datetime.now()
self._config: Optional[ConfigurationABC] = None
self._feature_flags: Optional[FeatureFlagsSettings] = None
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC:
environment.set_working_directory(os.path.dirname(os.path.realpath(__file__)))
configuration.add_environment_variables('KDB_')
configuration.add_environment_variables('DISCORD_')
configuration.add_json_file(f'config/appsettings.json', optional=False)
configuration.add_json_file(f'config/appsettings.{environment.environment_name}.json', optional=True)
configuration.add_json_file(f'config/appsettings.{environment.host_name}.json', optional=True)
configuration.add_configuration('Startup_StartTime', str(self._start_time))
self._config = configuration
self._feature_flags = configuration.get_configuration(FeatureFlagsSettings)
return configuration
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
services.add_logging()
# custom logging
services.add_singleton(LoggerABC, CommandLogger)
services.add_singleton(LoggerABC, EventLogger)
services.add_translation()
services.add_db_context(DBContext, self._config.get_configuration(DatabaseSettings))
# general services
if self._feature_flags.base_module:
services.add_transient(BaseHelperABC, BaseHelperService)
services.add_transient(MessageServiceABC, MessageService)
services.add_transient(ClientUtilsServiceABC, ClientUtilsService)
@ -59,7 +77,6 @@ class Startup(StartupABC):
services.add_transient(DateTimeOffsetPipe)
# module services
if self._feature_flags.permission_module:
services.add_singleton(PermissionServiceABC, PermissionService)
# data services

View File

@ -15,6 +15,7 @@ from modules.base.command.help_command import HelpCommand
from modules.base.command.info_command import InfoCommand
from modules.base.command.ping_command import PingCommand
from modules.base.events.base_on_command_error_event import BaseOnCommandErrorEvent
from modules.base.events.base_on_command_event import BaseOnCommandEvent
from modules.moderator.command.purge_command import PurgeCommand
from modules.base.command.user_info_command import UserInfoCommand
from modules.base.events.base_on_member_join_event import BaseOnMemberJoinEvent
@ -38,44 +39,46 @@ class StartupDiscordExtension(StartupExtensionABC):
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
services.add_discord()
dc = get_discord_collection(services)
""" commands """
# admin
"""modules"""
if self._feature_flags.admin_module:
""" commands """
dc.add_command(RestartCommand)
dc.add_command(ShutdownCommand)
# moderator
if self._feature_flags.moderator_module:
dc.add_command(PurgeCommand)
# simple
""" events """
if self._feature_flags.base_module:
""" commands """
dc.add_command(AFKCommand)
dc.add_command(HelpCommand)
dc.add_command(InfoCommand)
dc.add_command(PingCommand)
dc.add_command(UserInfoCommand)
""" events """
# on_command_error
if self._feature_flags.base_module:
dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent)
dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent)
# on_member_join
if self._feature_flags.base_module:
dc.add_event(DiscordEventTypesEnum.on_member_join.value, BaseOnMemberJoinEvent)
# on_member_remove
if self._feature_flags.base_module:
dc.add_event(DiscordEventTypesEnum.on_member_join.value, BaseOnMemberRemoveEvent)
# on_member_update
if self._feature_flags.permission_module:
dc.add_event(DiscordEventTypesEnum.on_member_update.value, PermissionOnMemberUpdateEvent)
# on_message
if self._feature_flags.base_module:
dc.add_event(DiscordEventTypesEnum.on_message.value, BaseOnMessageEvent)
# on_voice_state_update
if self._feature_flags.base_module:
dc.add_event(DiscordEventTypesEnum.on_voice_state_update.value, BaseOnVoiceStateUpdateEvent)
# on_ready
if self._feature_flags.database_module:
""" commands """
""" events """
dc.add_event(DiscordEventTypesEnum.on_ready.value, DatabaseOnReadyEvent)
if self._feature_flags.moderator_module:
""" commands """
dc.add_command(PurgeCommand)
""" events """
if self._feature_flags.permission_module:
""" commands """
""" events """
dc.add_event(DiscordEventTypesEnum.on_ready.value, PermissionOnReadyEvent)
dc.add_event(DiscordEventTypesEnum.on_member_update.value, PermissionOnMemberUpdateEvent)
# has to be last!
if self._feature_flags.boot_log_module:
dc.add_event(DiscordEventTypesEnum.on_ready.value, BootLogOnReadyEvent) # has to be last
""" commands """
""" events """
dc.add_event(DiscordEventTypesEnum.on_ready.value, BootLogOnReadyEvent)

View 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)

View 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)

View 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()}')

View File

@ -0,0 +1 @@
# imports

View 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)

View 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)

View File

@ -1,5 +1,4 @@
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
@ -8,6 +7,7 @@ from discord.ext.commands import Context
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.logging.command_logger import CommandLogger
from modules.permission.abc.permission_service_abc import PermissionServiceABC
@ -15,7 +15,7 @@ class RestartCommand(DiscordCommandABC):
def __init__(
self,
logger: LoggerABC,
logger: CommandLogger,
config: ConfigurationABC,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,

View File

@ -1,5 +1,4 @@
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
@ -8,6 +7,7 @@ from discord.ext.commands import Context
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.logging.command_logger import CommandLogger
from modules.permission.abc.permission_service_abc import PermissionServiceABC
@ -15,7 +15,7 @@ class ShutdownCommand(DiscordCommandABC):
def __init__(
self,
logger: LoggerABC,
logger: CommandLogger,
config: ConfigurationABC,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,

View File

@ -1,5 +1,4 @@
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
@ -9,6 +8,7 @@ from discord.ext.commands import Context
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.logging.command_logger import CommandLogger
from modules.base.configuration.base_server_settings import BaseServerSettings
@ -16,7 +16,7 @@ class AFKCommand(DiscordCommandABC):
def __init__(
self,
logger: LoggerABC,
logger: CommandLogger,
config: ConfigurationABC,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,

View File

@ -1,5 +1,4 @@
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from discord.ext import commands
@ -7,6 +6,7 @@ from discord.ext.commands import Context
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.logging.command_logger import CommandLogger
from modules.base.configuration.base_server_settings import BaseServerSettings
@ -15,7 +15,7 @@ class HelpCommand(DiscordCommandABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
logger: CommandLogger,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
client_utils: ClientUtilsServiceABC

View File

@ -2,7 +2,6 @@ from datetime import datetime
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
@ -12,6 +11,7 @@ from discord.ext.commands import Context
import bot
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.logging.command_logger import CommandLogger
class InfoCommand(DiscordCommandABC):
@ -19,7 +19,7 @@ class InfoCommand(DiscordCommandABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
logger: CommandLogger,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
client_utils: ClientUtilsServiceABC,

View File

@ -1,4 +1,3 @@
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
@ -7,13 +6,14 @@ from discord.ext.commands import Context
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.logging.command_logger import CommandLogger
class PingCommand(DiscordCommandABC):
def __init__(
self,
logger: LoggerABC,
logger: CommandLogger,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
client_utils: ClientUtilsServiceABC,

View File

@ -2,7 +2,6 @@ from typing import Optional
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
@ -11,6 +10,7 @@ from discord.ext.commands import Context
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.logging.command_logger import CommandLogger
from bot_core.pipes.date_time_offset_pipe import DateTimeOffsetPipe
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC
@ -23,7 +23,7 @@ class UserInfoCommand(DiscordCommandABC):
def __init__(
self,
config: ConfigurationABC,
logger: LoggerABC,
logger: CommandLogger,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
client_utils: ClientUtilsServiceABC,

View File

@ -2,6 +2,7 @@ import traceback
from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
from cpl_core.console import Console
from cpl_query.extension import List
class BaseServerSettings(ConfigurationModelABC):
@ -13,7 +14,9 @@ class BaseServerSettings(ConfigurationModelABC):
self._max_voice_state_hours: int = 0
self._xp_per_message: int = 0
self._xp_per_ontime_hour: int = 0
self._afk_channel_ids: list[int] = []
self._afk_channel_ids: List[int] = List(int)
self._afk_command_channel_id: int = 0
self._help_command_reference_url: str = ''
@property
def id(self) -> int:
@ -32,7 +35,7 @@ class BaseServerSettings(ConfigurationModelABC):
return self._xp_per_ontime_hour
@property
def afk_channel_ids(self) -> list[int]:
def afk_channel_ids(self) -> List[int]:
return self._afk_channel_ids
@property

View File

@ -0,0 +1,41 @@
import datetime
import traceback
import uuid
from cpl_core.time import TimeFormatSettings
from cpl_discord.events import OnCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
from discord.ext import commands
from discord.ext.commands import Context, CommandError
from cpl_core.logging import LoggerABC
from cpl_discord.events.on_command_error_abc import OnCommandErrorABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.configuration.bot_settings import BotSettings
from bot_core.logging.command_logger import CommandLogger
class BaseOnCommandEvent(OnCommandABC):
def __init__(
self,
logger: CommandLogger,
bot: DiscordBotServiceABC,
messenger: MessageServiceABC,
bot_settings: BotSettings,
time_format_settings: TimeFormatSettings,
translate: TranslatePipe
):
OnCommandABC.__init__(self)
self._logger = logger
self._bot = bot
self._messenger = messenger
self._bot_settings = bot_settings
self._time_format_settings = time_format_settings
self._t = translate
async def on_command(self, ctx: Context):
self._logger.warn(__name__, f'Received command afk {ctx}')
pass

View File

@ -1,7 +1,6 @@
import asyncio
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_discord.command import DiscordCommandABC
from cpl_translation import TranslatePipe
from discord.ext import commands
@ -10,6 +9,7 @@ from discord.ext.commands import Context
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.configuration.server_settings import ServerSettings
from bot_core.logging.command_logger import CommandLogger
from modules.permission.abc.permission_service_abc import PermissionServiceABC
@ -17,7 +17,7 @@ class PurgeCommand(DiscordCommandABC):
def __init__(
self,
logger: LoggerABC,
logger: CommandLogger,
config: ConfigurationABC,
message_service: MessageServiceABC,
permissions: PermissionServiceABC,