【问题标题】:logging works wrong with pyinstaller application: calling module and function are set to 'logging' and 'debug'pyinstaller 应用程序的日志记录工作错误:调用模块和函数设置为“日志记录”和“调试”
【发布时间】:2017-09-06 22:20:26
【问题描述】:

我有一个使用模块'logging'的python应用程序,我用pysintaller打包它。

我正在使用以下格式字符串:

%(asctime)s %(levelname)s [%(threadName)s:%(module)s.%(funcName)s()] %(message)s

如果我将应用程序作为 python 脚本运行,它可以正常工作,'%(module)s.%(funcName)s' 会被调用模块和函数的实际值替换:

2014-06-19 18:46:10,373 DEBUG [MainThread:subcommands.exec_cmd()] Executing `service iptables stop` on server centos6root
2014-06-19 18:46:10,373 DEBUG [MainThread:ssh._connect()] Trying to connect to server centos6root
2014-06-19 18:46:10,945 DEBUG [MainThread:ssh._connect()] Established connection with root@192.168.122.57:22
2014-06-19 18:46:11,533 DEBUG [MainThread:subcommands.exec_cmd()] exitstatus = 0
2014-06-19 18:46:11,648 DEBUG [MainThread:ssh.cleanup()] Closed connection to server centos6root
2014-06-19 18:46:11,649 DEBUG [MainThread:hwswa2.main()] Application finished

但是,如果我使用 pyinstaller 打包我的应用程序,它现在会用 'logging.debug' 替换 '%(module)s.%(funcName)s':

2014-06-19 19:04:05,577 DEBUG [MainThread:logging.debug()] Executing `echo hello` on server centos6root
2014-06-19 19:04:05,577 DEBUG [MainThread:logging.debug()] Trying to connect to server centos6root
2014-06-19 19:04:06,293 DEBUG [MainThread:logging.debug()] Established connection with root@192.168.122.57:22
2014-06-19 19:04:06,705 DEBUG [MainThread:logging.debug()] exitstatus = 0
2014-06-19 19:04:06,707 DEBUG [MainThread:logging.debug()] Closed connection to server centos6root
2014-06-19 19:04:06,707 DEBUG [MainThread:logging.debug()] Application finished

可能是什么原因以及如何解决此行为?

【问题讨论】:

标签: python logging pyinstaller


【解决方案1】:

我直接调用logging.info()logging.debug() 等。

通过为每个模块使用单独的记录器解决了这个问题

import logging
logger = logging.getLogger(__name__)

并致电logger.info()logger.debug() 等。

【讨论】:

    猜你喜欢
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    相关资源
    最近更新 更多