Skip to content

Commit

Permalink
edit logging logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrtye committed Jan 11, 2025
1 parent 4a516b1 commit 06d504c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 27 deletions.
6 changes: 3 additions & 3 deletions apiserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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}"
)

Expand Down
17 changes: 11 additions & 6 deletions shadowgate/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"]
Expand Down Expand Up @@ -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()


Expand Down
10 changes: 5 additions & 5 deletions shadowgate/mitce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions shadowgate/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions shadowgate/xui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand Down Expand Up @@ -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 []


Expand All @@ -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 []


Expand Down
17 changes: 11 additions & 6 deletions telebot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
15 changes: 11 additions & 4 deletions template/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -43,5 +48,7 @@ def start_health_server():
health_thread.daemon = True
health_thread.start()

logger.info("Starting")

while True:
time.sleep(10)

0 comments on commit 06d504c

Please sign in to comment.