betcode-org / betfair

betfairlightweight - Betfair API-NG python wrapper (with streaming)
MIT License
418 stars 146 forks source link

Lazily evaluate log message parameters #530

Closed petedmarsh closed 10 months ago

petedmarsh commented 11 months ago

Logging like this:

logger.debug("Hello, %s" % name)

causes the message string to always be interpolated even if the message is not going to be logged.

Logging like this:

logger.debug("Hello, %s", name)

will mean the string is only ever interpolated if it will be logged.

In practice here this will make little difference if the log level is INFO or above, but it does mean a if statement can be removed from StreamListener.

See:

https://docs.python.org/3/howto/logging.html#optimization https://stackoverflow.com/questions/4148790/lazy-logger-message-string-evaluation

liampauling commented 11 months ago

How does this compare to f strings?

petedmarsh commented 11 months ago

f-strings would also be eagerly evaluated, to have lazy interpolation of the log messages the best way is to use %-style formatting and pass the arguments to the logger methods

petedmarsh commented 10 months ago

@liampauling I've formatted the code

liampauling commented 10 months ago

thanks, test failing

petedmarsh commented 10 months ago

@liampauling I've fixed the test, sorry!