From 06d504c8de24189c4636513257d2e801c9c2403e Mon Sep 17 00:00:00 2001 From: sgrtye Date: Sat, 11 Jan 2025 22:36:58 +0000 Subject: [PATCH] edit logging logic --- apiserver/main.py | 6 +++--- shadowgate/main.py | 17 +++++++++++------ shadowgate/mitce.py | 10 +++++----- shadowgate/subscription.py | 1 + shadowgate/xui.py | 8 +++++--- telebot/main.py | 17 +++++++++++------ template/main.py | 15 +++++++++++---- 7 files changed, 47 insertions(+), 27 deletions(-) diff --git a/apiserver/main.py b/apiserver/main.py index 524e77b..7042fc3 100644 --- a/apiserver/main.py +++ b/apiserver/main.py @@ -24,11 +24,11 @@ console_handler = logging.StreamHandler() console_handler.setLevel(logging.INFO) formatter = logging.Formatter( - fmt="%(asctime)s - %(levelname)s - %(message)s", - datefmt="%Y-%m-%d %H:%M:%S" + fmt="%(asctime)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S" ) console_handler.setFormatter(formatter) logger.addHandler(console_handler) +logger.propagate = False XUI_URL: str | None = os.environ.get("XUI_URL") XUI_USERNAME: str | None = os.environ.get("XUI_USERNAME") @@ -246,7 +246,7 @@ def get_info_by_ticker(tickers: str) -> dict[str, str]: info[ticker + TREND_ENDING] = format_number(trend) except Exception as e: - logging.error( + logger.error( f"Error {repr(e)} occurred on line {e.__traceback__.tb_lineno}" ) diff --git a/shadowgate/main.py b/shadowgate/main.py index 000c90d..5eafc51 100644 --- a/shadowgate/main.py +++ b/shadowgate/main.py @@ -1,10 +1,15 @@ import logging -logging.basicConfig( - level=logging.INFO, - format="%(asctime)s - %(levelname)s - %(message)s", - datefmt="%Y-%m-%d %H:%M:%S", +logger = logging.getLogger("my_app") +logger.setLevel(logging.INFO) +console_handler = logging.StreamHandler() +console_handler.setLevel(logging.INFO) +formatter = logging.Formatter( + fmt="%(asctime)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S" ) +console_handler.setFormatter(formatter) +logger.addHandler(console_handler) +logger.propagate = False import os import asyncio @@ -30,7 +35,7 @@ XUI_PASSWORD: str | None = os.environ.get("XUI_PASSWORD") if HOST_DOMAIN is None: - logging.critical("HOST_DOMAIN not provided") + logger.critical("HOST_DOMAIN not provided") raise SystemExit(0) REQUEST_METHODS = ["GET", "POST", "PUT", "DELETE", "PATCH"] @@ -181,7 +186,7 @@ async def main() -> None: await add_api_routes() schedule_config_updates() - logging.info("Starting API server") + logger.info("Starting API server") await start_api_server() diff --git a/shadowgate/mitce.py b/shadowgate/mitce.py index 58848b0..3bdfacb 100644 --- a/shadowgate/mitce.py +++ b/shadowgate/mitce.py @@ -5,11 +5,11 @@ from constants import * MITCE_URL: str | None = os.environ.get("MITCE_URL") - +logger = logging.getLogger("my_app") async def update_mitce_config(): if MITCE_URL is None: - logging.error("MITCE_URL not provided") + logger.error("MITCE_URL not provided") return try: @@ -19,7 +19,7 @@ async def update_mitce_config(): ) if shadowrocket_response.status_code != 200: - logging.error("Shadowrocket config failed to update") + logger.error("Shadowrocket config failed to update") return os.makedirs(os.path.dirname(MITCE_SHADOWROCKET_PATH), exist_ok=True) @@ -32,7 +32,7 @@ async def update_mitce_config(): ) if clash_response.status_code != 200: - logging.error("Clash config failed to update") + logger.error("Clash config failed to update") return os.makedirs(os.path.dirname(MITCE_CLASH_PATH), exist_ok=True) @@ -44,7 +44,7 @@ async def update_mitce_config(): user_info = clash_response.headers.get("subscription-userinfo", "") file.write(user_info) - logging.info("New mitce config files fetched") + logger.info("New mitce config files fetched") except Exception: pass diff --git a/shadowgate/subscription.py b/shadowgate/subscription.py index ff1a9f8..a8f9e1c 100644 --- a/shadowgate/subscription.py +++ b/shadowgate/subscription.py @@ -12,6 +12,7 @@ logger = logging.getLogger("config_access") logger.setLevel(logging.INFO) handler = TimedRotatingFileHandler(CONFIG_ACCESS_LOG_PATH, when="W0", backupCount=4) +handler.setLevel(logging.INFO) formatter = logging.Formatter("%(asctime)s - %(message)s", "%Y-%m-%d %H:%M:%S") handler.setFormatter(formatter) logger.addHandler(handler) diff --git a/shadowgate/xui.py b/shadowgate/xui.py index 98d49da..b9f196d 100644 --- a/shadowgate/xui.py +++ b/shadowgate/xui.py @@ -5,6 +5,8 @@ import asyncio import logging +logger = logging.getLogger("my_app") + PROXY_HOST: str | None = os.environ.get("PROXY_HOST") PROXY_PORT: str | None = os.environ.get("PROXY_PORT") PROXY_PATH: str | None = os.environ.get("PROXY_PATH") @@ -18,7 +20,7 @@ or XUI_USERNAME is None or XUI_PASSWORD is None ): - logging.critical("Environment variables not fulfilled") + logger.critical("Environment variables not fulfilled") raise SystemExit(0) xui_session = httpx.AsyncClient() @@ -64,7 +66,7 @@ async def get_inbounds() -> list[dict[str, str]]: return results except Exception: - logging.critical("Failed to parse inbounds") + logger.critical("Failed to parse inbounds") return [] @@ -87,7 +89,7 @@ async def get_clients() -> list[dict[str, str]]: return results except Exception: - logging.error("Failed to parse clients") + logger.error("Failed to parse clients") return [] diff --git a/telebot/main.py b/telebot/main.py index de62842..43b0390 100644 --- a/telebot/main.py +++ b/telebot/main.py @@ -6,18 +6,23 @@ import logging -logging.basicConfig( - level=logging.INFO, - format="%(asctime)s - %(levelname)s - %(message)s", - datefmt="%Y-%m-%d %H:%M:%S", +logger = logging.getLogger("my_app") +logger.setLevel(logging.INFO) +console_handler = logging.StreamHandler() +console_handler.setLevel(logging.INFO) +formatter = logging.Formatter( + fmt="%(asctime)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S" ) +console_handler.setFormatter(formatter) +logger.addHandler(console_handler) +logger.propagate = False NOVEL_URL: str | None = os.environ.get("NOVEL_URL") GLANCES_URL: str | None = os.environ.get("GLANCES_URL") TELEBOT_TOKEN: str | None = os.environ.get("TELEBOT_TOKEN") if NOVEL_URL is None or GLANCES_URL is None or TELEBOT_TOKEN is None: - logging.critical("Environment variables not fulfilled") + logger.critical("Environment variables not fulfilled") raise SystemExit(0) bot = telebot.TeleBot(TELEBOT_TOKEN) @@ -139,7 +144,7 @@ def main() -> None: telebot.types.BotCommand("restore", "Restart all exited containers"), ] - logging.info("Telegram bot started") + logger.info("Telegram bot started") bot.set_my_commands(commands) bot.infinity_polling(logger_level=None) diff --git a/template/main.py b/template/main.py index a62faae..9609e5a 100644 --- a/template/main.py +++ b/template/main.py @@ -5,11 +5,16 @@ import logging -logging.basicConfig( - level=logging.INFO, - format="%(asctime)s - %(levelname)s - %(message)s", - datefmt="%Y-%m-%d %H:%M:%S", +logger = logging.getLogger("my_app") +logger.setLevel(logging.INFO) +console_handler = logging.StreamHandler() +console_handler.setLevel(logging.INFO) +formatter = logging.Formatter( + fmt="%(asctime)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S" ) +console_handler.setFormatter(formatter) +logger.addHandler(console_handler) +logger.propagate = False lastUpdatedTime = time.time() @@ -43,5 +48,7 @@ def start_health_server(): health_thread.daemon = True health_thread.start() +logger.info("Starting") + while True: time.sleep(10)