Merge pull request #27 from pvanarkel/bug/classify_log_output

Bug/classify log output
This commit is contained in:
Peter van Arkel
2021-08-11 20:04:43 +02:00
committed by GitHub
9 changed files with 90 additions and 68 deletions

0
__init__.py Normal file
View File

View File

@@ -1,16 +1,13 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
import logging from logging import getLogger
e = discord.Embed()
# setup logging # setup logging
logging.basicConfig(level=logging.INFO) log = getLogger(__name__)
logger = logging.getLogger('jeeves')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='jeeves.log', encoding='utf-8', mode='a') embed = discord.Embed()
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
class Admin(commands.Cog): class Admin(commands.Cog):
@@ -24,12 +21,12 @@ class Admin(commands.Cog):
@commands.is_owner() @commands.is_owner()
async def clear(self, ctx, amount=1): async def clear(self, ctx, amount=1):
await ctx.channel.purge(limit=amount) await ctx.channel.purge(limit=amount)
logger.warn(f'{ctx.message.author.name} cleared {amount} messages') log.warn(f'{ctx.message.author.name} cleared {amount} messages')
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self):
print('##### ADMIN module active') log.info(f'module active')
def setup(bot): def setup(bot):

View File

@@ -2,16 +2,14 @@ import discord
from discord.ext import commands from discord.ext import commands
import logging import logging
from jeevesbot import functions, babbelbingo from jeevesbot import functions, babbelbingo
from logging import getLogger
e = discord.Embed()
# setup logging # setup logging
logging.basicConfig(level=logging.INFO) log = getLogger(__name__)
logger = logging.getLogger('jeeves')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='jeeves.log', encoding='utf-8', mode='a') e = discord.Embed()
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
class Games(commands.Cog): class Games(commands.Cog):
@@ -29,7 +27,7 @@ class Games(commands.Cog):
roll,result = functions.roll(param) roll,result = functions.roll(param)
msg = f'Rolling %s for {ctx.message.author.mention}: `%s`'.format(roll) % (param,roll) msg = f'Rolling %s for {ctx.message.author.mention}: `%s`'.format(roll) % (param,roll)
logline = f'Rolling %s for {ctx.message.author}: `%s`'.format(roll) % (param,roll) logline = f'Rolling %s for {ctx.message.author}: `%s`'.format(roll) % (param,roll)
logger.info(logline) log.info(logline)
await ctx.send(msg) await ctx.send(msg)
@@ -43,7 +41,8 @@ class Games(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self):
print('##### GAMES module active') log.info(f'module active')
def setup(bot): def setup(bot):
bot.add_cog(Games(bot)) bot.add_cog(Games(bot))

View File

@@ -2,16 +2,14 @@ import discord
from discord.ext import commands from discord.ext import commands
import logging import logging
from jeevesbot import functions from jeevesbot import functions
from logging import getLogger
e = discord.Embed()
# setup logging # setup logging
logging.basicConfig(level=logging.INFO) log = getLogger(__name__)
logger = logging.getLogger('jeeves')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='jeeves.log', encoding='utf-8', mode='a') e = discord.Embed()
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
class Gif(commands.Cog): class Gif(commands.Cog):
@@ -35,7 +33,7 @@ class Gif(commands.Cog):
if roles is not True: if roles is not True:
await message.channel.send(embed=embed) await message.channel.send(embed=embed)
logline = (str(message.author) + ' requested a gif: ' + str(gif_url)) logline = (str(message.author) + ' requested a gif: ' + str(gif_url))
logger.info(logline) log.info(logline)
if message.content.endswith('.gif'): if message.content.endswith('.gif'):
roles = functions.checkrole(message.author.roles) roles = functions.checkrole(message.author.roles)
channel = functions.checkchannel(message.channel.id) channel = functions.checkchannel(message.channel.id)
@@ -45,7 +43,7 @@ class Gif(commands.Cog):
if roles is not True: if roles is not True:
await message.channel.send(embed=embed) await message.channel.send(embed=embed)
logline = (str(message.author) + ' requested a gif: ' + str(embed_url)) logline = (str(message.author) + ' requested a gif: ' + str(embed_url))
logger.info(logline) log.info(logline)
if message.content.startswith('https://giphy.com/'): if message.content.startswith('https://giphy.com/'):
roles = functions.checkrole(message.author.roles) roles = functions.checkrole(message.author.roles)
channel = functions.checkchannel(message.channel.id) channel = functions.checkchannel(message.channel.id)
@@ -57,12 +55,12 @@ class Gif(commands.Cog):
if roles is not True: if roles is not True:
await message.channel.send(embed=embed) await message.channel.send(embed=embed)
logline = (str(message.author) + ' requested a gif: ' + str(gif_url)) logline = (str(message.author) + ' requested a gif: ' + str(gif_url))
logger.info(logline) log.info(logline)
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self):
print('##### GIF module active') log.info(f'module active')
def setup(bot): def setup(bot):

View File

@@ -1,16 +1,13 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
import logging from logging import getLogger
e = discord.Embed()
# setup logging # setup logging
logging.basicConfig(level=logging.INFO) log = getLogger(__name__)
logger = logging.getLogger('jeeves')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='jeeves.log', encoding='utf-8', mode='a') e = discord.Embed()
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
class Links(commands.Cog): class Links(commands.Cog):
@@ -32,7 +29,8 @@ class Links(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self):
print('##### LINKS module active') log.info(f'module active')
def setup(bot): def setup(bot):
bot.add_cog(Links(bot)) bot.add_cog(Links(bot))

View File

@@ -1,16 +1,13 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
import logging from logging import getLogger
e = discord.Embed()
# setup logging # setup logging
logging.basicConfig(level=logging.INFO) log = getLogger(__name__)
logger = logging.getLogger('jeeves')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='jeeves.log', encoding='utf-8', mode='a') e = discord.Embed()
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
class Misc(commands.Cog): class Misc(commands.Cog):
@@ -33,7 +30,7 @@ class Misc(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self):
print('##### RANDOM module active') log.info(f'module active')
def setup(bot): def setup(bot):

View File

@@ -1,16 +1,13 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
import logging from logging import getLogger
e = discord.Embed()
# setup logging # setup logging
logging.basicConfig(level=logging.INFO) log = getLogger(__name__)
logger = logging.getLogger('jeeves')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='jeeves.log', encoding='utf-8', mode='a') e = discord.Embed()
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
class Moderator(commands.Cog): class Moderator(commands.Cog):
@@ -22,8 +19,8 @@ class Moderator(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self):
print('##### MODERATOR module active') log.info(f'module active')
def setup(bot): def setup(bot):
bot.add_cog(Moderator(bot)) bot.add_cog(Moderator(bot))

View File

@@ -2,17 +2,20 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
import logging
from jeevesbot import env from jeevesbot import env
import os import os
import log
import logging.config
from logging import getLogger
# setup root logger handlers
logging.config.dictConfig(log.LOGGING)
# setup logging # setup logging
logging.basicConfig(level=logging.INFO) log = getLogger(__name__)
logger = logging.getLogger('jeeves')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='jeeves.log', encoding='utf-8', mode='a')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
# setup discord.py bot # setup discord.py bot
intents = discord.Intents().all() intents = discord.Intents().all()
@@ -24,14 +27,14 @@ e = discord.Embed()
@commands.has_permissions(administrator=True) @commands.has_permissions(administrator=True)
async def load(ctx, extension): async def load(ctx, extension):
bot.load_extension(f'cogs.{extension}') bot.load_extension(f'cogs.{extension}')
print(extension, 'module loaded') log.info(f'{extension} module loaded')
@bot.command() @bot.command()
@commands.has_permissions(administrator=True) @commands.has_permissions(administrator=True)
async def unload(ctx, extension): async def unload(ctx, extension):
bot.unload_extension(f'cogs.{extension}') bot.unload_extension(f'cogs.{extension}')
print(extension, 'module unloaded') log.info(f'{extension} module unloaded')
@bot.command() @bot.command()
@@ -39,7 +42,7 @@ async def unload(ctx, extension):
async def reload(ctx, extension): async def reload(ctx, extension):
bot.unload_extension(f'cogs.{extension}') bot.unload_extension(f'cogs.{extension}')
bot.load_extension(f'cogs.{extension}') bot.load_extension(f'cogs.{extension}')
print(extension, 'module reloaded') log.info(f'{extension} module reloaded')
@bot.event @bot.event
@@ -56,3 +59,4 @@ for filename in os.listdir('./cogs'):
if __name__ == '__main__': if __name__ == '__main__':
bot.run(env.TOKEN) bot.run(env.TOKEN)

32
log.py Normal file
View File

@@ -0,0 +1,32 @@
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '[%(asctime)s - %(name)s - %(levelname)s - %(message)s'
}
},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'standard',
'stream': 'ext://sys.stdout'
},
'file': {
'level' : 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'standard',
'filename': 'jeeves.log',
'maxBytes': 4096,
'backupCount': 7
}
},
'loggers': {
'': { # root logger
'handlers': ['console','file'],
'level': 'DEBUG',
'propagate': False
}
}
}