Pithikos / python-websocket-server

A simple fully working websocket-server in Python with no external dependencies
MIT License
1.14k stars 384 forks source link

Logging config overwritten #85

Open Fruchtzwerg94 opened 4 years ago

Fruchtzwerg94 commented 4 years ago

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?

ramezanifar commented 4 years ago

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()
tysonite commented 2 years ago

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