jasonacox / tinytuya

Python API for Tuya WiFi smart devices using a direct local area network (LAN) connection or the cloud (TuyaCloud API).
MIT License
923 stars 165 forks source link

Log message formatting #285

Closed johnno1962 closed 1 year ago

johnno1962 commented 1 year ago

Hi, was rumaging around trying to find out where my API quota was going and noticed messages were not having values inserted as perhaps you intended.

jasonacox commented 1 year ago

Hi @johnno1962 - Thanks for opening this.

Were you getting an error or the wrong data using the current version? There may be something else wrong.

I see the addition of url and headers here. This makes sense and would be a good addition! Thanks for that.

However, moving to the "%" format is not needed for debug(). In fact, pylint will flag this as suboptimal (see W1201 ). Using lazy logging lets the interpreter skip doing the text formatting until it is needed (if debug level was set). It's a small optimization but it is the reason you will see it in the code.

example test

import logging

log = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)

url = "url"
headers = "headers"
code = 200
text = "text"
token = "token"

log.debug("GET URL=%s HEADERS=%s: response code=%d text=%s token=%s" % (url, headers, code, text, token))

log.debug("GET URL=%s HEADERS=%s: response code=%d text=%s token=%s", url, headers, code, text, token)
johnno1962 commented 1 year ago

Ha, shows how much Python I know. So, the log function is performing the % expansion on demand which is as it should be. You can disregard this PR or, I've pushed another commit to include only the URL=%s HEADERS=%s change. Thanks again for this invaluable resource. I've been able to talk to the cloud from Swift but exhausted my quota for this month :D

jasonacox commented 1 year ago

Thanks @johnno1962 ! It's a good addition.

Thanks to this great community, and contributors like you, the project keeps getting better. ;)