Re: Log Stacktrace of current Python Interpreter via PostgreSQLtrigger - Mailing list psycopg

From Thomas Güttler
Subject Re: Log Stacktrace of current Python Interpreter via PostgreSQLtrigger
Date
Msg-id ccf6b7eb-d2e4-db59-165a-0eaa26eefdd1@thomas-guettler.de
Whole thread Raw
In response to Re: Log Stacktrace of current Python Interpreter via PostgreSQLtrigger  (Thomas Güttler <guettliml@thomas-guettler.de>)
List psycopg
Am 20.05.19 um 14:43 schrieb Thomas Güttler:
> Am 20.05.19 um 12:19 schrieb Daniele Varrazzo:
>> If you use postgres logging in stored procedures you can retrieve the logs in 'connection.notices'.
>>
>> http://initd.org/psycopg/docs/connection.html#connection.notices
> 
> This sound great. Unfortunately I can't extract the whole stacktrace.
> I only get the lines below psycopg, not the above (lines of the callers).
> 
> Here is my code:
> 
> class MyAppConfig(AppConfig):
> 
>      def ready(self):
>          connection_created.connect(connection_created_check_for_notice_in_connection)
> 
> class ConnectionNoticeList(object):
>      def append(self, message):
>          if not 'some_magic_of_db_trigger' in message:
>              return
>          logger.warn('%s %s' % (message, ''.join(traceback.format_stack())))
> 
> 
> def connection_created_check_for_notice_in_connection(sender, connection, **kwargs):
>      connection.connection.notices=ConnectionNoticeList()
> 
> 
> I see this in the logs:
> 
> 'NOTICE:  some_magic_of_db_trigger: 17909
>       File "/snap/pycharm-community/128/helpers/pycharm/_jb_pytest_runner....ork/foo/apps.py", line 47, in append
>        logger.warn(\'%s %s\' % (message, \'\'.join(traceback.format_stack())))
>    '
> 
> 
> traceback.format_stack() inside ConnectionNoticeList.append() extracts not the callers.
> 
> Is there a way to get the callers lines?


Above code works. I see the whole traceback.

I don't know why the traceback was cut in PyCharm. In production I could see the whole traceback and I could find the 
broken code which modified the data in way which should not happen.

Many thanks to Daniele Varrazzo who provided the hint to overwrite connection.notices.

Regards,
   Thomas Güttler

-- 
Thomas Guettler http://www.thomas-guettler.de/
I am looking for feedback: https://github.com/guettli/programming-guidelines



psycopg by date:

Previous
From: Christophe Pettus
Date:
Subject: Re: Proper way to keep count of statements executed within currenttransaction
Next
From: Daniel Cohen
Date:
Subject: Creating dynamically-typed tables using psycopg2's built-in formatting