diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-06-09 10:59:23 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-06-09 11:59:22 -0400 |
commit | b0bf421f1b12eeedd77ec6c39df8e5e6cc1fcc3f (patch) | |
tree | 8905048db510986642f7978192751f3d471661ad /lib/sqlalchemy/dialects/postgresql/psycopg2.py | |
parent | e2521b595ae8b5b4ca8147c5ee13e2fc0f597e63 (diff) | |
download | sqlalchemy-b0bf421f1b12eeedd77ec6c39df8e5e6cc1fcc3f.tar.gz |
psycopg2 NOTICE fixup
- don't call relatively expensive isEnabledFor(), just call _log_notices
- don't reset the list if it's empty
- fix the test to use a custom function to definitely create a notice, confirmed
that PG seems to no longer create the "implicit sequence" notices
- assert that the reset of the notices works too
- update the docs to illustrate for folks who haven't worked with logging before
Change-Id: I7291e647c177d338e0ad673f3106b4d503e4b3ea
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/psycopg2.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/psycopg2.py | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py index f7c5f668b..cd875e71c 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py @@ -322,11 +322,25 @@ NOTICE logging --------------- The psycopg2 dialect will log PostgreSQL NOTICE messages -via the ``sqlalchemy.dialects.postgresql`` logger:: +via the ``sqlalchemy.dialects.postgresql`` logger. When this logger +is set to the ``logging.INFO`` level, notice messages will be logged:: import logging + + logging.getLogger('sqlalchemy.dialects.postgresql').setLevel(logging.INFO) + +Above, it is assumed that logging is configured externally. If this is not +the case, configuration such as ``logging.basicConfig()`` must be utilized:: + + import logging + + logging.basicConfig() # log messages to stdout logging.getLogger('sqlalchemy.dialects.postgresql').setLevel(logging.INFO) +.. seealso:: + + `Logging HOWTO <https://docs.python.org/3/howto/logging.html>`_ - on the python.org website + .. _psycopg2_hstore: HSTORE type @@ -392,7 +406,7 @@ from ... import processors from ... import types as sqltypes from ... import util from ...engine import result as _result - +from ...util import collections_abc try: from uuid import UUID as _python_UUID # noqa @@ -511,9 +525,7 @@ class PGExecutionContext_psycopg2(PGExecutionContext): return self._dbapi_connection.cursor(ident) def get_result_proxy(self): - # TODO: ouch - if logger.isEnabledFor(logging.INFO): - self._log_notices(self.cursor) + self._log_notices(self.cursor) if self._is_server_side: return _result.BufferedRowResultProxy(self) @@ -521,6 +533,15 @@ class PGExecutionContext_psycopg2(PGExecutionContext): return _result.ResultProxy(self) def _log_notices(self, cursor): + # check also that notices is an iterable, after it's already + # established that we will be iterating through it. This is to get + # around test suites such as SQLAlchemy's using a Mock object for + # cursor + if not cursor.connection.notices or not isinstance( + cursor.connection.notices, collections_abc.Iterable + ): + return + for notice in cursor.connection.notices: # NOTICE messages have a # newline character at the end |