我有一个Python包,我使用PyInstaller将其作为单个可执行文件分发。
我将logging
模块与基于文件的配置结合使用。为了实现基于云的集中式日志记录,我决定使用模块loggly-python-handler和python-json-logger将日志信息发送到loggly。后者将日志消息转换为JSON,第一个使用HTTP(S)调用将(JSON)日志消息发送到Loggly。
我的日志记录配置摘录(实际TOKEN
值已被删除):
[handler_loggly]
class=loggly.handlers.HTTPSHandler
formatter=json
args=('https://logs-01.loggly.com/inputs/TOKEN/tag/test','POST')
level=CRITICAL
[formatter_json]
format= %(name)s %(asctime)s %(filename)s %(created)f %(funcName)s %(levelno)s %(lineno)d %(msecs)d %(levelname)s %(message)s
class=pythonjsonlogger.jsonlogger.JsonFormatter
在安装了Python(和必要的库)的机器上运行它时可以正常工作。但是,PyInstaller生成的可执行文件似乎缺少必需的日志记录模块(因为它们不会在源代码中的任何位置导入,只是在日志记录配置中引用)。
运行可执行文件时输出错误:
Traceback (most recent call last):
File "c:\python35\lib\logging\config.py", line 98, in _resolve
AttributeError: module 'pythonjsonlogger' has no attribute 'jsonlogger'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 64, in <module>
File "<string>", line 41, in main
File "<string>", line 28, in init_logger
File "c:\python35\lib\logging\config.py", line 76, in fileConfig
File "c:\python35\lib\logging\config.py", line 123, in _create_formatters
File "c:\python35\lib\logging\config.py", line 100, in _resolve
ImportError: No module named 'pythonjsonlogger.jsonlogger'
main returned -1
尝试1
我已尝试使用PyInstaller的hidden import functionality来引用这两个模块:
pyinstaller \
--log-level=DEBUG \
--onefile \
--hidden-import=loggly \
--hidden-import=pythonjsonlogger main.py
日志文件输出:
3463 DEBUG: Hidden import: loggly
3463 INFO: Analyzing hidden import 'loggly'
3463 DEBUG: Hidden import: pythonjsonlogger
3478 INFO: Analyzing hidden import 'pythonjsonlogger'
尝试2
尝试在PyInstaller规范文件中指定导入(摘录如下):
a = Analysis(['main.py'],
pathex=['C:\\Dev\\pyWebiExport'],
binaries=None,
datas=None,
hiddenimports=['loggly.handlers.*', 'pythonjsonlogger.jsonlogger.*'],
hookspath=None,
…
日志文件输出:
3510 DEBUG: Hidden import: loggly.handlers.*
3510 INFO: Analyzing hidden import 'loggly.handlers.*'
5694 ERROR: Hidden import 'loggly.handlers.*' not found
5694 DEBUG: Hidden import: pythonjsonlogger.jsonlogger.*
5694 INFO: Analyzing hidden import 'pythonjsonlogger.jsonlogger.*'
5709 ERROR: Hidden import 'pythonjsonlogger.jsonlogger.*' not found
版本信息:
答案 0 :(得分:1)
我能够通过为PyInstaller写一个runtime hook来解决这个问题,PyInstaller基本上包含一个包含以下内容的附加Python脚本:
import pythonjsonlogger.jsonlogger
import loggly.handlers
我将这些语句保存在文件loggly_hook.py
中,并使用以下参数运行PyInstaller:
pyinstaller \
--log-level=DEBUG \
--onefile \
--clean \
--runtime-hook=loggly_hook.py main.py
PyInstaller日志输出:
10374 INFO: Analyzing run-time hooks ...
10389 INFO: Including custom run-time hook 'loggly_hook.py'