mirror of
https://github.com/GaMeNu/HFCNotificator.git
synced 2024-11-16 07:14:52 +02:00
v2.2.7: feat: Improved logging utilities
Changed the default color formatter, and added file logging services for the loggers
This commit is contained in:
parent
a0b3832c25
commit
f532f97e65
@ -2,7 +2,6 @@ import datetime
|
||||
import platform
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
|
||||
import cpuinfo
|
||||
import discord
|
||||
@ -15,6 +14,7 @@ from discord.ext import commands, tasks
|
||||
import botinfo
|
||||
import db_access
|
||||
import errlogging
|
||||
import loggers
|
||||
from alert_maker import AlertEmbed
|
||||
from db_access import *
|
||||
from markdown import md
|
||||
@ -120,6 +120,7 @@ class Notificator(commands.Cog):
|
||||
|
||||
self.log = logging.Logger('Notificator')
|
||||
self.log.addHandler(handler)
|
||||
self.log.addHandler(loggers.DefaultFileHandler("LOG_NOTIFICATOR.log"))
|
||||
|
||||
self.db = DBAccess()
|
||||
|
||||
@ -224,7 +225,6 @@ class Notificator(commands.Cog):
|
||||
# Check if the loop is running multiple too fast or too slow
|
||||
current_time = time.time()
|
||||
delta = round(current_time - self.last_loop_run_time, 3)
|
||||
print(delta)
|
||||
|
||||
if delta < EXPECTED_LOOP_DELTA_MIN:
|
||||
self.log.warning(f'Loop is running too quickly! Expected delta > {EXPECTED_LOOP_DELTA_MIN}s, but got {delta}s. Restarting...')
|
||||
@ -254,7 +254,6 @@ class Notificator(commands.Cog):
|
||||
# handle connection issues
|
||||
self.log.warning("Lost connection!")
|
||||
current_alert = await self.handle_connection_failure()
|
||||
print(current_alert)
|
||||
|
||||
self.log.debug(f'Alert response: {current_alert}')
|
||||
|
||||
@ -317,7 +316,6 @@ class Notificator(commands.Cog):
|
||||
# Attempt to force stupid "Unread Result" down its own throat
|
||||
# and just reset the connection.
|
||||
# I'm not dealing with Unread Results
|
||||
print("DINGUS!!!")
|
||||
self.db.connection.close()
|
||||
self.db = DBAccess()
|
||||
self.start_loop()
|
||||
|
@ -1,7 +1,6 @@
|
||||
import datetime
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
import __main__
|
||||
@ -13,7 +12,7 @@ errlog_dir = botdata_dir.joinpath('errlogs')
|
||||
|
||||
|
||||
class ErrLogger:
|
||||
def __init__(self, handler: logging.Handler=None):
|
||||
def __init__(self, handler: logging.Handler = None):
|
||||
if handler is None:
|
||||
handler = logging.StreamHandler()
|
||||
|
||||
@ -26,7 +25,6 @@ class ErrLogger:
|
||||
time = datetime.datetime.now()
|
||||
path = os.path.join(errlog_dir,
|
||||
f'ERRLOG_{time.strftime("%Y-%m-%d_%H-%M-%S")}.txt')
|
||||
tb_str = ''
|
||||
|
||||
context_ls = list()
|
||||
context_ls.append(e)
|
||||
@ -105,6 +103,3 @@ def errlog(func):
|
||||
|
||||
def async_errlog(func):
|
||||
return ErrLogger().async_errlog(func)
|
||||
|
||||
|
||||
|
||||
|
49
loggers.py
Normal file
49
loggers.py
Normal file
@ -0,0 +1,49 @@
|
||||
import __main__
|
||||
import logging
|
||||
import logging.handlers
|
||||
from pathlib import Path
|
||||
|
||||
import discord
|
||||
|
||||
MAIN_DIR = Path(__main__.__file__).parent
|
||||
BOTDATA_DIR = MAIN_DIR.joinpath('botdata')
|
||||
LOGGING_DIR = BOTDATA_DIR.joinpath('logs')
|
||||
|
||||
|
||||
class ColorFormatter(discord.utils._ColourFormatter):
|
||||
"""
|
||||
Custom formatter based on Discord.py's color formatter, while changing the color for the datetime
|
||||
"""
|
||||
|
||||
LEVEL_COLOURS = [
|
||||
(logging.DEBUG, '\x1b[40;1m', '\x1b[97m'),
|
||||
(logging.INFO, '\x1b[34;1m', '\x1b[97;1m'),
|
||||
(logging.WARNING, '\x1b[33;1m', '\x1b[93m'),
|
||||
(logging.ERROR, '\x1b[31m', '\x1b[91m'),
|
||||
(logging.CRITICAL, '\x1b[41m', '\x1b[41;97;1m'),
|
||||
]
|
||||
FORMATS = {
|
||||
level: logging.Formatter(
|
||||
f'\x1b[37;1m%(asctime)s\x1b[0m {type_color}%(levelname)-8s\x1b[0m \x1b[35m%(name)s\x1b[0m {msg_color}%(message)s\x1b[0m',
|
||||
'%Y-%m-%d %H:%M:%S',
|
||||
)
|
||||
for level, type_color, msg_color in LEVEL_COLOURS
|
||||
}
|
||||
|
||||
|
||||
class DefaultFileHandler(logging.FileHandler):
|
||||
def __init__(self, filename: str):
|
||||
super().__init__(Path(LOGGING_DIR, filename))
|
||||
self.setFormatter(logging.Formatter(
|
||||
"[%(asctime)s] [%(levelname)s] %(name)s: %(message)s",
|
||||
'%Y-%m-%d %H:%M:%S',
|
||||
))
|
||||
self.setLevel(logging.INFO)
|
||||
|
||||
|
||||
def generate_logging_folder():
|
||||
if not BOTDATA_DIR.is_dir():
|
||||
BOTDATA_DIR.mkdir()
|
||||
|
||||
if not LOGGING_DIR.is_dir():
|
||||
LOGGING_DIR.mkdir()
|
12
main.py
12
main.py
@ -1,23 +1,25 @@
|
||||
import sys
|
||||
|
||||
import discord
|
||||
from discord.ext import commands, tasks
|
||||
from discord.ext import commands
|
||||
from dotenv import load_dotenv
|
||||
import gettext
|
||||
import logging
|
||||
import os
|
||||
|
||||
import errlogging
|
||||
import loggers
|
||||
from cog_notificator import Notificator
|
||||
|
||||
# Set up constants and logger
|
||||
logger = logging.Logger('General Log')
|
||||
load_dotenv()
|
||||
TOKEN = os.getenv('TOKEN')
|
||||
AUTHOR_ID = int(os.getenv('AUTHOR_ID'))
|
||||
|
||||
logger = logging.Logger('General Log')
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(loggers.ColorFormatter())
|
||||
logger.addHandler(handler)
|
||||
logger.addHandler(loggers.DefaultFileHandler("LOG_GENERAL.log"))
|
||||
|
||||
bot = commands.Bot('!', intents=discord.Intents.all())
|
||||
tree = bot.tree
|
||||
@ -40,6 +42,7 @@ async def on_ready():
|
||||
await bot.change_presence(activity=discord.Activity(name='for HFC alerts.', type=discord.ActivityType.watching))
|
||||
|
||||
errlogging.generate_errlog_folder()
|
||||
loggers.generate_logging_folder()
|
||||
|
||||
await Notificator.setup(bot, handler)
|
||||
|
||||
@ -55,4 +58,5 @@ async def on_error(event, *args, **kwargs):
|
||||
logger.error('An error has occurred! Check the latest ERRLOG for more info')
|
||||
errlogging.new_errlog(sys.exc_info()[1])
|
||||
|
||||
bot.run(token=TOKEN, log_handler=handler)
|
||||
|
||||
bot.run(token=TOKEN, log_handler=handler, log_formatter=handler.formatter)
|
||||
|
Loading…
Reference in New Issue
Block a user