I want to log the database connection, but don't want to show the password (or the driver/dialect).
Is there a way to get this out of SQLAlchemy or should I just resort to regex parsing? SQLAlchemy should have already parsed it to find this out so it seems silly to do it myself, but I can't find anything on http://docs.sqlalchemy.org/en/latest/core/connections.html or http://docs.sqlalchemy.org/en/latest/core/engines.html
Thanks!
Note: As of SQLAlchemy 2.0, the password is hidden by default. See URL.render_as_string(hide_password: bool = True)
.
The URL.__repr__
method hides the password:
from sqlalchemy import create_engine
engine = create_engine('postgresql://scott:tiger@localhost/test')
assert repr(engine.url) == 'postgresql://scott:***@localhost/test'
If you want to hide the driver/dialect, you may wish to look at how the URL class computes a string in URL.__to_string__
.