【问题标题】:python3.X Issue with logging RotatingFileHandler while closing connectionspython3.X 在关闭连接时记录 RotatingFileHandler 的问题
【发布时间】:2020-05-06 19:55:58
【问题描述】:

我写了一个脚本,我从雪花中读取数据并在缓存中更新。执行脚本后,雪花试图自动关闭任何剩余的连接,而连接正在关闭库正在记录一些我遇到以下问题的详细信息。

--- Logging error ---
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/handlers.py", line 69, in emit
    if self.shouldRollover(record):
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/handlers.py", line 183, in shouldRollover
    self.stream = self._open()
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/logging/__init__.py", line 1116, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
NameError: name 'open' is not defined
Call stack:
  File "~/.virtualenvs/test-ven/lib/python3.7/site-packages/snowflake/connector/connection.py", line 211, in __del__
    self.close(retry=False)
  File "~/.virtualenvs/test-ven/lib/python3.7/site-packages/snowflake/connector/connection.py", line 529, in close
    logger.info('closed')
Message: 'closed'
Arguments: ()

我尝试删除 RotatingFileHandler 然后没有问题可以顺利执行,我尝试使用 python 3.5、3.7 版本,两个版本都出现错误。以下是我的日志记录配置,我可能做错了什么。

    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s'
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'level': 'INFO',
            'formatter': 'verbose'
        },
        'cron_file': {
            'class': 'logging.handlers.RotatingFileHandler',
            'level': 'INFO',
            'formatter': 'verbose',
            'filename': '/var/log/my-project/crons.log',
            'mode': 'a',
            'maxBytes': 5242880,
            'backupCount': 10
        },
        'error_file': {
            'class': 'logging.handlers.RotatingFileHandler',
            'level': 'WARN',
            'formatter': 'verbose',
            'filename': '/var/log/my-project/error.log',
            'mode': 'a',
            'maxBytes': 5242880,
            'backupCount': 10
        }
    },
    'loggers': {
        # root logger
        '': {
            'handlers': ['console', 'cron_file', 'error_file'],
            'level': 'DEBUG',
        },
        'crons': {
            'handlers': ['console', 'cron_file', 'error_file'],
            'level': 'DEBUG',
        }
    }
}

【问题讨论】:

    标签: python python-3.x snowflake-cloud-data-platform python-logging


    【解决方案1】:

    查看原来的调用栈:

    Call stack:
      File "~/.virtualenvs/test-ven/lib/python3.7/site-packages/snowflake/connector/connection.py", line 211, in __del__
        self.close(retry=False)
      File "~/.virtualenvs/test-ven/lib/python3.7/site-packages/snowflake/connector/connection.py", line 529, in close
        logger.info('closed')
    

    当从内存中删除对象时,它会告诉您它正在尝试记录。 调用__del__ 有两种情况。你打电话给del object 或者它被垃圾收集。对于后一种情况,它会发生两次。第一个是refcount 到达0 时,第二个是解释器关闭时,即。你的程序退出了。

    解释器关闭时会发生此错误。此时,对象被删除,全局命名空间被清除。这意味着当__del__ 调用它时,open 关键字不再存在。有时会发生此错误,有时不会。因为垃圾收集器在自己的线程上运行,有时它会在日志调用之前删除全局命名空间。

    那么你能做些什么呢?

    显而易见的选择是不要将日志记录放在__del__ 中。但是由于它是其他人的库,因此此选项实际上并不可行。另一种选择是记住在不再需要时自行关闭连接。 但是有更通用的解决方案,即在程序结束时禁用日志记录,就在解释器关闭发生之前:

    import logging
    # your code
    
    if __name__ == '__main__':
        # your code
        logging.disable(logging.CRITICAL)    # levels less than or equal to Critical are ignored
        # EOF
    

    【讨论】:

      猜你喜欢
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-21
      相关资源
      最近更新 更多