-
-
Notifications
You must be signed in to change notification settings - Fork 60
Conditional log message evaluation #316
Description
✨ Description
Currently most, if not all, log messages are using string interpolation in the message param:
_logger.trace("Received sample: t1=%s; t2=%s; t3=%s; t4=%s; theta=%sms; delta=%sms" % [
sample.ping_sent, sample.ping_received, sample.pong_sent, sample.pong_received,
sample.get_offset() * 1000., sample.get_rtt() * 1000.
])This means that even if the message is not logged, the string will be evaluated. This can waste some processing time for messages that are never seen. To improve on this, the following is my proposal:
_logger.trace("Received sample: t1=%s; t2=%s; t3=%s; t4=%s; theta=%sms; delta=%sms", [
sample.ping_sent, sample.ping_received, sample.pong_sent, sample.pong_received,
sample.get_offset() * 1000., sample.get_rtt() * 1000.
])Notice the change from "..." % [...] to "...", [...]. Instead of doing the string interpolation in the message param, the logger receives both the string and the values to substitute. The string interpolation is only performed when the message actually gets logged.
Use case
This would be used in netfox addons for logging messages, so would affect all projects using netfox.
Upcoming features could also include more toggleabble logging without being concerned about wasted performance. The above time sync snippet is a good example.
Distribution
This would be an update in netfox.internals.
Notes
Code parts to consider:
_NetfoxLoggerin netfox.internals- Search results for
_NetfoxLogger.for_and_NetfoxLogger.new- these should find all usages of the class
The change could be updating all logging methods to this signature:
func info(message: String, values: Array = [])Make sure that no interpolation is done if the array is empty! This should be a backwards-compatible refactor.