Skip to content

orm.Mapper can construct invalid logging messages #2171

Closed
@sqlalchemy-bot

Description

@sqlalchemy-bot
Collaborator

Migrated issue, originally created by Anonymous

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)

Activity

sqlalchemy-bot

sqlalchemy-bot commented on May 20, 2011

@sqlalchemy-bot
CollaboratorAuthor

Michael Bayer (@zzzeek) wrote:

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().

a1a588f
aa5ca4a

needless DEBUG from those commits removed in

16fc85c
ba30ce5

sqlalchemy-bot

sqlalchemy-bot commented on May 20, 2011

@sqlalchemy-bot
CollaboratorAuthor

Changes by Michael Bayer (@zzzeek):

  • set milestone to "0.6.8"
  • changed status to closed
added this to the 0.6.8 milestone on Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingorm

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sqlalchemy-bot

        Issue actions

          orm.Mapper can construct invalid logging messages · Issue #2171 · sqlalchemy/sqlalchemy