-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Michael Bayer ha scritto:
> On Feb 28, 2010, at 11:21 AM, Manlio Perillo wrote:
> 
> I would like to write a patch for ticket #877.
> 
> What is the best method to implement the requested feature?
> 
> The ExecutionContext class has a post_exec method, so the implementation
> for the psycopg2 driver can override this method, check for the notice
> messages and log them.
> 
> Attached is a very simple patch.
> 
> The problem is that NOTICE messages will only be logged if echo
> parameter is set to True, in the create_engine function; however I
> usually don't want to echo all SQL statatements, but I'm interested to
> see NOTICE messages.
> 
>> 1. dont use the engine's logger, use one specific to the psycopg2 dialect
>> 2. if the logger has info enabled, then you can dump the notices.  otherwise 
>> don't waste CPU digging through them
> 
>> put it under the namespace 
>> logging.getLogger('sqlalchemy.dialects.postgresql') .
> 

Done, new patch attached.
The is also a bug fix: I forgot to clear the notices list.
The notices list is cleared *only* if messages are actually logged.

I took the liberty to change the coding style, too.

If patch is ok, I will upload it on the bug tracker.



Thanks  Manlio
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkuKqP8ACgkQscQJ24LbaUT9ZgCfdGG+vREnuoJf0e0KmjQaKAGH
bkcAoIorPN24ZeDfvd6g7zym+jYGA1l3
=LuLD
-----END PGP SIGNATURE-----
-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py
--- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py
+++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py
@@ -46,8 +46,10 @@
 
 """
 
-import random, re
+import random
+import re
 import decimal
+import logging
 
 from sqlalchemy import util
 from sqlalchemy import processors
@@ -59,6 +61,10 @@
                                             PGIdentifierPreparer, PGExecutionContext, \
                                             ENUM, ARRAY
 
+
+logger = logging.getLogger('sqlalchemy.dialects.postgresql')
+
+
 class _PGNumeric(sqltypes.Numeric):
     def bind_processor(self, dialect):
         return None
@@ -135,6 +141,14 @@
         else:
             return base.ResultProxy(self)
 
+    def post_exec(self):
+        if logger.isEnabledFor(logging.INFO):
+            for notice in self._connection.connection.notices:
+                # NOTICE messages have a newline character at the end
+                logger.error(notice.rstrip())
+
+            self._connection.connection.notices[:] = []
+
 
 class PostgreSQL_psycopg2Compiler(PGCompiler):
     def visit_mod(self, binary, **kw):

Reply via email to