From 4521712a9ae2749de646da7b26190c6db2903f66 Mon Sep 17 00:00:00 2001 From: SonOfLope Date: Sat, 2 Dec 2023 17:49:39 -0500 Subject: [PATCH] adds logging --- events.py | 4 +++- requirements.txt | 6 +++++- routes/routes.py | 56 +++++++++++++++++++++++++++++++----------------- trema.py | 42 +++++++++++++++++++++++------------- 4 files changed, 71 insertions(+), 37 deletions(-) diff --git a/events.py b/events.py index 3f9be42..0eb2028 100644 --- a/events.py +++ b/events.py @@ -13,7 +13,9 @@ def _get_welcome_chan(guild, trema_db): welcome_chan = guild.get_channel(welcome_chan_id) return welcome_chan +import logging +logger = logging.getLogger(__name__) def create_event_reactions(trema_bot, trema_db): @trema_bot.event @@ -55,4 +57,4 @@ async def on_member_remove(member): @trema_bot.event async def on_ready(): - print(f"{trema_bot.user} fonctionne.") \ No newline at end of file + logger.info('{0.user} fonctionne.'.format(trema_bot)) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 9ca578b..e3d2e67 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,10 @@ +flask>=2.0.0 +click>=8.0.0 +Werkzeug~=2.2.2 jsonschema==4.7.2 pymongo==4.1.1 pytest~=7.2.0 discord~=2.1.0 pytz~=2023.3 -quart~=0.18.4 \ No newline at end of file +quart~=0.18.4 +urllib3<1.27,>=1.26.6 \ No newline at end of file diff --git a/routes/routes.py b/routes/routes.py index 42fac86..317bc74 100644 --- a/routes/routes.py +++ b/routes/routes.py @@ -2,6 +2,10 @@ Embed import re from quart import request, jsonify +import traceback +import logging + +logger = logging.getLogger(__name__) def create_routes(app, database, trema): create_routes_webhooks(app, database, trema) @@ -9,30 +13,42 @@ def create_routes(app, database, trema): def create_routes_webhooks(app, database, trema): @app.route('/webhooks/', methods=['POST']) async def handle_webhook(uuid): - channelID = database.get_channel_by_webhook(uuid) - if channelID is None: - return jsonify({'error': 'Invalid webhook UUID'}), 400 + logger.info(f"Processing webhook called with UUID: {uuid}") + + try: + channelID = database.get_channel_by_webhook(uuid) + if channelID is None: + return jsonify({'error': 'Invalid webhook UUID'}), 400 - incoming_data = await request.json - embed_data = incoming_data.get('embeds', [])[0] + incoming_data = await request.json + embed_data = incoming_data.get('embeds', [])[0] - embed = Embed( - title=embed_data.get('title', 'N/A'), - color=int(embed_data.get('color', '0')) - ) + embed = Embed( + title=embed_data.get('title', 'N/A'), + color=int(embed_data.get('color', '0')) + ) - description = embed_data.get('description', 'N/A') + description = embed_data.get('description', 'N/A') - fields = re.findall(r"\*\*(.+?):\*\* (.+?)(?=\n|$)", description) - for name, value in fields: - embed.add_field(name=name, value=value, inline=False if "Details" in name else True) + fields = re.findall(r"\*\*(.+?):\*\* (.+?)(?=\n|$)", description) + for name, value in fields: + embed.add_field(name=name, value=value, inline=False if "Details" in name else True) - footer_data = embed_data.get('footer', {}) - footer_text = footer_data.get('text', '') - if footer_text: - embed.set_footer(text=footer_text) + footer_data = embed_data.get('footer', {}) + footer_text = footer_data.get('text', '') + if footer_text: + embed.set_footer(text=footer_text) - channel = trema.get_channel(int(channelID)) - await channel.send(embed=embed) + channel = trema.get_channel(int(channelID)) + await channel.send(embed=embed) - return jsonify({'status': 'success'}), 200 \ No newline at end of file + logger.info(f"Webhook called with UUID: {uuid} - Success") + return jsonify({'status': 'success'}), 200 + except Exception as e: + error_info = { + 'error': 'Internal Server Error', + 'message': str(e), + 'trace': traceback.format_exc() + } + logger.error(f"Webhook called with UUID: {uuid} - Error: {error_info}") + return jsonify(error_info), 500 \ No newline at end of file diff --git a/trema.py b/trema.py index 7e743e5..1d2a5a5 100644 --- a/trema.py +++ b/trema.py @@ -8,31 +8,32 @@ from datetime import datetime from quart import Quart import asyncio -from events import\ - create_event_reactions -from slash_commands import\ - create_slash_cmds -from trema_database import\ - get_trema_database -from routes import\ - create_routes +from events import create_event_reactions +from slash_commands import create_slash_cmds +from trema_database import get_trema_database +from routes import create_routes +import logging + +# Set up logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) start_time = datetime.now() bot_token = os.getenv('DISCORD_TOKEN') if not bot_token: - print('ERROR: Token var is missing: DISCORD_TOKEN') + logger.error('ERROR: Token var is missing: DISCORD_TOKEN') sys.exit(-1) api_address = os.getenv('API_ADDRESS') if not api_address: - print('ERROR: API address var is missing: API_ADDRESS') - sys.exit(-1) + logger.error('ERROR: API address var is missing: API_ADDRESS') + sys.exit(-1) api_port = os.getenv('API_PORT') if not api_port: - print('ERROR: API port var is missing: API_PORT') - sys.exit(-1) + logger.error('ERROR: API port var is missing: API_PORT') + sys.exit(-1) app = Quart(__name__) @@ -49,12 +50,23 @@ async def main(): loop = asyncio.get_event_loop() - # Start the bot and the API + logger.info("Starting the bot and API") + + # Start the bot and the API bot_coro = loop.create_task(trema.start(bot_token)) api_coro = loop.create_task(app.run_task(host=api_address, port=api_port)) await asyncio.gather(bot_coro, api_coro) if __name__ == '__main__': + logger.info("Main application starting") loop = asyncio.get_event_loop() - loop.run_until_complete(main()) \ No newline at end of file + try: + loop.run_until_complete(main()) + except Exception as e: + logger.error(f"Unhandled exception in main application: {e}") + raise + finally: + logger.info("Main application shutting down") + loop.close() +