删除 Log4p/websocketHander.py
This commit is contained in:
parent
7f92741462
commit
34bdabc4db
|
@ -1,28 +0,0 @@
|
||||||
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)
|
|
Loading…
Reference in New Issue
Block a user