5

I am using the websockets package to create a websocket server in python; at the same time I am heavily using the logging in different log-levels.

The websockets documentation mentions the logger configuration and that it could be changed to the log-level ERROR using

     logger = logging.getLogger('websockets.server')
     logger.setLevel(logging.ERROR)
     logger.addHandler(logging.StreamHandler())

This, however, does not have any effect in my code, wherever I place it (below the import, before the websockets import, within __main__). I'd like to have two configurations - one global logging configuration and the logger-configuration of the websockets server.

Another option would be to disable the logging completely for the websockets module but I can't figure out how to do that. Any ideas?

2 Answers 2

6

It turns out it is not only the websockets.server that is emitting lots of debug messages, there's other ones too. I've ended up hunting down those packages by using the %(name)s field in the root logger configuration and could find websockets.protocol being responsible for the additional messages.

logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s %(name)s %(levelname)-8s  %(message)s',
    datefmt='(%H:%M:%S)')

# disable all loggers from different files
logging.getLogger('asyncio').setLevel(logging.ERROR)
logging.getLogger('asyncio.coroutines').setLevel(logging.ERROR)
logging.getLogger('websockets.server').setLevel(logging.ERROR)
logging.getLogger('websockets.protocol').setLevel(logging.ERROR)
Sign up to request clarification or add additional context in comments.

Comments

3

When you start a websocket, set:

websocket.enableTrace(False) 

No DEBUG and hart beat messages will be logged or shown in the console.

My example code:

def start_websocket(url, listen_method):
"""
Start a websocket connection.

:param url: string
:param listen_method: method
"""
# Enable or Disable logging
websocket.enableTrace(False)

ws = websocket.WebSocketApp(
    url,
    on_message=listen_method,
    on_error=on_error,
    on_close=on_close
)

ws.on_open = on_open

ws.run_forever(ping_interval=3, ping_timeout=2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.