ranaroussi / ezibpy

ezIBpy, a Pythonic Client for Interactive Brokers API
Apache License 2.0
324 stars 116 forks source link

Pass error callbacks message object; make programmatic logging possible #3

Closed jkleint closed 7 years ago

jkleint commented 7 years ago

handleErrorEvents() was calling ibCallback() with a msg consisting of just the integer error code. The other callbacks all expect a message object. Passing the full object allows clients to handle messages uniformly and extract the error message text.

Added logging=False kwarg to ezIBpy.__init__() to make logging possible.

It seems like maybe izIBpy() was avoiding kwargs by design, so if another method is preferred (a configure() method or something) that's fine.

Also, it's not clear what the ezIBpy.log() method buys: it would be less typing to just use logging.info() and friends directly, and you would only have to enable logging in one place, using the standard Python method. Seems simpler, but I refrained from refactoring.

ranaroussi commented 7 years ago

I took the logging thing one step further and allow users to have a filename or a buffer as the logging value instead of True/False.

Added two optional parameters to __init__() for auto-logging: logger as the log type (either "stream" for stdout or "file") and logger_file as log file path (if logger == "file")

Example:

ibConn = ezIBpy(logger="stream") # also accepts "stdout"

or

ibConn = ezIBpy(logger="file", logger_file="path/to/file.log") # also accepts "filename"

Hope you'll like it :)

ranaroussi commented 7 years ago

Version 1.12.27 released!

jkleint commented 7 years ago

Hey Ran, thanks, glad you liked it. I was actually thinking it might be easier to just let the builtin Python logger do its thing, since it's already programmatically configurable to set the level and the stream, even from other modules, without you having to write or maintain any code! All you have to do is initialize the logger with a name, and then people can log the normal Python way that most all other Python software does.

I'm happy to whip up the patch for this, just let me know.

https://docs.python.org/2/howto/logging-cookbook.html#logging-cookbook

ranaroussi commented 7 years ago

That's actually more elegant than my solution. I'd love it if you could write the patch 👍

Thanks!