a3a26f2ba4
Added discord commands (issue: as they're running over the event hub, they're processed on the same track as other events. => activity ingame triggers the next run; otherwise there's some delay for responses) relay is a standard module now.
47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
import logging
|
|
import logging.handlers
|
|
import re
|
|
import traceback
|
|
|
|
|
|
class Logger:
|
|
def __init__(self, name):
|
|
self.logger = logging.getLogger(name)
|
|
|
|
def warning(self, msg, obj: Exception = None):
|
|
self.logger.warning(self.format_message(msg, obj))
|
|
|
|
def info(self, msg, obj: Exception = None):
|
|
self.logger.info(self.format_message(msg, obj))
|
|
|
|
def error(self, msg, obj: Exception = None):
|
|
self.logger.error(self.format_message(msg, obj))
|
|
|
|
def debug(self, msg, obj: Exception = None):
|
|
self.logger.debug(self.format_message(msg, obj))
|
|
|
|
def log_chat(self, conn_id, channel, sender, msg):
|
|
if sender:
|
|
self.info(f"({conn_id}) [{channel}] {sender}: {self.format_chat_message(msg)}")
|
|
else:
|
|
self.info(f"({conn_id}) [{channel}] {self.format_chat_message(msg)}")
|
|
|
|
def log_tell(self, conn_id, direction, sender, msg):
|
|
self.info(f"({conn_id}) {direction.capitalize()} {sender}: {self.format_chat_message(msg)}")
|
|
|
|
def format_chat_message(self, msg):
|
|
msg = re.sub(r"<a\s+href=\".+?[^\\]\">", "[link]", msg, flags=re.UNICODE | re.DOTALL)
|
|
msg = re.sub(r"<a\s+href='.+?'>", "[link]", msg, flags=re.UNICODE | re.DOTALL)
|
|
msg = re.sub(r"<a\s+href=user://\w+?>(\w+?)</a>", "\1", msg, flags=re.UNICODE | re.DOTALL)
|
|
|
|
msg = re.sub(r"<font\s+.+?>", "", msg, flags=re.UNICODE)
|
|
msg = re.sub("</font>", "", msg, flags=re.UNICODE)
|
|
msg = re.sub("</a>", "[/link]", msg, flags=re.UNICODE)
|
|
return msg
|
|
|
|
def format_message(self, msg, obj):
|
|
if obj:
|
|
return msg + "\n" + traceback.format_exc()
|
|
else:
|
|
return msg
|