python-websocket-server icon indicating copy to clipboard operation
python-websocket-server copied to clipboard

Logging config overwritten

Open Fruchtzwerg94 opened this issue 5 years ago • 2 comments

https://github.com/Pithikos/python-websocket-server/blob/fd0b190de3bef32967f64f673897a96544166593/websocket_server/websocket_server.py#L18

overwrites an already set logging configuration if the module is imported afterwards. Whats the reason of this line of code here?

Fruchtzwerg94 avatar May 21 '20 07:05 Fruchtzwerg94

To @Pithikos I would like to thank you for this wonderful module. It is super easy to use. Really good job. I also recommend allowing to pass a logger handle as an argument to the class for a custom configuration instead of logging.basicConfig().

To @Fruchtzwerg94 For me, it does not overwrite a custom configuration but it prints a message twice. To solve that issue I used: logger.propagate = False

This is a simple sample :

import logging
from websocket_server import WebsocketServer

def new_client(client, server):
	server.send_message_to_all("Hey all, a new client has joined us")

#------------------------------------------------------------
# A custom logger
logger = logging.getLogger('my_custom_logger')
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.setLevel(logging.INFO)
logger.propagate = False  # Without this, the next message will be printed twice
#------------------------------------------------------------
logger.info("Ready to start the server")

server = WebsocketServer(13254, host='127.0.0.1', loglevel=logging.INFO)
server.set_fn_new_client(new_client)
server.run_forever()

ramezanifar avatar Oct 08 '20 20:10 ramezanifar

Yeah. Agree, nice module/library. It would be nice to fix global logging, though. Hopefully, someone can provide PR...

tysonite avatar Jul 12 '22 13:07 tysonite