Search code examples
pythonsqlalchemypyramid

Enabling SQL statements for SQLAlchemy / Pyramid from the command line


I am using Pyramid with SQLAlchemy, and I have the following section in my development.ini:

[logger_sqlalchemy]
# level = INFO
level = WARN
handlers =
qualname = sqlalchemy.engine

Whenever I want to enable SQL statement / echo, I edit that file and change the comment so it becomes level = INFO.

My question is how can I do this from the command line, without editing the file in a text editor.

Other web frameworks have built-in support for this, but I couldn't find a solution for Pyramid / SQLAlchemy.

I'd like to support pserve, pshell, and alembic.


Solution

  • If you're using pyramid_sqlalchemy, there's an option --sql-echo that you can use when running tests.

    Otherwise, you will have parse the arguments yourself and set the logging level:

    import logging
    logging.getLogger('sqlalchemy.engine').setLevel('INFO')
    

    For pserve and pshell, you would probably have to read the command line arguments somewhere in your app initalisation code.

    For alembic, you would need to modify your env.py.