【问题标题】:Stacktrace of Python is cut in psycopg2 connection.noticesPython 的 Stacktrace 在 psycopg2 connection.notices 中被截断
【发布时间】:2019-05-22 08:38:08
【问题描述】:

我正在覆盖 psycopg2 的 connection.notices

我的目标是从 PostgreSQL 触发器向我的应用程序发送通知。

在 python 中,我想将当前堆栈跟踪写入日志文件,以查看哪个 python 代码触发了数据库中的触发器。

它有效,但不幸的是我无法提取整个堆栈跟踪。

我只得到 psycopg 下面的行,而不是上面的行(调用者的行)。

这是我的代码:

# Overwriting connetion.notices via Django
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()

我在日志中看到了这一点:

'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() 内的ConnectionNoticeList.append() 不会提取调用者。

有没有办法获取上层方法的行?

(这与一个较老的问题有关:Log Stacktrace of current Python Interpreter via PostgreSQL trigger

【问题讨论】:

  • 是的!这是标记为“postgresql”的第 100000 个问题!
  • @LaurenzAlbe 如果您查看此图表,它看起来不错。很快就会有更多关于 PostgreSQL 的问题:sotagtrends.com/?tags=[postgresql,mysql]

标签: python django postgresql psycopg2


【解决方案1】:

以上代码有效。我看到了整个回溯。

我不知道为什么 PyCharm 中的回溯被切断了。在生产中,我可以看到整个回溯,并且可以找到以不应该发生的方式修改数据的损坏代码。

非常感谢 Daniele Varrazzo 提供了覆盖 connection.notices 的提示:

https://www.postgresql.org/message-id/CA%2Bmi_8a5rij_5xchPkYPFS3moAtw9i6TrwKndynWfbJ%3DkFA4fg%40mail.gmail.com

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2017-11-22
    • 1970-01-01
    相关资源
    最近更新 更多