Re: Log Stacktrace of current Python Interpreter via PostgreSQL trigger

From: Thomas Güttler <guettliml(at)thomas-guettler(dot)de>
To: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: Log Stacktrace of current Python Interpreter via PostgreSQL trigger
Date: 2019-05-20 12:43:19
Message-ID: 2356ac85-773c-7aef-48b6-6f1f0ad4b1d4@thomas-guettler.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

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?

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

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Podrigal, Aron 2019-05-24 20:49:04 Proper way to keep count of statements executed within current transaction
Previous Message Daniele Varrazzo 2019-05-20 10:19:24 Re: Log Stacktrace of current Python Interpreter via PostgreSQL trigger