Skip to content
Snippets Groups Projects
Commit 3abc627e authored by Tulir Asokan's avatar Tulir Asokan :cat2:
Browse files

Add proper log coloring

parent 7ef7706a
No related branches found
No related tags found
No related merge requests found
......@@ -73,18 +73,21 @@ bridge:
logging:
version: 1
formatters:
precise:
colored:
(): mautrix_facebook.util.ColorFormatter
format: "[%(asctime)s] [%(levelname)s@%(name)s] %(message)s"
normal:
format: "[%(asctime)s] [%(levelname)s@%(name)s] %(message)s"
handlers:
file:
class: logging.handlers.RotatingFileHandler
formatter: precise
formatter: normal
filename: ./mautrix-facebook.log
maxBytes: 10485760
backupCount: 10
console:
class: logging.StreamHandler
formatter: precise
formatter: colored
loggers:
mau:
level: DEBUG
......
......@@ -17,7 +17,6 @@ from typing import Any, Dict, Iterator, Optional, TYPE_CHECKING
from http.cookies import SimpleCookie
import asyncio
import logging
import os
from fbchat import Client, Message, ThreadType, User as FBUser
from mautrix.types import UserID
......@@ -33,16 +32,11 @@ if TYPE_CHECKING:
config: Config
GREEN = "\u001b[32m"
YELLOW = "\u001b[33m"
MAGENTA = "\u001b[35m"
RESET = "\u001b[0m"
class User(Client):
az: AppService
loop: asyncio.AbstractEventLoop
log: logging.Logger = logging.getLogger(f"{GREEN}mau.user{RESET}")
log: logging.Logger = logging.getLogger("mau.user")
by_mxid: Dict[UserID, 'User'] = {}
command_status: Optional[Dict[str, Any]]
......@@ -63,12 +57,10 @@ class User(Client):
self._session_data = session
self._db_instance = db_instance
log_id = f"{YELLOW}{self.mxid}{RESET}"
self.log = self.log.getChild(log_id)
# TODO non-hacky log coloring
self._log = logging.getLogger(f"{MAGENTA}fbchat.client{RESET}").getChild(log_id)
self._req_log = logging.getLogger(f"{MAGENTA}fbchat.request{RESET}").getChild(log_id)
self._util_log = logging.getLogger(f"{MAGENTA}fbchat.util{RESET}").getChild(log_id)
self.log = self.log.getChild(self.mxid)
self._log = self._log.getChild(self.mxid)
self._req_log = self._req_log.getChild(self.mxid)
self._util_log = self._util_log.getChild(self.mxid)
# region Sessions
......
from .color_log import ColorFormatter
# mautrix-facebook - A Matrix-Facebook Messenger puppeting bridge
# Copyright (C) 2019 Tulir Asokan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from logging import Formatter, LogRecord
from copy import copy
PREFIX = "\033["
LEVEL_COLORS = {
"DEBUG": "37m", # white
"INFO": "36m", # cyan
"WARNING": "33;1m", # yellow
"ERROR": "31;1m", # red
"CRITICAL": "41m", # white on red bg
}
FBCHAT_COLOR = PREFIX + "35;1m" # magenta
MAU_COLOR = PREFIX + "32;1m" # green
AIOHTTP_COLOR = PREFIX + "36;1m" # cyan
MXID_COLOR = PREFIX + "33m" # yellow
RESET = "\033[0m"
class ColorFormatter(Formatter):
def __init__(self, *args):
super().__init__(*args)
def _color_name(self, module: str) -> str:
fbchat = ["fbchat.util", "fbchat.request", "fbchat.client"]
for prefix in fbchat:
if module.startswith(prefix):
return (FBCHAT_COLOR + prefix + RESET
+ "." + MXID_COLOR + module[len(prefix) + 1:] + RESET)
if module.startswith("mau.as"):
return MAU_COLOR + module + RESET
elif module.startswith("mau."):
try:
next_dot = module.index(".", len("mau."))
return (MAU_COLOR + module[:next_dot] + RESET
+ "." + MXID_COLOR + module[next_dot + 1:] + RESET)
except ValueError:
return MAU_COLOR + module + RESET
elif module.startswith("aiohttp"):
return AIOHTTP_COLOR + module + RESET
def format(self, record: LogRecord):
colored_record: LogRecord = copy(record)
colored_record.name = self._color_name(record.name)
try:
levelname = record.levelname
colored_record.levelname = PREFIX + LEVEL_COLORS[levelname] + levelname + RESET
except KeyError:
pass
return super().format(colored_record)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment