fix(logging): Split warning and error logs.

This commit is contained in:
2023-09-18 08:56:57 +03:00
parent a27e07be97
commit c63027f20e
5 changed files with 29 additions and 9 deletions

View File

@@ -35,6 +35,7 @@ FlatContext = namedtuple(
'interaction',
'guild',
'author',
'channel',
'alias',
'prefix',
'failed')

View File

@@ -3,7 +3,7 @@ import configparser as cfgp
from .args import args
shard_number = args.shard or 0
shard_number = args.shard
class configEmoji(PartialEmoji):
__slots__ = ('fallback',)
@@ -87,7 +87,10 @@ class Conf:
"emoji": configEmoji.from_str,
}
)
self.config.read(configfile)
with open(configfile) as conff:
# Opening with read_file mainly to ensure the file exists
self.config.read_file(conff)
self.section_name = section_name if section_name in self.config else 'DEFAULT'

View File

@@ -188,6 +188,14 @@ class LessThanFilter(logging.Filter):
# non-zero return means we log this message
return 1 if record.levelno < self.max_level else 0
class ExactLevelFilter(logging.Filter):
def __init__(self, target_level, name=""):
super().__init__(name)
self.target_level = target_level
def filter(self, record):
return (record.levelno == self.target_level)
class ThreadFilter(logging.Filter):
def __init__(self, thread_name):
@@ -234,7 +242,6 @@ class ContextInjection(logging.Filter):
logging_handler_out = logging.StreamHandler(sys.stdout)
logging_handler_out.setLevel(logging.DEBUG)
logging_handler_out.setFormatter(log_fmt)
logging_handler_out.addFilter(LessThanFilter(logging.WARNING))
logging_handler_out.addFilter(ContextInjection())
logger.addHandler(logging_handler_out)
log_logger.addHandler(logging_handler_out)
@@ -363,14 +370,15 @@ class WebHookHandler(logging.StreamHandler):
return
except BucketFull:
logger.warning(
f"Live logging webhook {self.webhook.id} going too fast! "
"Ignoring records until rate slows down."
"Can't keep up! "
"Ignoring records on live-logger {self.webhook.id}."
)
self.ignored += 1
return
else:
if self.ignored > 0:
logger.warning(
"Can't keep up! "
f"{self.ignored} live logging records on webhook {self.webhook.id} skipped, continuing."
)
self.ignored = 0
@@ -392,9 +400,15 @@ if webhook := conf.logging['general_log']:
handler = WebHookHandler(webhook, batch=True)
handlers.append(handler)
if webhook := conf.logging['warning_log']:
handler = WebHookHandler(webhook, prefix=conf.logging['warning_prefix'], batch=True)
handler.addFilter(ExactLevelFilter(logging.WARNING))
handler.setLevel(logging.WARNING)
handlers.append(handler)
if webhook := conf.logging['error_log']:
handler = WebHookHandler(webhook, prefix=conf.logging['error_prefix'], batch=True)
handler.setLevel(logging.WARNING)
handler.setLevel(logging.ERROR)
handlers.append(handler)
if webhook := conf.logging['critical_log']: