Improved logging

This commit is contained in:
Sven Heidemann 2021-11-16 11:41:34 +01:00
parent 16546b4d3c
commit 2176037d08
7 changed files with 38 additions and 13 deletions

View File

@ -21,6 +21,7 @@ class Application(ApplicationABC):
async def main(self): async def main(self):
try: try:
self._logger.trace(__name__, f'Try to start {BotService}')
await self._bot.start_async() await self._bot.start_async()
except Exception as e: except Exception as e:
self._logger.error(__name__, 'Start failed', e) self._logger.error(__name__, 'Start failed', e)

View File

@ -0,0 +1,2 @@
{
}

View File

@ -1,7 +1,12 @@
from datetime import datetime
from typing import Optional
from cpl_core.application import StartupABC from cpl_core.application import StartupABC
from cpl_core.configuration import ConfigurationABC from cpl_core.configuration import ConfigurationABC
from cpl_core.dependency_injection import ServiceCollectionABC, ServiceProviderABC from cpl_core.dependency_injection import (ServiceCollectionABC,
ServiceProviderABC)
from cpl_core.environment import ApplicationEnvironment from cpl_core.environment import ApplicationEnvironment
from cpl_core.logging.logger_abc import LoggerABC
from gismo_core.abc.bot_service_abc import BotServiceABC from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.service.bot_service import BotService from gismo_core.service.bot_service import BotService
@ -15,6 +20,9 @@ class Startup(StartupABC):
def __init__(self): def __init__(self):
StartupABC.__init__(self) StartupABC.__init__(self)
self._start_time = datetime.now()
self._config: Optional[ConfigurationABC] = None
async def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC: async def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC:
configuration.add_environment_variables('GISMO_') configuration.add_environment_variables('GISMO_')
@ -23,6 +31,9 @@ class Startup(StartupABC):
configuration.add_json_file(f'appsettings.{environment.environment_name}.json', optional=True) configuration.add_json_file(f'appsettings.{environment.environment_name}.json', optional=True)
configuration.add_json_file(f'appsettings.{environment.host_name}.json', optional=True) configuration.add_json_file(f'appsettings.{environment.host_name}.json', optional=True)
configuration.add_configuration('Startup_StartTime', self._start_time)
self._config = configuration
return configuration return configuration
async def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC: async def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
@ -33,4 +44,11 @@ class Startup(StartupABC):
services.add_transient(ModuleABC, BootLog) services.add_transient(ModuleABC, BootLog)
return services.build_service_provider() provider: ServiceProviderABC = services.build_service_provider()
startup_init_time = round((datetime.now() - self._config.get_configuration('Startup_StartTime')).total_seconds(), 2)
self._config.add_configuration('Startup_InitTime', startup_init_time)
logger: LoggerABC = provider.get_service(LoggerABC)
logger.debug(__name__, f'Startup Init time: {startup_init_time}s')
return provider

View File

@ -27,9 +27,11 @@ class BotService(BotServiceABC, commands.Bot):
# continue at on_ready # continue at on_ready
async def on_ready(self): async def on_ready(self):
self._logger.info(__name__, 'Connected to discord')
await self._modules.start_modules() await self._modules.start_modules()
async def stop_async(self): async def stop_async(self):
self._logger.debug(__name__, f'Try to stop {BotService}')
try: try:
pass pass
# save data # save data

View File

@ -16,13 +16,15 @@ class BootLog(ModuleABC):
self._logger = logger self._logger = logger
self._bot = bot self._bot = bot
ModuleABC.__init__(self) ModuleABC.__init__(self)
self._logger.trace(__name__, f'Module {type(self)} loaded')
async def on_ready(self): async def on_ready(self):
self._logger.info(__name__, f'Bot started') self._logger.debug(__name__, f'Module {type(self)} started')
try: try:
start_time = self._config.get_configuration('StartTime') start_time = self._config.get_configuration('Bot_StartTime')
init_time = round((datetime.now() - start_time).total_seconds(), 2) init_time = round((datetime.now() - start_time).total_seconds(), 2)
self._logger.info(__name__, f'Init time: {init_time}s') self._config.add_configuration('InitTime', init_time)
self._logger.debug(__name__, f'Bot Init time: {init_time}s')
# print warning if initialisation took too long # print warning if initialisation took too long
if init_time >= 30: if init_time >= 30:
self._logger.warn(__name__, 'It takes long time to start the bot!') self._logger.warn(__name__, 'It takes long time to start the bot!')
@ -37,3 +39,5 @@ class BootLog(ModuleABC):
self._logger.header(f'{self._bot.user.name}:') self._logger.header(f'{self._bot.user.name}:')
if self._logging_st.console.value >= LoggingLevelEnum.INFO.value: if self._logging_st.console.value >= LoggingLevelEnum.INFO.value:
Console.banner(self._bot.user.name) Console.banner(self._bot.user.name)
self._logger.trace(__name__, f'Module {type(self)} stopped')

View File

@ -13,5 +13,5 @@ class BootLogExtension(ApplicationExtensionABC):
async def run(self, config: ConfigurationABC, services: ServiceProviderABC): async def run(self, config: ConfigurationABC, services: ServiceProviderABC):
logger: LoggerABC = services.get_service(LoggerABC) logger: LoggerABC = services.get_service(LoggerABC)
logger.trace(__name__, 'Boot extension started') logger.debug(__name__, 'BootLog extension started')
config.add_configuration('StartTime', datetime.now()) config.add_configuration('Bot_StartTime', datetime.now())

View File

@ -1,14 +1,11 @@
from os import path from os import path
import discord import discord
from cpl_core.dependency_injection.service_provider_abc import \ from cpl_core.dependency_injection import ServiceProviderABC
ServiceProviderABC from cpl_core.environment import ApplicationEnvironmentABC
from cpl_core.environment.application_environment_abc import \
ApplicationEnvironmentABC
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from cpl_query.extension import List from cpl_query.extension import List
from discord.ext import commands from discord.ext import commands
from modules_core.abc.module_abc import ModuleABC from modules_core.abc.module_abc import ModuleABC
from modules_core.abc.module_service_abc import ModuleServiceABC from modules_core.abc.module_service_abc import ModuleServiceABC
@ -23,6 +20,7 @@ class ModuleService(ModuleServiceABC):
self._modules.extend(ModuleABC.__subclasses__()) self._modules.extend(ModuleABC.__subclasses__())
async def start_modules(self): async def start_modules(self):
self._logger.trace(__name__, 'Start loading modules')
modules = self._modules.where(lambda m: hasattr(m, 'on_ready') and callable(m.on_ready)) modules = self._modules.where(lambda m: hasattr(m, 'on_ready') and callable(m.on_ready))
for module_type in modules: for module_type in modules:
module = self._services.get_service(module_type) module = self._services.get_service(module_type)