Added logging to bot.py

doc_update
Joey Hines 2018-08-11 22:21:21 -05:00
parent c5d69fc9e8
commit 59c1078426
2 changed files with 18 additions and 4 deletions

View File

@ -6,16 +6,18 @@ Created by: Joey Hines (ZeroHD)
""" """
import logging import logging
import logging.handlers as handlers import logging.handlers as handlers
from bot import start_bot import bot
from BotConfig import bot_config from BotConfig import bot_config
def setup_logging(): def setup_logging():
discord_logger = logging.getLogger('discord') discord_logger = logging.getLogger('discord')
discord_logger.setLevel(logging.DEBUG) discord_logger.setLevel(logging.INFO)
sql_logger = logging.getLogger('sqlalchemy.engine') sql_logger = logging.getLogger('sqlalchemy.engine')
sql_logger.setLevel(logging.INFO) sql_logger.setLevel(logging.INFO)
bot_logger = logging.getLogger('bot')
bot_logger.setLevel(logging.INFO)
handler = handlers.TimedRotatingFileHandler(filename='Geoffrey.log', when='D', handler = handlers.TimedRotatingFileHandler(filename='Geoffrey.log', when='D',
interval=bot_config.rotation_duration, backupCount=bot_config.count, interval=bot_config.rotation_duration, backupCount=bot_config.count,
@ -25,10 +27,11 @@ def setup_logging():
discord_logger.addHandler(handler) discord_logger.addHandler(handler)
sql_logger.addHandler(handler) sql_logger.addHandler(handler)
bot_logger.addHandler(handler)
if __name__ == '__main__': if __name__ == '__main__':
print("Starting logging...") print("Starting logging...")
setup_logging() setup_logging()
print("Starting bot...") print("Starting bot...")
start_bot() bot.start_bot()

13
bot.py
View File

@ -6,6 +6,9 @@ from discord import Game
from MinecraftAccountInfoGrabber import * from MinecraftAccountInfoGrabber import *
from BotConfig import * from BotConfig import *
import asyncio import asyncio
import logging
logger = logging.getLogger(__name__)
description = ''' description = '''
Geoffrey (pronounced JOFF-ree) started his life as an inside joke none of you will understand. Geoffrey (pronounced JOFF-ree) started his life as an inside joke none of you will understand.
@ -38,6 +41,7 @@ async def on_ready():
print('Username: ' + bot.user.name) print('Username: ' + bot.user.name)
print('ID: ' + bot.user.id) print('ID: ' + bot.user.id)
logger.info("Geoffrey Online, ID: %s", bot.user.id)
await bot.change_presence(game=Game(name=bot_config.status)) await bot.change_presence(game=Game(name=bot_config.status))
@ -67,6 +71,7 @@ async def on_command_error(error, ctx):
elif isinstance(error.original, NotOnServerError): elif isinstance(error.original, NotOnServerError):
error_str = 'Command needs to be run on 24CC. Run this command there whoever you are.'.format() error_str = 'Command needs to be run on 24CC. Run this command there whoever you are.'.format()
else: else:
logger.error("Geoffrey encountered unhandled exception: %s", error)
error_str = bad_error_message.format(ctx.invoked_with, error) error_str = bad_error_message.format(ctx.invoked_with, error)
await bot.send_message(ctx.message.channel, '{} **Error Running Command:** {}'.format(ctx.message.author.mention, await bot.send_message(ctx.message.channel, '{} **Error Running Command:** {}'.format(ctx.message.author.mention,
@ -86,14 +91,15 @@ async def username_update():
player.name = grab_playername(player.mc_uuid) player.name = grab_playername(player.mc_uuid)
session.commit() session.commit()
print("Done.")
await asyncio.sleep(600) await asyncio.sleep(600)
except UsernameLookupFailed: except UsernameLookupFailed:
logger.info("Username lookup error.")
print("Username lookup error, are Mojang's servers down?") print("Username lookup error, are Mojang's servers down?")
session.rollback() session.rollback()
finally: finally:
print("Done.")
session.close() session.close()
if session is not None: if session is not None:
@ -101,16 +107,21 @@ async def username_update():
def start_bot(): def start_bot():
for extension in extensions: for extension in extensions:
try: try:
bot.load_extension(extension) bot.load_extension(extension)
except Exception as e: except Exception as e:
logger.info('Failed to load extension {}, {}'.format(extension, e))
print('Failed to load extension {}, {}'.format(extension, e)) print('Failed to load extension {}, {}'.format(extension, e))
try: try:
bot.loop.create_task(username_update()) bot.loop.create_task(username_update())
logger.info('Logging into discord...')
bot.run(bot_config.token) bot.run(bot_config.token)
except TimeoutError: except TimeoutError:
print("Disconnected, is Discord offline?") print("Disconnected, is Discord offline?")
logger.info('Disconnected, is Discord offline?')
finally: finally:
logger.info("Bot shutting down...")
print("Bot shutting down...") print("Bot shutting down...")