logger+fmt+isort
This commit is contained in:
@@ -5,10 +5,9 @@ from aiohttp import ClientSession
|
||||
from starlette.exceptions import HTTPException
|
||||
|
||||
from services.core import get_author_by_user
|
||||
from services.logger import root_logger as logger
|
||||
from settings import AUTH_URL
|
||||
|
||||
|
||||
logger = logging.getLogger("[services.auth] ")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
|
@@ -6,10 +6,9 @@ from typing import List
|
||||
import requests
|
||||
|
||||
from models.member import ChatMember
|
||||
from services.logger import root_logger as logger
|
||||
from settings import API_BASE
|
||||
|
||||
|
||||
logger = logging.getLogger("[services.core] ")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
|
81
services/logger.py
Normal file
81
services/logger.py
Normal file
@@ -0,0 +1,81 @@
|
||||
import logging
|
||||
|
||||
import colorlog
|
||||
|
||||
# Define the color scheme
|
||||
color_scheme = {
|
||||
"DEBUG": "light_black",
|
||||
"INFO": "green",
|
||||
"WARNING": "yellow",
|
||||
"ERROR": "red",
|
||||
"CRITICAL": "red,bg_white",
|
||||
}
|
||||
|
||||
# Define secondary log colors
|
||||
secondary_colors = {
|
||||
"log_name": {"DEBUG": "blue"},
|
||||
"asctime": {"DEBUG": "cyan"},
|
||||
"process": {"DEBUG": "purple"},
|
||||
"module": {"DEBUG": "light_black,bg_blue"},
|
||||
"funcName": {"DEBUG": "light_white,bg_blue"}, # Add this line
|
||||
}
|
||||
|
||||
# Define the log format string
|
||||
fmt_string = "%(log_color)s%(levelname)s: %(log_color)s[%(module)s.%(funcName)s]%(reset)s %(white)s%(message)s"
|
||||
|
||||
# Define formatting configuration
|
||||
fmt_config = {
|
||||
"log_colors": color_scheme,
|
||||
"secondary_log_colors": secondary_colors,
|
||||
"style": "%",
|
||||
"reset": True,
|
||||
}
|
||||
|
||||
|
||||
class MultilineColoredFormatter(colorlog.ColoredFormatter):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.log_colors = kwargs.pop("log_colors", {})
|
||||
self.secondary_log_colors = kwargs.pop("secondary_log_colors", {})
|
||||
|
||||
def format(self, record):
|
||||
message = record.getMessage()
|
||||
if "\n" in message:
|
||||
lines = message.split("\n")
|
||||
first_line = lines[0]
|
||||
record.message = first_line
|
||||
formatted_first_line = super().format(record)
|
||||
formatted_lines = [formatted_first_line]
|
||||
for line in lines[1:]:
|
||||
formatted_lines.append(line)
|
||||
return "\n".join(formatted_lines)
|
||||
else:
|
||||
return super().format(record)
|
||||
|
||||
|
||||
# Create a MultilineColoredFormatter object for colorized logging
|
||||
formatter = MultilineColoredFormatter(fmt_string, **fmt_config)
|
||||
|
||||
# Create a stream handler for logging output
|
||||
stream = logging.StreamHandler()
|
||||
stream.setFormatter(formatter)
|
||||
|
||||
|
||||
def get_colorful_logger(name="main"):
|
||||
# Create and configure the logger
|
||||
logger = logging.getLogger(name)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger.addHandler(stream)
|
||||
|
||||
return logger
|
||||
|
||||
|
||||
# Set up the root logger with the same formatting
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.setLevel(logging.DEBUG)
|
||||
root_logger.addHandler(stream)
|
||||
|
||||
ignore_logs = ["_trace", "httpx", "_client", "_trace.atrace", "aiohttp", "_client"]
|
||||
for lgr in ignore_logs:
|
||||
loggr = logging.getLogger(lgr)
|
||||
loggr.setLevel(logging.INFO)
|
@@ -1,5 +1,7 @@
|
||||
import json
|
||||
|
||||
from servies.logger import root_logger as logger
|
||||
|
||||
from models.chat import ChatUpdate, Message
|
||||
from services.rediscache import redis
|
||||
|
||||
@@ -9,9 +11,9 @@ async def notify_message(message: Message, action="create"):
|
||||
data = {"payload": message, "action": action}
|
||||
try:
|
||||
await redis.publish(channel_name, json.dumps(data))
|
||||
print(f"[services.presence] ok {data}")
|
||||
logger.info(f"ok {data}")
|
||||
except Exception as e:
|
||||
print(f"Failed to publish to channel {channel_name}: {e}")
|
||||
logger.error(f"Failed to publish to channel {channel_name}: {e}")
|
||||
|
||||
|
||||
async def notify_chat(chat: ChatUpdate, member_id: int, action="create"):
|
||||
@@ -19,6 +21,6 @@ async def notify_chat(chat: ChatUpdate, member_id: int, action="create"):
|
||||
data = {"payload": chat, "action": action}
|
||||
try:
|
||||
await redis.publish(channel_name, json.dumps(data))
|
||||
print(f"[services.presence] ok {data}")
|
||||
logger.info(f"ok {data}")
|
||||
except Exception as e:
|
||||
print(f"Failed to publish to channel {channel_name}: {e}")
|
||||
logger.error(f"Failed to publish to channel {channel_name}: {e}")
|
||||
|
@@ -2,10 +2,9 @@ import logging
|
||||
|
||||
import redis.asyncio as aredis
|
||||
|
||||
from services.logger import root_logger as logger
|
||||
from settings import REDIS_URL
|
||||
|
||||
|
||||
logger = logging.getLogger("[services.redis] ")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
|
@@ -1,6 +1,5 @@
|
||||
from ariadne import MutationType, QueryType
|
||||
|
||||
|
||||
query = QueryType()
|
||||
mutation = MutationType()
|
||||
|
||||
|
@@ -3,6 +3,7 @@ from sentry_sdk.integrations.ariadne import AriadneIntegration
|
||||
from sentry_sdk.integrations.redis import RedisIntegration
|
||||
from sentry_sdk.integrations.starlette import StarletteIntegration
|
||||
|
||||
from services.logger import root_logger as logger
|
||||
from settings import GLITCHTIP_DSN
|
||||
|
||||
|
||||
@@ -26,5 +27,4 @@ def start_sentry():
|
||||
],
|
||||
)
|
||||
except Exception as e:
|
||||
print("[services.sentry] init error")
|
||||
print(e)
|
||||
logger.error(e)
|
||||
|
Reference in New Issue
Block a user