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!
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__
.
Using repr(engine)
will include the class name. To get just the URL representation do:
from sqlalchemy import create_engine
engine = create_engine('postgresql://scott:tiger@localhost/test')
assert repr(engine.url) == 'postgresql://scott:***@localhost/test'
(note that .url
is the only addition)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With