paho.mqtt.python icon indicating copy to clipboard operation
paho.mqtt.python copied to clipboard

Print exception raised in callbacks

Open daadu opened this issue 4 years ago • 2 comments

Problem

Any exception raised on callbacks (on_connect, on_message, etc) are not being printed in the console. This can cause issue when debugging. It looks like the messages are not being handled properly.

Possible Solution

  • print the exception with traceback

daadu avatar Aug 15 '20 10:08 daadu

Looks like Client._logger is not configured by default.

  • Either we can initialise Client._logger by default.
  • Or if Client._logger is not configured then just print() in Client._easy_log

I think we should go for the second option to just print if no logger is configured. Let me know what the appropriate solution could be, so that I can work on the fix.

daadu avatar Aug 15 '20 10:08 daadu

I think we should initialize a logger by default. loggers from the logging module are infinitely better than print for libraries you intend on including. It makes it trivially easy to handle logging the way you want to in a client application.

It's really convenient to have log messages that only print out when debugging the application, but people writing clients often want to be able to turn debug logging on for their application code without being completely inundated with output from included libraries.

I will often set my default level to DEBUG, then grab the requests logger and set the log level to WARNING or INFO with a single line of code. I don't have to worry about instantiating and passing a new logger to stuff I instantiate from that library, and I can configure logging behavior just the way I want to using nothing more than the logging package. Using print instead of a default logger ruins that behavior, and forces developers to monkey-patch the print function to achieve the same behavior.

Also, logging.exception does print the traceback if used in an except block.

swails avatar Aug 15 '20 11:08 swails