"sqlalchemy.dialects.postgresql" logger name.
[ticket:877]
- Fixed bug introduced in 0.6beta2 where column labels would
render inside of column expressions already assigned a label.
[ticket:1747]
+
+- postgresql
+ - The psycopg2 dialect will log NOTICE messages via the
+ "sqlalchemy.dialects.postgresql" logger name.
+ [ticket:877]
0.6beta2
========
This section assumes familiarity with the above linked logging module. All logging performed by SQLAlchemy exists underneath the ``sqlalchemy`` namespace, as used by ``logging.getLogger('sqlalchemy')``. When logging has been configured (i.e. such as via ``logging.basicConfig()``), the general namespace of SA loggers that can be turned on is as follows:
* ``sqlalchemy.engine`` - controls SQL echoing. set to ``logging.INFO`` for SQL query output, ``logging.DEBUG`` for query + result set output.
+* ``sqlalchemy.dialects`` - controls custom logging for SQL dialects. See the documentation of individual dialects for details.
* ``sqlalchemy.pool`` - controls connection pool logging. set to ``logging.INFO`` or lower to log connection pool checkouts/checkins.
* ``sqlalchemy.orm`` - controls logging of various ORM functions. set to ``logging.INFO`` for configurational logging as well as unit of work dumps, ``logging.DEBUG`` for extensive logging during query and flush() operations. Subcategories of ``sqlalchemy.orm`` include:
* ``sqlalchemy.orm.attributes`` - logs certain instrumented attribute operations, such as triggered callables
The psycopg2 dialect fully supports SAVEPOINT and two-phase commit operations.
+NOTICE logging
+---------------
+
+The psycopg2 dialect will log Postgresql NOTICE messages via the
+``sqlalchemy.dialects.postgresql`` logger::
+
+ import logging
+ logging.getLogger('sqlalchemy.dialects.postgresql').setLevel(logging.INFO)
+
Per-Statement Execution Options
-------------------------------
"""
-import random, re
+import random
+import re
import decimal
+import logging
from sqlalchemy import util
from sqlalchemy import processors
PGIdentifierPreparer, PGExecutionContext, \
ENUM, ARRAY
+
+logger = logging.getLogger('sqlalchemy.dialects.postgresql')
+
+
class _PGNumeric(sqltypes.Numeric):
def bind_processor(self, dialect):
return None
return self._connection.connection.cursor()
def get_result_proxy(self):
+ if logger.isEnabledFor(logging.INFO):
+ self._log_notices(self.cursor)
+
if self.__is_server_side:
return base.BufferedRowResultProxy(self)
else:
return base.ResultProxy(self)
+ def _log_notices(self, cursor):
+ for notice in cursor.connection.notices:
+ # NOTICE messages have a
+ # newline character at the end
+ logger.info(notice.rstrip())
+
+ cursor.connection.notices[:] = []
+
class PGCompiler_psycopg2(PGCompiler):
def visit_mod(self, binary, **kw):
return connect
else:
return base_on_connect
-
+
def create_connect_args(self, url):
opts = url.translate_connect_args(username='user')
if 'port' in opts:
from sqlalchemy.sql import table, column
from sqlalchemy.test.testing import eq_
from test.engine._base import TablesTest
+import logging
class SequenceTest(TestBase, AssertsCompiledSQL):
def test_basic(self):
finally:
metadata.drop_all()
-
-
-
class InsertTest(TestBase, AssertsExecutionResults):
__only_on__ = 'postgresql'
finally:
postgresql.PGDialect.ischema_names = ischema_names
-
class MiscTest(TestBase, AssertsExecutionResults, AssertsCompiledSQL):
__only_on__ = 'postgresql'
]:
eq_(testing.db.dialect._get_server_version_info(MockConn(string)), version)
+
+ @testing.only_on('postgresql+psycopg2', 'psycopg2-specific feature')
+ def test_notice_logging(self):
+ log = logging.getLogger('sqlalchemy.dialects.postgresql')
+ buf = logging.handlers.BufferingHandler(100)
+ lev = log.level
+ log.addHandler(buf)
+ log.setLevel(logging.INFO)
+ try:
+ conn = testing.db.connect()
+ trans = conn.begin()
+ try:
+ conn.execute("create table foo (id serial primary key)")
+ finally:
+ trans.rollback()
+ finally:
+ log.removeHandler(buf)
+ log.setLevel(lev)
+
+ msgs = " ".join(b.msg for b in buf.buffer)
+ assert "will create implicit sequence" in msgs
+ assert "will create implicit index" in msgs
+
def test_pg_weirdchar_reflection(self):
meta1 = MetaData(testing.db)