Skip to content

Commit eb93f50

Browse files
committed
Allow logging configuration directly via API
1 parent 4815a08 commit eb93f50

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,14 @@ The API is simply methods and properties of the `WebsocketServer` class.
4141

4242
## WebsocketServer
4343

44-
The WebsocketServer takes two arguments: a `port` and a `hostname`.
45-
By default the localhost `127.0.0.1` is used. However if you want to be able and connect
46-
to the server from the network you need to pass `0.0.0.0` as hostname e.g. `WebsocketServer(13254, host='0.0.0.0')`.
44+
The WebsocketServer can be initialized with the below parameters.
45+
46+
*`port`* - The port clients will need to connect to.
47+
48+
*`host`* - By default the `127.0.0.1` is used which allows connections only from the current machine. If you wish to allow all network machines to connect, you need to pass `0.0.0.0` as hostname.
49+
50+
*`loglevel`* - logging level to print. By default WARNING is used. You can use `logging.DEBUG` or `logging.INFO` for more verbose output.
51+
4752

4853
###Properties
4954

@@ -72,18 +77,18 @@ to the server from the network you need to pass `0.0.0.0` as hostname e.g. `Webs
7277
| `set_fn_message_received()` | Called when a `client` sends a `message` | client, server, message |
7378

7479

75-
The client passed to the callback is the client that left, sent the message, etc. The server might not have any use to use. However it is
76-
passed in case you want to send messages to clients.
80+
The client passed to the callback is the client that left, sent the message, etc. The server might not have any use to use. However it is passed in case you want to send messages to clients.
7781

7882

7983
Example:
8084
````
85+
import logging
8186
from websocket_server import WebsocketServer
8287
8388
def new_client(client, server):
8489
server.send_message_to_all("Hey all, a new client has joined us")
8590
86-
server = WebsocketServer(13254, host='127.0.0.1')
91+
server = WebsocketServer(13254, host='127.0.0.1', loglevel=logging.INFO)
8792
server.set_fn_new_client(new_client)
8893
server.run_forever()
8994
````

websocket_server/websocket_server.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from socketserver import ThreadingMixIn, TCPServer, StreamRequestHandler
1515

1616
logger = logging.getLogger(__name__)
17-
17+
logging.basicConfig()
1818

1919
'''
2020
+-+-+-+-+-------+-+-------------+-------------------------------+
@@ -105,8 +105,9 @@ class WebsocketServer(ThreadingMixIn, TCPServer, API):
105105
clients=[]
106106
id_counter=0
107107

108-
def __init__(self, port, host='127.0.0.1'):
109-
self.port=port
108+
def __init__(self, port, host='127.0.0.1', loglevel=logging.WARNING):
109+
logger.setLevel(loglevel)
110+
self.port = port
110111
TCPServer.__init__(self, (host, port), WebSocketHandler)
111112

112113
def _message_received_(self, handler, msg):

0 commit comments

Comments
 (0)