python-3.x - Python 日志记录调用在 useTime 崩溃

标签 python-3.x logging crash

每隔 50-100 次调用记录器,程序就会崩溃并显示以下跟踪消息:

Mar 20 07:10:14 service.bash[7693]: Fatal Python error: Cannot recover from stack overflow.
Mar 20 07:10:14 service.bash[7693]: Current thread 0x76fa3010 (most recent call first):
Mar 20 07:10:14 service.bash[7693]:   File "/usr/lib/python3.5/logging/__init__.py", line 381 in usesTime
Mar 20 07:10:14 service.bash[7693]:   File "/usr/lib/python3.5/logging/__init__.py", line 537 in usesTime
Mar 20 07:10:14 service.bash[7693]:   File "/usr/lib/python3.5/logging/__init__.py", line 569 in format
Mar 20 07:10:14 service.bash[7693]:   File "/usr/lib/python3.5/logging/__init__.py", line 831 in format
Mar 20 07:10:14 service.bash[7693]:   File "/usr/lib/python3.5/logging/__init__.py", line 981 in emit
Mar 20 07:10:14 service.bash[7693]:   File "/usr/lib/python3.5/logging/__init__.py", line 856 in handle
Mar 20 07:10:14 service.bash[7693]:   File "/usr/lib/python3.5/logging/__init__.py", line 1488 in callHandlers
Mar 20 07:10:14 service.bash[7693]:   File "/usr/lib/python3.5/logging/__init__.py", line 1426 in handle
Mar 20 07:10:14 service.bash[7693]:   File "/usr/lib/python3.5/logging/__init__.py", line 1416 in _log
Mar 20 07:10:14 service.bash[7693]:   File "/usr/lib/python3.5/logging/__init__.py", line 1280 in info
Mar 20 07:10:14 service.bash[7693]:   File "*****************_app/base.py", line 63 in log_this

知道什么可能导致这次崩溃吗?

在程序的其他地方没有看到类似或其他日志记录调用导致程序崩溃。

这是对记录器进行的调用的堆栈:

self.info("cs={} miso={} mosi{} clk{}".format( self.csPin, self.misoPin, self.mosiPin, self.clkPin))
|
self.log_this("info", msg)
|
self.log.info(msg)

记录器通过以下方式在基类初始化例程中设置:

# Global logger is declared as a class attribute
cls.log = logging.getLogger(cls.args["app"])
c_handler = logging.StreamHandler()
f_handler = logging.handlers.RotatingFileHandler(
                cls.args["--log"],
                maxBytes=(10**6)*int(cls.args["--logsize"]), # CLI is in MB
                backupCount=1)


# Create handlers
if cls.args["--debug"]:
    cls.log.setLevel(logging.DEBUG)
    c_handler.setLevel(logging.DEBUG)
    f_handler.setLevel(logging.DEBUG)
else:
    cls.log.setLevel(logging.INFO)
    c_handler.setLevel(logging.INFO)
    f_handler.setLevel(logging.INFO)


# Create formatters and add it to handlers
c_format = logging.Formatter('%(name)s: %(levelname)s: %(message)s')
f_format = logging.Formatter('%(asctime)s: %(name)s: %(levelname)s: %(message)s')
c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)


# Add handlers to the logger
cls.log.addHandler(c_handler)
cls.log.addHandler(f_handler)


cls.log.info("Logger initialized")

谢谢。

最佳答案

您是否使用 Windows 并在 Python 中切换到 UTF8 语言环境?在这个特定场景中存在一个 Python 错误(类似问题: https://bugs.python.org/issue36792 )。这是在我的机器上重现该错误的最小代码示例:

import locale
import logging

handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s'))
logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger().addHandler(handler)

logging.info(1)
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
logging.info(2)

输出:

2019-10-25 21:20:15,657
Process finished with exit code -1073740940 (0xC0000374)

如果您不确定这是否与您的问题有关,请尝试在调用日志记录模块之前添加以下行,看看是否可以解决问题:

locale.setlocale(locale.LC_ALL, 'en_US')

关于python-3.x - Python 日志记录调用在 useTime 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55262139/

相关文章:

python - 列表增量快捷方式 (a+=[1]) 和更长方式 (a=a+[1]) 之间的区别

Python 记录到标准输出和日志文件

python - 什么是中间值?

python-3.x - 如何将多个单元格添加到bigtable表中的一行?

python - 如何映射两个字符串之间的差异?

logging - 试图理解 tomcat hs_err_pid 日志

java - 在使用 tomcat server.xml 或 web.xml 创建新日志之前备份日志文件

crash - 启动 WindowsPhone 7 模拟器会导致正在运行的 VMWare(服务器)实例崩溃

crash - UICollectionView scrollToItemAtIndexPath崩溃iOS 10

c# - 当所有行的高度超过DataGridview的视觉高度时,DataGridview的ScrollBars会Crash