New logging

This commit is contained in:
Logan Cusano
2022-03-28 01:27:37 -04:00
parent d97e51d960
commit c741288b87
7 changed files with 158 additions and 95 deletions

View File

@@ -1,4 +1,6 @@
import configparser
import logging
from datetime import date
from os.path import exists
from NoiseGatev2 import AudioStream
@@ -12,6 +14,8 @@ PDB_KNOWN_BOT_IDS = {756327271597473863: "Greada", 915064996994633729: "Jorn", 9
DEFAULT_NOISEGATE_THRESHOLD = 50
LOGGER = logging.getLogger('Discord_Radio_Bot.Bot_Resources')
def check_if_config_exists():
if exists('./config.ini'):
@@ -38,9 +42,9 @@ def read_config_file():
'Handler': str(config['Config']['Handler'])
}
print("Found config options:")
LOGGER.debug("Found config options:")
for key in config_return.keys():
print(f"\t{key} : {config_return[key]}")
LOGGER.debug(f"\t{key} : {config_return[key]}")
return config_return
@@ -91,14 +95,14 @@ def write_config_file(**kwargs):
with open('./config.ini', 'w') as config_file:
config.write(config_file)
print("Config Saved")
LOGGER.info("Config Saved")
return True
def get_device_list():
list_of_devices = sound.query_devices().items()
print(list_of_devices)
list_of_devices = AudioStream().list_devices()
LOGGER.debug(list_of_devices)
return list_of_devices
@@ -106,38 +110,38 @@ def get_user_device_selection():
device_list = get_device_list()
org_device_list = []
for device, dev_id in device_list:
print(f"{dev_id + 1}\t-\t{device}")
LOGGER.debug(f"{dev_id + 1}\t-\t{device}")
org_device_list.append((dev_id, device))
selected_list_id = None
selected_device = None
while not selected_list_id:
print(f"selected device: {selected_list_id}")
print(device_list)
LOGGER.debug(f"selected device: {selected_list_id}")
LOGGER.debug(device_list)
try:
selected_list_id = int(input(f"Please select the input device from above:\t"))
except Exception as e:
print(e)
LOGGER.warning(e)
continue
if int(selected_list_id) < int(len(device_list)):
print("ford")
LOGGER.debug("Selected ID within range")
continue
elif selected_list_id > int(len(device_list)):
print("Out of range, try again...")
LOGGER.debug("Out of range, try again...")
selected_list_id = None
continue
else:
selected_list_id = None
print("Internal error, try again")
LOGGER.error("Internal error, try again")
continue
for dev_dict in org_device_list:
print(list(device_list)[selected_list_id-1][0])
LOGGER.debug(list(device_list)[selected_list_id-1][0])
if dev_dict[1] == list(device_list)[selected_list_id-1][0]:
selected_device = dev_dict
print(selected_device)
LOGGER.debug(selected_device)
return selected_device
@@ -149,7 +153,7 @@ def get_user_token():
if len(token) == 59:
return token
else:
print('Length error in token, please try again...')
LOGGER.error('Length error in token, please try again...')
token = None
continue
@@ -168,7 +172,7 @@ def get_user_mention_channel_id():
if len(str(channel_id)) == len('757379843792044102'):
return channel_id
else:
print("Length error in ID, please try again")
LOGGER.error("Length error in ID, please try again")
channel_id = None
continue
@@ -204,3 +208,36 @@ async def check_and_reply_to_ping(bot, message):
else:
await bot.process_commands(message)
return False
# Create the logger
def init_global_logger(_verbose_level: str = "WARNING"):
numeric_log_level = getattr(logging, _verbose_level.upper(), None)
if not isinstance(numeric_log_level, int):
raise ValueError('Invalid log level: %s' % _verbose_level)
else:
# create logger
init_logger = logging.getLogger('Discord_Radio_Bot')
init_logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler(f'DRB-{date.today()}.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(numeric_log_level)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
init_logger.addHandler(fh)
init_logger.addHandler(ch)
#if __name__ is not 'main':
# init_global_logger()
# LOGGER = logging.getLogger('Discord_Radio_Bot')