0

I'm setting up SQLAlchemy logging like so, so that only a specific type of delete statements are logged:

class DeleteFilter(logging.Filter):
    def filter(self, record):
        return 'DELETE FROM cr_pending_config WHERE cr_pending_config.id' in record.getMessage()

logger = logging.getLogger('sqlalchemy.engine')
logger.setLevel(logging.INFO)
logger.addFilter(DeleteFilter())

# create a file handler
handler = logging.FileHandler('sql_log.log')
handler.setLevel(logging.INFO)
handler.addFilter(DeleteFilter())

# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# add the handlers to the logger
logger.addHandler(handler)

Problem is, in my log file, I see this:

2017-11-07 20:34:44,196 - sqlalchemy.engine.base.Engine - INFO - DELETE FROM cr_pending_config WHERE cr_pending_config.id = %s
2017-11-07 20:40:37,169 - sqlalchemy.engine.base.Engine - INFO - DELETE FROM cr_pending_config WHERE cr_pending_config.id = %s
2017-11-07 20:41:21,704 - sqlalchemy.engine.base.Engine - INFO - DELETE FROM cr_pending_config WHERE cr_pending_config.id = %s
2017-11-07 20:41:21,858 - sqlalchemy.engine.base.Engine - INFO - DELETE FROM cr_pending_config WHERE cr_pending_config.id = %s
2017-11-07 20:41:21,987 - sqlalchemy.engine.base.Engine - INFO - DELETE FROM cr_pending_config WHERE cr_pending_config.id = %s
2017-11-07 20:41:44,658 - sqlalchemy.engine.base.Engine - INFO - DELETE FROM cr_pending_config WHERE cr_pending_config.id = %s

That is, the actual IDs are not being logged. Why ???

4
  • SQLA logs the statements and their parameter lists separately, as shortly mentioned in the engine configuration docs. So in essence you've filtered the parameters out. Commented Nov 8, 2017 at 7:50
  • Hi. Can you elaborate on what I need to do to fix this issue please ? Thanks. I'm very new to SQLAlchemy. Commented Nov 8, 2017 at 19:31
  • just remove your log filter. Commented Nov 14, 2017 at 6:08
  • Possible duplicate of Debugging (displaying) SQL command sent to the db by SQLAlchemy Commented Nov 14, 2017 at 6:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.