Opened 12 months ago

Last modified 11 months ago

#9347 defect new

twisted.logger doesn't execute methods despite documentation

Reported by: ElementalAlchemist Owned by:
Priority: normal Milestone:
Component: logger Keywords:
Cc: Branch:
Author:

Description (last modified by ElementalAlchemist)

It may come up that people want to log things based on data gotten from object methods, e.g.

self.ircd.log.debug("Disconnecting user {user.uuid} ({user.hostmask()}): {reason}", user=self, reason=reason)

where user.hostmask() is some sort of useful data.

According to the documentation (https://twistedmatrix.com/documents/16.1.1/core/howto/logger.html#format-strings), not only is this allowed, but there is an example showing that this is explicitly allowed.

However, attempting to log the above message results in Twisted logging something like this:

2017-12-05 20:52:26-0600 [-] Unable to format event {'log_namespace': 'txircd', 'log_level': <LogLevel=debug>, 'format': '%(log_legacy)s', 'log_logger': <Logger 'txircd'>, 'log_source': None, 'system': '-', 'reason': 'Registration timeout', 'user': <txircd.user.IRCUser instance at 0x7f0c092e8050>, 'time': 1512528746.643236, 'log_format': 'Disconnecting user {user.uuid} ({user.hostmask()}): {reason}', 'message': (), 'log_time': 1512528746.643236}: IRCUser instance has no attribute 'hostmask()'

Instead, in order to get the desired logging, you need to modify it so that the functions aren't run on objects in the string:

self.ircd.log.debug("Disconnecting user {user.uuid} ({hostmask()}): {reason}", user=self, hostmask=self.hostmask, reason=reason)

This logs as expected, but the need to change to this doesn't match up with what the documentation says.

Either object methods need to be allowed, or the documentation needs to be changed to disallow it (including modifying the example where an object method is explicitly used in this way).

Change History (1)

comment:1 Changed 11 months ago by ElementalAlchemist

Description: modified (diff)
Note: See TracTickets for help on using tickets.