orm.Mapper can construct invalid logging messages

Issue #2171 resolved
Former user created an issue

Percent characters in the message string constructed in orm.Mapper._log are not escaped.

In 0.6.7, a mapper based on an alias construct can have something like self.local_table.description == '%(146396108 anon)s' . This is annoying because the logging module then catches the exception and only logs a message with no reference to the source outside the logging module itself:

Traceback (most recent call last): File "/usr/lib/python2.6/logging/handlers.py", line 71, in emit if self.shouldRollover(record): File "/usr/lib/python2.6/logging/handlers.py", line 144, in shouldRollover msg = "%s\n" % self.format(record) File "/usr/lib/python2.6/logging/init.py", line 648, in format return fmt.format(record) File "/usr/lib/python2.6/logging/init.py", line 436, in format record.message = record.getMessage() File "/usr/lib/python2.6/logging/init.py", line 306, in getMessage msg = msg % self.args TypeError: format requires a mapping

Proposed fix:

    def _log(self, msg, *args):
        self.logger.info(
            "(" + self.class_.__name__ +
            "|" +
            (self.local_table is not None and
                self.local_table.description or
                str(self.local_table)) +
            (self.non_primary and "|non-primary" or "") + ") " +
            msg, *args)

--->

        msg0 = "(" + self.class_.__name__ + "|" + 
            (self.local_table is not None and 
                self.local_table.description or 
                str(self.local_table)) +
            (self.non_primary and "|non-primary" or "") + ") "
        self.logger.info(msg0.replace('%', '%%') + msg, *args)

Comments (2)

  1. Mike Bayer repo owner

    logging unfortuantely applies the "%" operator inconsistently based on if args is empty or not, so a more generalized solution ensures that all literals are passed as *args to log.info() and log.debug().

    a1a588fabdf76dede32af5292535bf7dbabcc0bf aa5ca4af5b48dbf0080998f3e5aa837ff31b5ef9

    needless DEBUG from those commits removed in

    16fc85c2a0111363c5917e81d55eba642dcf1457 ba30ce550d6f4addf9b2f2827b1c42a320b1a131

  2. Log in to comment