Merged
Conversation
coleleavitt
reviewed
Jan 8, 2024
Contributor
coleleavitt
left a comment
There was a problem hiding this comment.
class TuxLogger(logging.Logger):
def __init__(self, name, project_logging_level=logging.INFO):
super().__init__(name, level=project_logging_level)
self.__setup_logging()
self.file_handlers = {}
def __setup_logging(self):
log_format = "%(asctime)s [%(log_color)s%(levelname)s%(reset)s] [%(name)s]: %(message)s"
handler = colorlog.StreamHandler()
handler.setFormatter(colorlog.ColoredFormatter(log_format))
self.addHandler(handler)
file_name = os.path.join("logs", "bot.log")
file_handler = self.__create_file_handler(file_name)
self.addHandler(file_handler)
def __create_file_handler(self, filename):
file_handler = logging.FileHandler(filename, mode="a")
file_handler.setFormatter(
logging.Formatter("%(asctime)s [%(levelname)s] [%(name)s]: %(message)s")
)
return file_handler
def __get_file_handler(self, module_name):
if module_name not in self.file_handlers:
file_name = os.path.join("logs", f"{module_name}.log")
self.file_handlers[module_name] = self.__create_file_handler(file_name)
return self.file_handlers[module_name]
def __log_to_file(self, level, message, caller_module):
file_handler = self.__get_file_handler(caller_module)
self.addHandler(file_handler)
self.log(level, message)
self.removeHandler(file_handler)
def debug(self, message, filename="unknown"):
self.__log_to_file(logging.DEBUG, message, filename)
def info(self, message, filename="unknown"):
self.__log_to_file(logging.INFO, message, filename)
def warning(self, message, filename="unknown"):
self.__log_to_file(logging.WARNING, message, filename)
def error(self, message, filename="unknown"):
self.__log_to_file(logging.ERROR, message, filename)
def critical(self, message, filename="unknown"):
self.__log_to_file(logging.CRITICAL, message, filename)
class LoggingCog(commands.Cog):
def __init__(self, bot, discord_logging_level=logging.WARNING):
self.bot = bot
discord_logger = logging.getLogger("discord")
discord_logger.setLevel(discord_logging_level)
logger = TuxLogger(__name__)
async def setup(bot, project_logging_level=logging.DEBUG, discord_logging_level=logging.WARNING):
global logger
log_cog = LoggingCog(bot, discord_logging_level)
logger.setLevel(project_logging_level)
await bot.add_cog(log_cog)```
broke down the logging function and renamed the functions to call:
UpperCaseCamelCase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.