mirror of
https://gitflic.ru/project/maks1ms/ocab.git
synced 2025-01-11 17:28:12 +03:00
фикс логгирования в hypercorn
This commit is contained in:
parent
34c365178b
commit
2177c712a3
@ -1,30 +1,42 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
|
||||||
|
|
||||||
# import time
|
|
||||||
import traceback
|
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():
|
def setup_logger():
|
||||||
"""
|
"""
|
||||||
Настройка логирования
|
Настройка логирования
|
||||||
"""
|
"""
|
||||||
# current_date = time.strftime("%d-%m-%Y")
|
patch_logger(app_logger)
|
||||||
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",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def log(message):
|
def log(message):
|
||||||
if isinstance(message, Exception):
|
if isinstance(message, Exception):
|
||||||
error_message = f"Error: {str(message)}\n{traceback.format_exc()}"
|
error_message = f"Error: {str(message)}\n{traceback.format_exc()}"
|
||||||
logging.error(error_message)
|
app_logger.error(error_message)
|
||||||
else:
|
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)
|
||||||
|
@ -7,7 +7,7 @@ from hypercorn.asyncio import serve
|
|||||||
from hypercorn.config import Config as HyperConfig
|
from hypercorn.config import Config as HyperConfig
|
||||||
|
|
||||||
from ocab_core.lib import get_module_directory, register_bot_webhook
|
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 import ModulesManager
|
||||||
from ocab_core.modules_system.loaders import FSLoader, UnsafeFSLoader
|
from ocab_core.modules_system.loaders import FSLoader, UnsafeFSLoader
|
||||||
from ocab_core.singleton import Singleton
|
from ocab_core.singleton import Singleton
|
||||||
@ -60,6 +60,7 @@ async def webhook_mode():
|
|||||||
)
|
)
|
||||||
config = HyperConfig()
|
config = HyperConfig()
|
||||||
config.bind = ["0.0.0.0:9000"]
|
config.bind = ["0.0.0.0:9000"]
|
||||||
|
config.logger_class = CustomLogger
|
||||||
await serve(app, config)
|
await serve(app, config)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user