From 67640948d840477b8aa7c996c1abf8e28fe92a40 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Wed, 29 Aug 2018 10:08:21 -0500 Subject: [PATCH] Moved logging to bot.py and fixed config issues --- geoffrey/BotConfig.py | 11 +++++------ geoffrey/Geoffrey.py | 28 ---------------------------- geoffrey/bot.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/geoffrey/BotConfig.py b/geoffrey/BotConfig.py index 307da4f..b47d169 100644 --- a/geoffrey/BotConfig.py +++ b/geoffrey/BotConfig.py @@ -3,12 +3,12 @@ import configparser import os -def create_config(config): +def create_config(config, path): config['Discord'] = {'Token': '', 'Status': '', 'Prefix': '?', - 'Bot_Mod': '' - 'Error_Users: ''' + 'Bot_Mod': '', + 'Error_Users': '' } config['SQL'] = {'Dialect+Driver': 'mysql+mysqldb', 'Username': '', @@ -29,7 +29,7 @@ def create_config(config): } config['Special Names'] = {} - with open('GeoffreyConfig.ini', 'w') as configfile: + with open('{}/GeoffreyConfig.ini'.format(path), 'w') as configfile: config.write(configfile) @@ -39,7 +39,7 @@ def read_config(): config.read_file(codecs.open("{}/GeoffreyConfig.ini".format(path), "r", "utf8")) if len(config.sections()) == 0: - create_config(config) + create_config(config, path) print("GeoffreyConfig.ini generated.") quit(0) @@ -86,5 +86,4 @@ class Config: return engine_args.format(driver, username, password, host, port, database_name) - bot_config = Config() diff --git a/geoffrey/Geoffrey.py b/geoffrey/Geoffrey.py index 7a2d610..59e9a34 100644 --- a/geoffrey/Geoffrey.py +++ b/geoffrey/Geoffrey.py @@ -11,34 +11,6 @@ from sys import stdout from geoffrey import bot from geoffrey.BotConfig import bot_config - -def setup_logging(): - - discord_logger = logging.getLogger('discord') - discord_logger.setLevel(logging.INFO) - sql_logger = logging.getLogger('sqlalchemy.engine') - sql_logger.setLevel(logging.INFO) - bot_info_logger = logging.getLogger('geoffrey.bot') - bot_info_logger.setLevel(logging.INFO) - - handler = handlers.TimedRotatingFileHandler(filename='Geoffrey.log', when='D', - interval=bot_config.rotation_duration, backupCount=bot_config.count, - encoding='utf-8') - - handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) - - console = logging.StreamHandler(stdout) - - console.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) - - discord_logger.addHandler(handler) - sql_logger.addHandler(handler) - bot_info_logger.addHandler(handler) - bot_info_logger.addHandler(console) - - if __name__ == '__main__': - print("Starting logging...") - setup_logging() print("Starting bot...") bot.start_bot() diff --git a/geoffrey/bot.py b/geoffrey/bot.py index f976ff9..e1329f6 100644 --- a/geoffrey/bot.py +++ b/geoffrey/bot.py @@ -6,6 +6,10 @@ from discord.ext import commands from discord.utils import oauth_url from sqlalchemy.exc import OperationalError +import logging.handlers as handlers +from sys import stdout + + from geoffrey.BotConfig import bot_config from geoffrey.BotErrors import * from geoffrey.Commands import Commands @@ -134,9 +138,34 @@ async def username_update(): session.close() await asyncio.sleep(600) +def setup_logging(): + + discord_logger = logging.getLogger('discord') + discord_logger.setLevel(logging.INFO) + sql_logger = logging.getLogger('sqlalchemy.engine') + sql_logger.setLevel(logging.INFO) + bot_info_logger = logging.getLogger('geoffrey.bot') + bot_info_logger.setLevel(logging.INFO) + + handler = handlers.TimedRotatingFileHandler(filename='Geoffrey.log', when='D', + interval=bot_config.rotation_duration, backupCount=bot_config.count, + encoding='utf-8') + + handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) + + console = logging.StreamHandler(stdout) + + console.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) + + discord_logger.addHandler(handler) + sql_logger.addHandler(handler) + bot_info_logger.addHandler(handler) + bot_info_logger.addHandler(console) def start_bot(): try: + + setup_logging() Commands() for extension in extensions: try: