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
from discord.ext import commands
import log
from logging import getLogger
# setup logging
logger = log.get_logger(__name__)
log = getLogger(__name__)
embed = discord.Embed()
@@ -20,12 +21,12 @@ class Admin(commands.Cog):
@commands.is_owner()
async def clear(self, ctx, amount=1):
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()
async def on_ready(self):
print('##### ADMIN module active')
log.info(f'module active')
def setup(bot):

View File

@@ -2,11 +2,11 @@ import discord
from discord.ext import commands
import logging
from jeevesbot import functions, babbelbingo
import log
from logging import getLogger
# setup logging
logger = log.get_logger(__name__)
log = getLogger(__name__)
e = discord.Embed()
@@ -27,7 +27,7 @@ class Games(commands.Cog):
roll,result = functions.roll(param)
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)
logger.info(logline)
log.info(logline)
await ctx.send(msg)
@@ -41,7 +41,7 @@ class Games(commands.Cog):
@commands.Cog.listener()
async def on_ready(self):
print('##### GAMES module active')
log.info(f'module active')
def setup(bot):

View File

@@ -2,11 +2,11 @@ import discord
from discord.ext import commands
import logging
from jeevesbot import functions
import log
from logging import getLogger
# setup logging
logger = log.get_logger(__name__)
log = getLogger(__name__)
e = discord.Embed()
@@ -33,7 +33,7 @@ class Gif(commands.Cog):
if roles is not True:
await message.channel.send(embed=embed)
logline = (str(message.author) + ' requested a gif: ' + str(gif_url))
logger.info(logline)
log.info(logline)
if message.content.endswith('.gif'):
roles = functions.checkrole(message.author.roles)
channel = functions.checkchannel(message.channel.id)
@@ -43,7 +43,7 @@ class Gif(commands.Cog):
if roles is not True:
await message.channel.send(embed=embed)
logline = (str(message.author) + ' requested a gif: ' + str(embed_url))
logger.info(logline)
log.info(logline)
if message.content.startswith('https://giphy.com/'):
roles = functions.checkrole(message.author.roles)
channel = functions.checkchannel(message.channel.id)
@@ -55,12 +55,12 @@ class Gif(commands.Cog):
if roles is not True:
await message.channel.send(embed=embed)
logline = (str(message.author) + ' requested a gif: ' + str(gif_url))
logger.info(logline)
log.info(logline)
@commands.Cog.listener()
async def on_ready(self):
print('##### GIF module active')
log.info(f'module active')
def setup(bot):

View File

@@ -1,10 +1,10 @@
import discord
from discord.ext import commands
import log
from logging import getLogger
# setup logging
logger = log.get_logger(__name__)
log = getLogger(__name__)
e = discord.Embed()
@@ -29,7 +29,7 @@ class Links(commands.Cog):
@commands.Cog.listener()
async def on_ready(self):
print('##### LINKS module active')
log.info(f'module active')
def setup(bot):

View File

@@ -1,10 +1,10 @@
import discord
from discord.ext import commands
import log
from logging import getLogger
# setup logging
logger = log.get_logger(__name__)
log = getLogger(__name__)
e = discord.Embed()
@@ -30,7 +30,7 @@ class Misc(commands.Cog):
@commands.Cog.listener()
async def on_ready(self):
print('##### RANDOM module active')
log.info(f'module active')
def setup(bot):

View File

@@ -1,10 +1,10 @@
import discord
from discord.ext import commands
import log
from logging import getLogger
# setup logging
logger = log.get_logger(__name__)
log = getLogger(__name__)
e = discord.Embed()
@@ -19,8 +19,8 @@ class Moderator(commands.Cog):
@commands.Cog.listener()
async def on_ready(self):
print('##### MODERATOR module active')
log.info(f'module active')
def setup(bot):
bot.add_cog(Moderator(bot))

View File

@@ -5,10 +5,16 @@ from discord.ext import commands
from jeevesbot import env
import os
import log
import logging.config
from logging import getLogger
# setup root logger handlers
logging.config.dictConfig(log.LOGGING)
# setup logging
logger = log.get_logger(__name__)
log = getLogger(__name__)
# setup discord.py bot
@@ -21,14 +27,14 @@ e = discord.Embed()
@commands.has_permissions(administrator=True)
async def load(ctx, extension):
bot.load_extension(f'cogs.{extension}')
print(extension, 'module loaded')
log.info(f'{extension} module loaded')
@bot.command()
@commands.has_permissions(administrator=True)
async def unload(ctx, extension):
bot.unload_extension(f'cogs.{extension}')
print(extension, 'module unloaded')
log.info(f'{extension} module unloaded')
@bot.command()
@@ -36,7 +42,7 @@ async def unload(ctx, extension):
async def reload(ctx, extension):
bot.unload_extension(f'cogs.{extension}')
bot.load_extension(f'cogs.{extension}')
print(extension, 'module reloaded')
log.info(f'{extension} module reloaded')
@bot.event

60
log.py
View File

@@ -1,28 +1,32 @@
import logging
import sys
FORMATTER = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
LOG_FILE = "jeeves.log"
def get_console_handler():
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(FORMATTER)
return console_handler
def get_file_handler():
file_handler = logging.FileHandler(LOG_FILE, encoding='utf-8', mode='a')
file_handler.setFormatter(FORMATTER)
return file_handler
def get_logger(logger_name):
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG) # better to have too much log than not enough
logger.addHandler(get_console_handler())
logger.addHandler(get_file_handler())
# with this pattern, it's rarely necessary to propagate the error up to parent
logger.propagate = False
return logger
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
}
}
}