{"action":"opened","issue":{"url":"https://api.github.com/repos/quarkslab/irma-brain/issues/10","labels_url":"https://api.github.com/repos/quarkslab/irma-brain/issues/10/labels{/name}","comments_url":"https://api.github.com/repos/quarkslab/irma-brain/issues/10/comments","events_url":"https://api.github.com/repos/quarkslab/irma-brain/issues/10/events","html_url":"https://github.com/quarkslab/irma-brain/issues/10","id":65155088,"number":10,"title":"Use lazy string formatting in logging calls","user":{"login":"Zopieux","id":81353,"avatar_url":"https://avatars.githubusercontent.com/u/81353?v=3","gravatar_id":"","url":"https://api.github.com/users/Zopieux","html_url":"https://github.com/Zopieux","followers_url":"https://api.github.com/users/Zopieux/followers","following_url":"https://api.github.com/users/Zopieux/following{/other_user}","gists_url":"https://api.github.com/users/Zopieux/gists{/gist_id}","starred_url":"https://api.github.com/users/Zopieux/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Zopieux/subscriptions","organizations_url":"https://api.github.com/users/Zopieux/orgs","repos_url":"https://api.github.com/users/Zopieux/repos","events_url":"https://api.github.com/users/Zopieux/events{/privacy}","received_events_url":"https://api.github.com/users/Zopieux/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-03-30T07:48:02Z","updated_at":"2015-03-30T07:48:02Z","closed_at":null,"body":"I've seen many instances of code (if not *all*) that uses raw string formatting such as:\r\n\r\n```python\r\n# https://github.com/quarkslab/irma-brain/blob/1e5e52ca2b1cab3ea279776085f102f90c91e244/brain/tasks.py#L68\r\nlog.info(\"{0}: Found user\".format(frontend_scanid) + \r\n         \"quota remaining {0}/{1}\".format(remaining, quota)) \r\n```\r\n\r\nThe right way™ is to pass parameters to the `log.{debug,info,warning,error}` methods and use printf placeholders, so that string formatting, which is quite an expensive computation, *is only done when it's needed*. Eg. if I disable logging or set it to *warning* level, there is no need to compute *debug* and *info* strings.\r\n\r\n```python\r\nlog.info(\"%s: Found user quota remaining %d/%d\", frontend_scanid, remaining, quota)\r\n```\r\nNote this is not only a performance improvement, it also prevents the program from crashing because of an exception during the string formatting in a message that was not supposed to be shown:\r\n```python\r\nlogging.basicConfig(level=logging.WARNING)\r\nn = \"not a number\"\r\nlogging.debug(\"this is not shown, does not crash %d\", n)\r\nlogging.debug(\"this is not shown, but crashes %d\" % n)\r\n```"}}