add log.py and set logger handlers correctly in all files.

This commit is contained in:
2021-08-11 20:02:47 +02:00
parent 6e5d8261ff
commit 2d69abbd37
8 changed files with 67 additions and 56 deletions

View File

@@ -1,10 +1,11 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
import log from logging import getLogger
# setup logging # setup logging
logger = log.get_logger(__name__) log = getLogger(__name__)
embed = discord.Embed() embed = discord.Embed()
@@ -20,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,11 +2,11 @@ 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
import log from logging import getLogger
# setup logging # setup logging
logger = log.get_logger(__name__) log = getLogger(__name__)
e = discord.Embed() e = discord.Embed()
@@ -27,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)
@@ -41,7 +41,7 @@ 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):

View File

@@ -2,11 +2,11 @@ import discord
from discord.ext import commands from discord.ext import commands
import logging import logging
from jeevesbot import functions from jeevesbot import functions
import log from logging import getLogger
# setup logging # setup logging
logger = log.get_logger(__name__) log = getLogger(__name__)
e = discord.Embed() e = discord.Embed()
@@ -33,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)
@@ -43,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)
@@ -55,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,10 +1,10 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
import log from logging import getLogger
# setup logging # setup logging
logger = log.get_logger(__name__) log = getLogger(__name__)
e = discord.Embed() e = discord.Embed()
@@ -29,7 +29,7 @@ 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):

View File

@@ -1,10 +1,10 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
import log from logging import getLogger
# setup logging # setup logging
logger = log.get_logger(__name__) log = getLogger(__name__)
e = discord.Embed() e = discord.Embed()
@@ -30,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,10 +1,10 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
import log from logging import getLogger
# setup logging # setup logging
logger = log.get_logger(__name__) log = getLogger(__name__)
e = discord.Embed() e = discord.Embed()
@@ -19,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

@@ -5,10 +5,16 @@ from discord.ext import commands
from jeevesbot import env from jeevesbot import env
import os import os
import log import log
import logging.config
from logging import getLogger
# setup root logger handlers
logging.config.dictConfig(log.LOGGING)
# setup logging # setup logging
logger = log.get_logger(__name__) log = getLogger(__name__)
# setup discord.py bot # setup discord.py bot
@@ -21,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()
@@ -36,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

60
log.py
View File

@@ -1,28 +1,32 @@
import logging LOGGING = {
import sys 'version': 1,
'disable_existing_loggers': False,
FORMATTER = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") 'formatters': {
LOG_FILE = "jeeves.log" 'standard': {
'format': '[%(asctime)s - %(name)s - %(levelname)s - %(message)s'
def get_console_handler(): }
console_handler = logging.StreamHandler(sys.stdout) },
console_handler.setFormatter(FORMATTER) 'handlers': {
return console_handler 'console': {
'level': 'INFO',
def get_file_handler(): 'class': 'logging.StreamHandler',
file_handler = logging.FileHandler(LOG_FILE, encoding='utf-8', mode='a') 'formatter': 'standard',
file_handler.setFormatter(FORMATTER) 'stream': 'ext://sys.stdout'
return file_handler },
'file': {
def get_logger(logger_name): 'level' : 'INFO',
logger = logging.getLogger(logger_name) 'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'standard',
logger.setLevel(logging.DEBUG) # better to have too much log than not enough 'filename': 'jeeves.log',
'maxBytes': 4096,
logger.addHandler(get_console_handler()) 'backupCount': 7
logger.addHandler(get_file_handler()) }
},
# with this pattern, it's rarely necessary to propagate the error up to parent 'loggers': {
logger.propagate = False '': { # root logger
'handlers': ['console','file'],
return logger 'level': 'DEBUG',
'propagate': False
}
}
}