0
0
mirror of https://gitflic.ru/project/maks1ms/ocab.git synced 2025-01-11 09:18:11 +03:00

фикс логгирования в hypercorn

This commit is contained in:
Maxim Slipenko 2024-07-21 21:27:57 +03:00
parent 34c365178b
commit 2177c712a3
2 changed files with 30 additions and 17 deletions

View File

@ -1,30 +1,42 @@
import logging
import os
# import time
import traceback
from hypercorn.logging import Logger as HypercornLogger
app_logger = logging.getLogger("ocab")
log_level = logging.INFO
def patch_logger(logger_: logging.Logger):
logger_.handlers = []
formatter = logging.Formatter("%(asctime)s %(message)s", datefmt="%H:%M:%S")
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger_.addHandler(console_handler)
logger_.propagate = False
logger_.setLevel(log_level)
return logger_
def setup_logger():
"""
Настройка логирования
"""
# current_date = time.strftime("%d-%m-%Y")
log_dir = os.path.join(os.path.dirname(__file__), "log")
os.makedirs(log_dir, exist_ok=True)
# log_file = os.path.join(log_dir, f"log-{current_date}.log")
logging.basicConfig(
# filename=log_file,
level=logging.INFO,
format="%(asctime)s %(message)s",
datefmt="%H:%M:%S",
)
patch_logger(app_logger)
def log(message):
if isinstance(message, Exception):
error_message = f"Error: {str(message)}\n{traceback.format_exc()}"
logging.error(error_message)
app_logger.error(error_message)
else:
logging.info(message)
app_logger.info(message)
class CustomLogger(HypercornLogger):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
if self.error_logger:
patch_logger(self.error_logger)
if self.access_logger:
patch_logger(self.access_logger)

View File

@ -7,7 +7,7 @@ from hypercorn.asyncio import serve
from hypercorn.config import Config as HyperConfig
from ocab_core.lib import get_module_directory, register_bot_webhook
from ocab_core.logger import log, setup_logger
from ocab_core.logger import CustomLogger, log, setup_logger
from ocab_core.modules_system import ModulesManager
from ocab_core.modules_system.loaders import FSLoader, UnsafeFSLoader
from ocab_core.singleton import Singleton
@ -60,6 +60,7 @@ async def webhook_mode():
)
config = HyperConfig()
config.bind = ["0.0.0.0:9000"]
config.logger_class = CustomLogger
await serve(app, config)