added logger

This commit is contained in:
phixxy 2024-01-25 01:38:15 -08:00
parent 2cf17d021c
commit 4425afff8d
2 changed files with 49 additions and 4 deletions

View file

@ -1,15 +1,17 @@
import discord import discord
from discord.ext import commands, tasks from discord.ext import commands, tasks
from discord.utils import get
import shutil import shutil
import time import time
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
import aiohttp import aiohttp
import src.logger as logger
load_dotenv() load_dotenv()
discord_token = os.getenv('discord_token') discord_token = os.getenv('discord_token')
logger = logger.logging.getLogger("bot")
intents = discord.Intents.all() intents = discord.Intents.all()
intents.message_content = True intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents) bot = commands.Bot(command_prefix='!', intents=intents)
@ -35,8 +37,9 @@ async def task_loop():
if current_time.tm_hour == 0 and current_time.tm_min == 0 and current_time.tm_sec == 0: if current_time.tm_hour == 0 and current_time.tm_min == 0 and current_time.tm_sec == 0:
try: try:
await delete_all_files("tmp/") await delete_all_files("tmp/")
logger.info("Deleted tmp/ files.")
except Exception as error: except Exception as error:
print("Failed to delete_all_files") logger.exception("Failed to delete files!")
async def create_session(): async def create_session():
return aiohttp.ClientSession() return aiohttp.ClientSession()
@ -63,11 +66,11 @@ async def on_ready():
for plugin_file in os.listdir('extensions/'): for plugin_file in os.listdir('extensions/'):
if plugin_file[0] != '_' and plugin_file[-3:] == '.py': if plugin_file[0] != '_' and plugin_file[-3:] == '.py':
await bot.load_extension(f'extensions.{plugin_file[:-3]}') await bot.load_extension(f'extensions.{plugin_file[:-3]}')
print('We have logged in as {0.user}'.format(bot)) logger.info('We have logged in as {0.user}'.format(bot))
task_loop.start() task_loop.start()
@bot.event @bot.event
async def on_message(ctx): async def on_message(ctx):
await bot.process_commands(ctx) await bot.process_commands(ctx)
bot.run(discord_token) bot.run(discord_token, root_logger=True)

42
src/logger.py Normal file
View file

@ -0,0 +1,42 @@
import logging
from logging.config import dictConfig
LOGGING_CONFIG = {
"version": 1,
"disabled_existing_loggers": False,
"formatters": {
"verbose": {
"format": "%(levelname)-10s - %(asctime)s - %(module)-15s : %(message)s"
},
"standard": {"format": "%(levelname)-10s - %(name)-15s : %(message)s"},
},
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "standard",
},
"console2": {
"level": "WARNING",
"class": "logging.StreamHandler",
"formatter": "standard",
},
"file": {
"level": "INFO",
"class": "logging.FileHandler",
"filename": "logs/info.log",
"mode": "a",
"formatter": "verbose",
},
},
"loggers": {
"bot": {"handlers": ["console", "file"], "level": "INFO", "propagate": False},
"discord": {
"handlers": ["console2", "file"],
"level": "INFO",
"propagate": False,
},
},
}
dictConfig(LOGGING_CONFIG)