kopia lustrzana https://github.com/cirospaciari/socketify.py
add logging example and remove print to use proper logging in socketify.py and loop.py
rodzic
1881bf8982
commit
8cce143d96
|
@ -0,0 +1,32 @@
|
||||||
|
# This example just show how to use python logging to log requests
|
||||||
|
|
||||||
|
from socketify import App
|
||||||
|
import logging
|
||||||
|
# Setup log format
|
||||||
|
logging.basicConfig(
|
||||||
|
format='%(asctime)s [%(levelname)s] %(message)s', level=logging.INFO
|
||||||
|
)
|
||||||
|
|
||||||
|
# simply devlog high-order function, you can also create an middleware to use logging, see middleware_router.py and middleware.py
|
||||||
|
def devlog(handler):
|
||||||
|
def devlog_route(res, req):
|
||||||
|
logging.info(f'{req.get_method()} {req.get_full_url()} {req.get_headers()=}')
|
||||||
|
handler(res, req)
|
||||||
|
return devlog_route
|
||||||
|
|
||||||
|
# Now is just use the devlog function or middleware
|
||||||
|
|
||||||
|
app = App()
|
||||||
|
|
||||||
|
def home(res, req):
|
||||||
|
res.end("Hello World!")
|
||||||
|
|
||||||
|
app.get("/", devlog(home))
|
||||||
|
|
||||||
|
app.listen(
|
||||||
|
3000,
|
||||||
|
lambda config: logging.info("Listening on port http://localhost:%d now\n" % config.port),
|
||||||
|
)
|
||||||
|
app.run()
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ def auth(route):
|
||||||
params = req.get_parameters()
|
params = req.get_parameters()
|
||||||
# get queries will preserve all queries inside req after await
|
# get queries will preserve all queries inside req after await
|
||||||
queries = req.get_queries()
|
queries = req.get_queries()
|
||||||
|
# or just use req.preserve() to preserve all
|
||||||
|
|
||||||
user = await get_user(headers.get("authorization", None))
|
user = await get_user(headers.get("authorization", None))
|
||||||
if user:
|
if user:
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import threading
|
import logging
|
||||||
import time
|
|
||||||
|
|
||||||
from .uv import UVLoop
|
from .uv import UVLoop
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
@ -17,7 +15,7 @@ def future_handler(future, loop, exception_handler, response):
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
# just log in console the error to call attention
|
# just log in console the error to call attention
|
||||||
print("Uncaught Exception: %s" % str(error))
|
logging.error("Uncaught Exception: %s" % str(error))
|
||||||
if response != None:
|
if response != None:
|
||||||
response.write_status(500).end("Internal Error")
|
response.write_status(500).end("Internal Error")
|
||||||
finally:
|
finally:
|
||||||
|
|
|
@ -6,17 +6,15 @@ import inspect
|
||||||
import json
|
import json
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import os
|
import os
|
||||||
from os import path
|
|
||||||
import platform
|
import platform
|
||||||
import signal
|
import signal
|
||||||
from threading import Thread, local, Lock
|
|
||||||
import uuid
|
import uuid
|
||||||
from urllib.parse import parse_qs, quote_plus, unquote_plus
|
from urllib.parse import parse_qs, quote_plus, unquote_plus
|
||||||
|
import logging
|
||||||
|
|
||||||
from .loop import Loop
|
from .loop import Loop
|
||||||
from .status_codes import status_codes
|
from .status_codes import status_codes
|
||||||
from .helpers import static_route
|
from .helpers import static_route
|
||||||
from queue import SimpleQueue
|
|
||||||
|
|
||||||
mimetypes.init()
|
mimetypes.init()
|
||||||
|
|
||||||
|
@ -296,7 +294,7 @@ def uws_missing_server_name(hostname, hostname_length, user_data):
|
||||||
else:
|
else:
|
||||||
handler(data)
|
handler(data)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
|
|
||||||
|
@ -323,7 +321,7 @@ def uws_websocket_factory_drain_handler(ws, user_data):
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
if dispose:
|
if dispose:
|
||||||
app._ws_factory.dispose(instances)
|
app._ws_factory.dispose(instances)
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
|
|
||||||
|
@ -339,7 +337,7 @@ def uws_websocket_drain_handler(ws, user_data):
|
||||||
else:
|
else:
|
||||||
handler(ws)
|
handler(ws)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
|
|
||||||
|
@ -366,7 +364,7 @@ def uws_websocket_factory_open_handler(ws, user_data):
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
if dispose:
|
if dispose:
|
||||||
app._ws_factory.dispose(instances)
|
app._ws_factory.dispose(instances)
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
|
|
||||||
|
@ -383,7 +381,7 @@ def uws_websocket_open_handler(ws, user_data):
|
||||||
else:
|
else:
|
||||||
handler(ws)
|
handler(ws)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
|
|
||||||
|
@ -419,7 +417,7 @@ def uws_websocket_factory_message_handler(ws, message, length, opcode, user_data
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
if dispose:
|
if dispose:
|
||||||
app._ws_factory.dispose(instances)
|
app._ws_factory.dispose(instances)
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
|
|
||||||
|
@ -445,7 +443,7 @@ def uws_websocket_message_handler(ws, message, length, opcode, user_data):
|
||||||
handler(ws, data, opcode)
|
handler(ws, data, opcode)
|
||||||
|
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
|
|
||||||
|
@ -478,7 +476,7 @@ def uws_websocket_factory_pong_handler(ws, message, length, user_data):
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
if dispose:
|
if dispose:
|
||||||
app._ws_factory.dispose(instances)
|
app._ws_factory.dispose(instances)
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
@ffi.callback("void(uws_websocket_t*, const char*, size_t, void*)")
|
@ffi.callback("void(uws_websocket_t*, const char*, size_t, void*)")
|
||||||
|
@ -498,7 +496,7 @@ def uws_websocket_pong_handler(ws, message, length, user_data):
|
||||||
else:
|
else:
|
||||||
handler(ws, data)
|
handler(ws, data)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
|
|
||||||
|
@ -532,7 +530,7 @@ def uws_websocket_factory_ping_handler(ws, message, length, user_data):
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
if dispose:
|
if dispose:
|
||||||
app._ws_factory.dispose(instances)
|
app._ws_factory.dispose(instances)
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
|
|
||||||
|
@ -555,7 +553,7 @@ def uws_websocket_ping_handler(ws, message, length, user_data):
|
||||||
handler(ws, data)
|
handler(ws, data)
|
||||||
|
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
|
|
||||||
|
@ -600,7 +598,7 @@ def uws_websocket_factory_close_handler(ws, code, message, length, user_data):
|
||||||
app._ws_factory.dispose(instances)
|
app._ws_factory.dispose(instances)
|
||||||
|
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
|
|
||||||
|
@ -638,7 +636,7 @@ def uws_websocket_close_handler(ws, code, message, length, user_data):
|
||||||
SocketRefs.pop(key, None)
|
SocketRefs.pop(key, None)
|
||||||
|
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(err)
|
"Uncaught Exception: %s" % str(err)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
|
|
||||||
|
@ -846,7 +844,7 @@ def uws_generic_cork_handler(res, user_data):
|
||||||
raise RuntimeError("Calls inside cork must be sync")
|
raise RuntimeError("Calls inside cork must be sync")
|
||||||
response._cork_handler(response)
|
response._cork_handler(response)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print("Error on cork handler %s" % str(err))
|
logging.error("Error on cork handler %s" % str(err))
|
||||||
|
|
||||||
|
|
||||||
@ffi.callback("void(void*)")
|
@ffi.callback("void(void*)")
|
||||||
|
@ -858,7 +856,7 @@ def uws_ws_cork_handler(user_data):
|
||||||
raise RuntimeError("Calls inside cork must be sync")
|
raise RuntimeError("Calls inside cork must be sync")
|
||||||
ws._cork_handler(ws)
|
ws._cork_handler(ws)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print("Error on cork handler %s" % str(err))
|
logging.error("Error on cork handler %s" % str(err))
|
||||||
|
|
||||||
|
|
||||||
# Compressor mode is 8 lowest bits where HIGH4(windowBits), LOW4(memLevel).
|
# Compressor mode is 8 lowest bits where HIGH4(windowBits), LOW4(memLevel).
|
||||||
|
@ -936,7 +934,7 @@ class WebSocket:
|
||||||
)
|
)
|
||||||
self._for_each_topic_handler(topic)
|
self._for_each_topic_handler(topic)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print("Error on for each topic handler %s" % str(err))
|
logging.error("Error on for each topic handler %s" % str(err))
|
||||||
|
|
||||||
# uuid for socket data, used to free data after socket closes
|
# uuid for socket data, used to free data after socket closes
|
||||||
def get_user_data_uuid(self):
|
def get_user_data_uuid(self):
|
||||||
|
@ -1502,7 +1500,7 @@ class AppRequest:
|
||||||
)
|
)
|
||||||
self._for_each_header_handler(key, value)
|
self._for_each_header_handler(key, value)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print("Error on data handler %s" % str(err))
|
logging.error("Error on data handler %s" % str(err))
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@ -1562,7 +1560,7 @@ class AppResponse:
|
||||||
else:
|
else:
|
||||||
self._aborted_handler(self)
|
self._aborted_handler(self)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print("Error on abort handler %s" % str(err))
|
logging.error("Error on abort handler %s" % str(err))
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def trigger_data_handler(self, data, is_end):
|
def trigger_data_handler(self, data, is_end):
|
||||||
|
@ -1575,7 +1573,7 @@ class AppResponse:
|
||||||
else:
|
else:
|
||||||
self._data_handler(self, data, is_end)
|
self._data_handler(self, data, is_end)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print("Error on data handler %s" % str(err))
|
logging.error("Error on data handler %s" % str(err))
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@ -1590,7 +1588,7 @@ class AppResponse:
|
||||||
raise RuntimeError("AppResponse.on_writable must be synchronous")
|
raise RuntimeError("AppResponse.on_writable must be synchronous")
|
||||||
return self._writable_handler(self, offset)
|
return self._writable_handler(self, offset)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print("Error on writable handler %s" % str(err))
|
logging.error("Error on writable handler %s" % str(err))
|
||||||
return False
|
return False
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -2529,7 +2527,7 @@ class App:
|
||||||
def trigger_error(self, error, response, request):
|
def trigger_error(self, error, response, request):
|
||||||
if self.error_handler == None:
|
if self.error_handler == None:
|
||||||
try:
|
try:
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(error)
|
"Uncaught Exception: %s" % str(error)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
response.write_status(500).end("Internal Error")
|
response.write_status(500).end("Internal Error")
|
||||||
|
@ -2546,7 +2544,7 @@ class App:
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
try:
|
try:
|
||||||
# Error handler got an error :D
|
# Error handler got an error :D
|
||||||
print(
|
logging.error(
|
||||||
"Uncaught Exception: %s" % str(error)
|
"Uncaught Exception: %s" % str(error)
|
||||||
) # just log in console the error to call attention
|
) # just log in console the error to call attention
|
||||||
response.write_status(500).end("Internal Error")
|
response.write_status(500).end("Internal Error")
|
||||||
|
|
Ładowanie…
Reference in New Issue