Opened 4 years ago

Last modified 4 years 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:

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 (, 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 4 years ago by ElementalAlchemist

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