From 4f88ab21add4a2e4f394d65fa7aad903e02aabc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=99=E5=AE=81=E5=A8=9C?= <3072252442@qq.com> Date: Sun, 17 Mar 2024 14:08:54 +0000 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20Log4p/plugins?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Log4p/plugins/DecoratorsTools.py | 11 ++++++++++ Log4p/plugins/HttpHander.py | 35 +++++++++++++++++++++++++++++++ Log4p/plugins/__init__.py | Bin 0 -> 1024 bytes Log4p/plugins/websocketHander.py | 28 +++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 Log4p/plugins/DecoratorsTools.py create mode 100644 Log4p/plugins/HttpHander.py create mode 100644 Log4p/plugins/__init__.py create mode 100644 Log4p/plugins/websocketHander.py diff --git a/Log4p/plugins/DecoratorsTools.py b/Log4p/plugins/DecoratorsTools.py new file mode 100644 index 0000000..82ea6bb --- /dev/null +++ b/Log4p/plugins/DecoratorsTools.py @@ -0,0 +1,11 @@ +import warnings + +def deprecated(use_func, d_version): + def decorator(func): + async def wrapper(*args, **kwargs): + warnings.warn(f"The '{func.__name__}' function is deprecated, " + f"use '{use_func}' instead. Deprecated in version {d_version}.", + DeprecationWarning, stacklevel=2) + return await func(*args, **kwargs) + return wrapper + return decorator diff --git a/Log4p/plugins/HttpHander.py b/Log4p/plugins/HttpHander.py new file mode 100644 index 0000000..4b9ddf7 --- /dev/null +++ b/Log4p/plugins/HttpHander.py @@ -0,0 +1,35 @@ +import logging +import requests +import httpx + +class HTTPhandler(logging.Handler): + def __init__(self, url): + super().__init__() + self.url = url + + def emit(self, record): + log_entry = self.format(record) + payload = {'log': log_entry} + try: + response = requests.post(self.url, json=payload) + if not response.ok: + raise ValueError(response.text) + except Exception as e: + logging.error("Failed to send log to %s: %s", self.url, e) + +class AsyncHTTPhandler(logging.Handler): + def __init__(self, url): + super().__init__() + self.url = url + + async def emit(self, record): + log_entry = self.format(record) + payload = {'log': log_entry} + try: + async with httpx.AsyncClient(timeout=120,max_redirects=5) as client: + response = await client.post(self.url, json=payload) + if not response.is_success: + raise ValueError(await response.text()) + except Exception as e: + logging.error("Failed to send log to %s: %s", self.url, e) + diff --git a/Log4p/plugins/__init__.py b/Log4p/plugins/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..06d7405020018ddf3cacee90fd4af10487da3d20 GIT binary patch literal 1024 ScmZQz7zLvtFd70QH3R?z00031 literal 0 HcmV?d00001 diff --git a/Log4p/plugins/websocketHander.py b/Log4p/plugins/websocketHander.py new file mode 100644 index 0000000..93d5ed9 --- /dev/null +++ b/Log4p/plugins/websocketHander.py @@ -0,0 +1,28 @@ +import logging +import asyncio +import websockets + +class WebsocketHandler(logging.Handler): + def __init__(self, server_address): + super().__init__() + self.server_address = server_address + + async def send_log_async(self, message): + async with websockets.connect(self.server_address) as websocket: + await websocket.send(message) + + def send_log_sync(self, message): + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + loop.run_until_complete(self.send_log_async(message)) + + def emit(self, record): + log_entry = self.format(record) + + if asyncio.get_event_loop().is_running(): + asyncio.create_task(self.send_log_async(log_entry)) + else: + try: + self.send_log_sync(log_entry) + except Exception as e: + logging.warning("Failed to send log synchronously: %s", e) \ No newline at end of file