Skip to content

Conditional log message evaluation #316

@elementbound

Description

@elementbound

✨ 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:

  • _NetfoxLogger in 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactorSame functionality with improved code quality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions