Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hY5F3-0005wX-Mm for psycopg@arkaria.postgresql.org; Tue, 04 Jun 2019 08:56:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hY5F2-0005B1-6K for psycopg@arkaria.postgresql.org; Tue, 04 Jun 2019 08:56:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hY5F1-00056e-Rt for psycopg@lists.postgresql.org; Tue, 04 Jun 2019 08:56:24 +0000 Received: from hz1.yz.to ([2a01:4f8:120:9383::2]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hY5Ey-0002rm-LS for psycopg@lists.postgresql.org; Tue, 04 Jun 2019 08:56:22 +0000 Received: from [172.17.6.34] (mail.tbz-pariv.de [146.0.121.193]) by hz1.yz.to (Postfix) with ESMTPSA id 8E23487F0F9F for ; Tue, 4 Jun 2019 10:56:16 +0200 (CEST) Subject: Re: Log Stacktrace of current Python Interpreter via PostgreSQL trigger To: psycopg@lists.postgresql.org References: <46505a4b-517b-0967-c2f9-1364a794ecef@thomas-guettler.de> <2356ac85-773c-7aef-48b6-6f1f0ad4b1d4@thomas-guettler.de> From: =?UTF-8?Q?Thomas_G=c3=bcttler?= Message-ID: Date: Tue, 4 Jun 2019 10:56:16 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: <2356ac85-773c-7aef-48b6-6f1f0ad4b1d4@thomas-guettler.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: de-DE Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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