diff --git a/Log4p/DecoratorsTools.py b/Log4p/DecoratorsTools.py new file mode 100644 index 0000000..82ea6bb --- /dev/null +++ b/Log4p/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/HttpHander.py b/Log4p/HttpHander.py new file mode 100644 index 0000000..4b9ddf7 --- /dev/null +++ b/Log4p/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/__init__.py b/Log4p/__init__.py new file mode 100644 index 0000000..06d7405 Binary files /dev/null and b/Log4p/__init__.py differ diff --git a/Log4p/websocketHander.py b/Log4p/websocketHander.py new file mode 100644 index 0000000..93d5ed9 --- /dev/null +++ b/Log4p/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