logger+fmt+isort
This commit is contained in:
parent
2b3aa43faf
commit
12602ac57c
|
@ -1,3 +1,6 @@
|
|||
[0.3.2]
|
||||
- added custom logger
|
||||
|
||||
[0.3.1]
|
||||
- glitchtip connect
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[tool.poetry]
|
||||
name = "discoursio-inbox"
|
||||
version = "0.3.1"
|
||||
version = "0.3.2"
|
||||
description = "Inbox server for discours.io"
|
||||
authors = ["Tony Rewin <anton.rewin@gmail.com>"]
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@ from resolvers.messages import (
|
|||
)
|
||||
from resolvers.search import search_messages, search_recipients
|
||||
|
||||
|
||||
__all__ = [
|
||||
# inbox
|
||||
"load_chats",
|
||||
|
|
|
@ -9,7 +9,6 @@ from services.presence import notify_chat
|
|||
from services.rediscache import redis
|
||||
from services.schema import mutation
|
||||
|
||||
|
||||
logger = logging.getLogger("[resolvers.chats] ")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
|
|
@ -10,7 +10,6 @@ from services.core import CacheStorage
|
|||
from services.rediscache import redis
|
||||
from services.schema import query
|
||||
|
||||
|
||||
logger = logging.getLogger("[resolvers.load] ")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@ from services.presence import notify_message
|
|||
from services.rediscache import redis
|
||||
from services.schema import mutation
|
||||
|
||||
|
||||
logger = logging.getLogger("[resolvers.messages] ")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ from granian.server import Granian
|
|||
|
||||
from settings import PORT
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("[server] starting...")
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
from os import environ
|
||||
|
||||
|
||||
PORT = 8000
|
||||
REDIS_URL = environ.get("REDIS_URL") or "redis://127.0.0.1"
|
||||
API_BASE = environ.get("API_BASE") or "http://127.0.0.1:8001/"
|
||||
|
|
Loading…
Reference in New Issue
Block a user